mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-18 16:34:32 +00:00
feat: Allow leases to have custom labels set when a new holder has the lease
Kubernetes-commit: 109ae1bacadfa5c8655adb4594762c77b135f446
This commit is contained in:
committed by
Kubernetes Publisher
parent
647bbbe859
commit
34f791d2b1
@@ -35,6 +35,7 @@ type LeaseLock struct {
|
||||
Client coordinationv1client.LeasesGetter
|
||||
LockConfig ResourceLockConfig
|
||||
lease *coordinationv1.Lease
|
||||
Labels map[string]string
|
||||
}
|
||||
|
||||
// Get returns the election record from a Lease spec
|
||||
@@ -55,13 +56,16 @@ func (ll *LeaseLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte, er
|
||||
// Create attempts to create a Lease
|
||||
func (ll *LeaseLock) Create(ctx context.Context, ler LeaderElectionRecord) error {
|
||||
var err error
|
||||
ll.lease, err = ll.Client.Leases(ll.LeaseMeta.Namespace).Create(ctx, &coordinationv1.Lease{
|
||||
lease := &coordinationv1.Lease{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: ll.LeaseMeta.Name,
|
||||
Namespace: ll.LeaseMeta.Namespace,
|
||||
Labels: ll.Labels,
|
||||
},
|
||||
Spec: LeaderElectionRecordToLeaseSpec(&ler),
|
||||
}, metav1.CreateOptions{})
|
||||
}
|
||||
|
||||
ll.lease, err = ll.Client.Leases(ll.LeaseMeta.Namespace).Create(ctx, lease, metav1.CreateOptions{})
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -72,6 +76,13 @@ func (ll *LeaseLock) Update(ctx context.Context, ler LeaderElectionRecord) error
|
||||
}
|
||||
ll.lease.Spec = LeaderElectionRecordToLeaseSpec(&ler)
|
||||
|
||||
if ll.Labels != nil {
|
||||
// Only overwrite the labels that are specifically set
|
||||
for k, v := range ll.Labels {
|
||||
ll.lease.Labels[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
lease, err := ll.Client.Leases(ll.LeaseMeta.Namespace).Update(ctx, ll.lease, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
|
Reference in New Issue
Block a user