Skip to content

Commit ddcdc12

Browse files
DerekFrankk8s-publishing-bot
authored andcommitted
fix: Update unit test to catch actual nil Labels case and fix functionality to handle nil Labels
Kubernetes-commit: 4ae3b64b73617c9f9f162874e87f500e265f05e1
1 parent 97396af commit ddcdc12

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

tools/leaderelection/resourcelock/leaselock.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ func (ll *LeaseLock) Update(ctx context.Context, ler LeaderElectionRecord) error
7777
ll.lease.Spec = LeaderElectionRecordToLeaseSpec(&ler)
7878

7979
if ll.Labels != nil {
80+
if ll.lease.Labels == nil {
81+
ll.lease.Labels = map[string]string{}
82+
}
8083
// Only overwrite the labels that are specifically set
8184
for k, v := range ll.Labels {
8285
ll.lease.Labels[k] = v

tools/leaderelection/resourcelock/leaselock_test.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func TestLeaseConversion(t *testing.T) {
266266
}
267267
}
268268

269-
func TestUpdateWithNilLabels(t *testing.T) {
269+
func TestUpdateWithNilLabelsOnLease(t *testing.T) {
270270
setup()
271271

272272
// Create initial lease
@@ -278,23 +278,30 @@ func TestUpdateWithNilLabels(t *testing.T) {
278278
t.Fatalf("Failed to get lease: %v", err)
279279
}
280280

281-
leaseLock.lease.Labels = map[string]string{"custom-key": "custom-val"}
281+
leaseLock.Labels = map[string]string{"custom-key": "custom-val"}
282282

283-
// Update labels
284-
lease, err := leaseLock.Client.Leases(testNamespace).Update(context.Background(), leaseLock.lease, metav1.UpdateOptions{})
285-
if err != nil {
286-
t.Fatalf("Failed to update lease labels: %v", err)
283+
// Update should succeed even with nil Labels on the lease itself
284+
if err := leaseLock.Update(context.Background(), testRecord); err != nil {
285+
t.Errorf("Update failed with nil Labels: %v", err)
287286
}
287+
}
288288

289-
val, exists := lease.Labels["custom-key"]
290-
if !exists {
291-
t.Error("Label was overidden on the lease")
289+
290+
func TestUpdateWithNilLabelsOnLeaseLock(t *testing.T) {
291+
setup()
292+
293+
// Create initial lease
294+
if err := leaseLock.Create(context.Background(), testRecord); err != nil {
295+
t.Fatalf("Failed to create lease: %v", err)
292296
}
293-
if val != "custom-val" {
294-
t.Errorf("Label value mismatch, got %q want %q", val, "custom-val")
297+
// Get the lease to initialize leaseLock.lease
298+
if _, _, err := leaseLock.Get(context.Background()); err != nil {
299+
t.Fatalf("Failed to get lease: %v", err)
295300
}
296301

297-
// Update should succeed even with nil Labels
302+
leaseLock.lease.Labels = map[string]string{"custom-key": "custom-val"}
303+
304+
// Update should succeed even with nil Labels on the leaselock
298305
if err := leaseLock.Update(context.Background(), testRecord); err != nil {
299306
t.Errorf("Update failed with nil Labels: %v", err)
300307
}
@@ -364,4 +371,4 @@ func TestLabelUpdate(t *testing.T) {
364371
if val != "custom-val-2" {
365372
t.Errorf("Label value mismatch, got %q want %q", val, "custom-val-2")
366373
}
367-
}
374+
}

0 commit comments

Comments
 (0)