Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a89b7b7

Browse filesBrowse files
committed
feat: Allow leases to have custom labels set when a new holder has the lease
1 parent 2ac0bdf commit a89b7b7
Copy full SHA for a89b7b7

File tree

3 files changed

+390
-3
lines changed
Filter options

3 files changed

+390
-3
lines changed

‎staging/src/k8s.io/client-go/tools/leaderelection/resourcelock/interface.go

Copy file name to clipboardExpand all lines: staging/src/k8s.io/client-go/tools/leaderelection/resourcelock/interface.go
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,15 @@ type Interface interface {
100100
}
101101

102102
// Manufacture will create a lock of a given type according to the input parameters
103-
func New(lockType string, ns string, name string, coreClient corev1.CoreV1Interface, coordinationClient coordinationv1.CoordinationV1Interface, rlc ResourceLockConfig) (Interface, error) {
103+
func new(lockType string, ns string, name string, coreClient corev1.CoreV1Interface, coordinationClient coordinationv1.CoordinationV1Interface, rlc ResourceLockConfig, labels map[string]string) (Interface, error) {
104104
leaseLock := &LeaseLock{
105105
LeaseMeta: metav1.ObjectMeta{
106106
Namespace: ns,
107107
Name: name,
108108
},
109109
Client: coordinationClient,
110110
LockConfig: rlc,
111+
Labels: labels,
111112
}
112113
switch lockType {
113114
case endpointsResourceLock:
@@ -125,6 +126,14 @@ func New(lockType string, ns string, name string, coreClient corev1.CoreV1Interf
125126
}
126127
}
127128

129+
func New(lockType string, ns string, name string, coreClient corev1.CoreV1Interface, coordinationClient coordinationv1.CoordinationV1Interface, rlc ResourceLockConfig) (Interface, error) {
130+
return new(lockType, ns, name, coreClient, coordinationClient, rlc, nil)
131+
}
132+
133+
func NewWithLabels(lockType string, ns string, name string, coreClient corev1.CoreV1Interface, coordinationClient coordinationv1.CoordinationV1Interface, rlc ResourceLockConfig, labels map[string]string) (Interface, error) {
134+
return new(lockType, ns, name, coreClient, coordinationClient, rlc, labels)
135+
}
136+
128137
// NewFromKubeconfig will create a lock of a given type according to the input parameters.
129138
// Timeout set for a client used to contact to Kubernetes should be lower than
130139
// RenewDeadline to keep a single hung request from forcing a leader loss.

‎staging/src/k8s.io/client-go/tools/leaderelection/resourcelock/leaselock.go

Copy file name to clipboardExpand all lines: staging/src/k8s.io/client-go/tools/leaderelection/resourcelock/leaselock.go
+13-2Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type LeaseLock struct {
3535
Client coordinationv1client.LeasesGetter
3636
LockConfig ResourceLockConfig
3737
lease *coordinationv1.Lease
38+
Labels map[string]string
3839
}
3940

4041
// Get returns the election record from a Lease spec
@@ -55,13 +56,16 @@ func (ll *LeaseLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte, er
5556
// Create attempts to create a Lease
5657
func (ll *LeaseLock) Create(ctx context.Context, ler LeaderElectionRecord) error {
5758
var err error
58-
ll.lease, err = ll.Client.Leases(ll.LeaseMeta.Namespace).Create(ctx, &coordinationv1.Lease{
59+
lease := &coordinationv1.Lease{
5960
ObjectMeta: metav1.ObjectMeta{
6061
Name: ll.LeaseMeta.Name,
6162
Namespace: ll.LeaseMeta.Namespace,
63+
Labels: ll.Labels,
6264
},
6365
Spec: LeaderElectionRecordToLeaseSpec(&ler),
64-
}, metav1.CreateOptions{})
66+
}
67+
68+
ll.lease, err = ll.Client.Leases(ll.LeaseMeta.Namespace).Create(ctx, lease, metav1.CreateOptions{})
6569
return err
6670
}
6771

@@ -72,6 +76,13 @@ func (ll *LeaseLock) Update(ctx context.Context, ler LeaderElectionRecord) error
7276
}
7377
ll.lease.Spec = LeaderElectionRecordToLeaseSpec(&ler)
7478

79+
if ll.Labels != nil {
80+
// Only overwrite the labels that are specifically set
81+
for k, v := range ll.Labels {
82+
ll.lease.Labels[k] = v
83+
}
84+
}
85+
7586
lease, err := ll.Client.Leases(ll.LeaseMeta.Namespace).Update(ctx, ll.lease, metav1.UpdateOptions{})
7687
if err != nil {
7788
return err

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.