mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-13 22:01:43 +00:00
Merge pull request #133573 from DerekFrank/lease-metadata-fix
fix: Update unit test to catch actual nil Labels case and fix functionality to handle nil Labels Kubernetes-commit: 7c7fd78793c8974f22fcc6d7d58f263d987b238e
This commit is contained in:
@@ -77,6 +77,9 @@ func (ll *LeaseLock) Update(ctx context.Context, ler LeaderElectionRecord) error
|
||||
ll.lease.Spec = LeaderElectionRecordToLeaseSpec(&ler)
|
||||
|
||||
if ll.Labels != nil {
|
||||
if ll.lease.Labels == nil {
|
||||
ll.lease.Labels = map[string]string{}
|
||||
}
|
||||
// Only overwrite the labels that are specifically set
|
||||
for k, v := range ll.Labels {
|
||||
ll.lease.Labels[k] = v
|
||||
|
@@ -266,7 +266,7 @@ func TestLeaseConversion(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateWithNilLabels(t *testing.T) {
|
||||
func TestUpdateWithNilLabelsOnLease(t *testing.T) {
|
||||
setup()
|
||||
|
||||
// Create initial lease
|
||||
@@ -278,23 +278,33 @@ func TestUpdateWithNilLabels(t *testing.T) {
|
||||
t.Fatalf("Failed to get lease: %v", err)
|
||||
}
|
||||
|
||||
leaseLock.lease.Labels = nil
|
||||
|
||||
leaseLock.Labels = map[string]string{"custom-key": "custom-val"}
|
||||
|
||||
// Update should succeed even with nil Labels on the lease itself
|
||||
if err := leaseLock.Update(context.Background(), testRecord); err != nil {
|
||||
t.Errorf("Update failed with nil Labels: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateWithNilLabelsOnLeaseLock(t *testing.T) {
|
||||
setup()
|
||||
|
||||
// Create initial lease
|
||||
if err := leaseLock.Create(context.Background(), testRecord); err != nil {
|
||||
t.Fatalf("Failed to create lease: %v", err)
|
||||
}
|
||||
// Get the lease to initialize leaseLock.lease
|
||||
if _, _, err := leaseLock.Get(context.Background()); err != nil {
|
||||
t.Fatalf("Failed to get lease: %v", err)
|
||||
}
|
||||
|
||||
leaseLock.Labels = nil
|
||||
|
||||
leaseLock.lease.Labels = map[string]string{"custom-key": "custom-val"}
|
||||
|
||||
// Update labels
|
||||
lease, err := leaseLock.Client.Leases(testNamespace).Update(context.Background(), leaseLock.lease, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to update lease labels: %v", err)
|
||||
}
|
||||
|
||||
val, exists := lease.Labels["custom-key"]
|
||||
if !exists {
|
||||
t.Error("Label was overidden on the lease")
|
||||
}
|
||||
if val != "custom-val" {
|
||||
t.Errorf("Label value mismatch, got %q want %q", val, "custom-val")
|
||||
}
|
||||
|
||||
// Update should succeed even with nil Labels
|
||||
// Update should succeed even with nil Labels on the leaselock
|
||||
if err := leaseLock.Update(context.Background(), testRecord); err != nil {
|
||||
t.Errorf("Update failed with nil Labels: %v", err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user