Skip to content

Commit 1a090ca

Browse files
authored
Merge pull request #1427 from tony-clarke-amdocs/ownerref
Allow specify an owner reference for leader election lock
2 parents ffb7dee + 33bc232 commit 1a090ca

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed

extended/src/main/java/io/kubernetes/client/extended/leaderelection/LeaderElectionConfig.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package io.kubernetes.client.extended.leaderelection;
1414

15+
import io.kubernetes.client.openapi.models.V1OwnerReference;
1516
import java.time.Duration;
1617

1718
public class LeaderElectionConfig {
@@ -24,14 +25,26 @@ public class LeaderElectionConfig {
2425

2526
private Duration retryPeriod;
2627

28+
private V1OwnerReference ownerReference;
29+
2730
public LeaderElectionConfig() {}
2831

2932
public LeaderElectionConfig(
3033
Lock lock, Duration leaseDuration, Duration renewDeadline, Duration retryPeriod) {
34+
this(lock, leaseDuration, renewDeadline, retryPeriod, null);
35+
}
36+
37+
public LeaderElectionConfig(
38+
Lock lock,
39+
Duration leaseDuration,
40+
Duration renewDeadline,
41+
Duration retryPeriod,
42+
V1OwnerReference ownerReference) {
3143
this.lock = lock;
3244
this.leaseDuration = leaseDuration;
3345
this.renewDeadline = renewDeadline;
3446
this.retryPeriod = retryPeriod;
47+
this.ownerReference = ownerReference;
3548
}
3649

3750
public Lock getLock() {
@@ -65,4 +78,12 @@ public Duration getRetryPeriod() {
6578
public void setRetryPeriod(Duration retryPeriod) {
6679
this.retryPeriod = retryPeriod;
6780
}
81+
82+
public V1OwnerReference getOwnerReference() {
83+
return ownerReference;
84+
}
85+
86+
public void setOwnerReference(V1OwnerReference ownerReference) {
87+
this.ownerReference = ownerReference;
88+
}
6889
}

extended/src/main/java/io/kubernetes/client/extended/leaderelection/LeaderElectionRecord.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package io.kubernetes.client.extended.leaderelection;
1414

15+
import io.kubernetes.client.openapi.models.V1OwnerReference;
1516
import java.util.Date;
1617

1718
public class LeaderElectionRecord {
@@ -26,6 +27,8 @@ public class LeaderElectionRecord {
2627

2728
private int leaderTransitions;
2829

30+
private V1OwnerReference ownerReference;
31+
2932
public LeaderElectionRecord() {}
3033

3134
public LeaderElectionRecord(
@@ -34,11 +37,22 @@ public LeaderElectionRecord(
3437
Date acquireTime,
3538
Date renewTime,
3639
int leaderTransitions) {
40+
this(holderIdentity, leaseDurationSeconds, acquireTime, renewTime, leaderTransitions, null);
41+
}
42+
43+
public LeaderElectionRecord(
44+
String holderIdentity,
45+
int leaseDurationSeconds,
46+
Date acquireTime,
47+
Date renewTime,
48+
int leaderTransitions,
49+
V1OwnerReference ownerReference) {
3750
this.holderIdentity = holderIdentity;
3851
this.leaseDurationSeconds = leaseDurationSeconds;
3952
this.acquireTime = acquireTime;
4053
this.renewTime = renewTime;
4154
this.leaderTransitions = leaderTransitions;
55+
this.ownerReference = ownerReference;
4256
}
4357

4458
public String getHolderIdentity() {
@@ -81,6 +95,14 @@ public void setLeaderTransitions(int leaderTransitions) {
8195
this.leaderTransitions = leaderTransitions;
8296
}
8397

98+
public V1OwnerReference getOwnerReference() {
99+
return ownerReference;
100+
}
101+
102+
public void setOwnerReference(V1OwnerReference ownerReference) {
103+
this.ownerReference = ownerReference;
104+
}
105+
84106
@Override
85107
public boolean equals(Object o) {
86108
if (this == o) return true;
@@ -95,6 +117,11 @@ public boolean equals(Object o) {
95117
} else {
96118
if (!holderIdentity.equals(that.holderIdentity)) return false;
97119
}
120+
if (ownerReference == null) {
121+
if (that.ownerReference != null) return false;
122+
} else {
123+
if (!ownerReference.equals(that.ownerReference)) return false;
124+
}
98125
if (acquireTime == null) {
99126
if (that.acquireTime != null) return false;
100127
} else {

extended/src/main/java/io/kubernetes/client/extended/leaderelection/LeaderElector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ private boolean tryAcquireOrRenew() {
241241
Long.valueOf(config.getLeaseDuration().getSeconds()).intValue(),
242242
now,
243243
now,
244-
0);
244+
0,
245+
config.getOwnerReference());
245246

246247
// 1. obtain or create the ElectionRecord
247248
LeaderElectionRecord oldLeaderElectionRecord;

extended/src/main/java/io/kubernetes/client/extended/leaderelection/resourcelock/ConfigMapLock.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.kubernetes.client.openapi.models.V1ConfigMap;
2222
import io.kubernetes.client.openapi.models.V1ObjectMeta;
2323
import java.net.HttpURLConnection;
24+
import java.util.Collections;
2425
import java.util.HashMap;
2526
import java.util.Map;
2627
import java.util.concurrent.atomic.AtomicReference;
@@ -89,6 +90,9 @@ public boolean create(LeaderElectionRecord record) {
8990
LeaderElectionRecordAnnotationKey,
9091
coreV1Client.getApiClient().getJSON().serialize(record));
9192
objectMeta.setAnnotations(annotations);
93+
if (record.getOwnerReference() != null) {
94+
objectMeta.setOwnerReferences(Collections.singletonList(record.getOwnerReference()));
95+
}
9296
configMap.setMetadata(objectMeta);
9397
V1ConfigMap createdConfigMap =
9498
coreV1Client.createNamespacedConfigMap(namespace, configMap, null, null, null);

extended/src/main/java/io/kubernetes/client/extended/leaderelection/resourcelock/EndpointsLock.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.kubernetes.client.openapi.models.V1Endpoints;
2222
import io.kubernetes.client.openapi.models.V1ObjectMeta;
2323
import java.net.HttpURLConnection;
24+
import java.util.Collections;
2425
import java.util.HashMap;
2526
import java.util.Map;
2627
import java.util.concurrent.atomic.AtomicReference;
@@ -89,6 +90,9 @@ public boolean create(LeaderElectionRecord record) {
8990
LeaderElectionRecordAnnotationKey,
9091
coreV1Client.getApiClient().getJSON().serialize(record));
9192
objectMeta.setAnnotations(annotations);
93+
if (record.getOwnerReference() != null) {
94+
objectMeta.setOwnerReferences(Collections.singletonList(record.getOwnerReference()));
95+
}
9296
endpoints.setMetadata(objectMeta);
9397
V1Endpoints createdendpoints =
9498
coreV1Client.createNamespacedEndpoints(namespace, endpoints, null, null, null);

extended/src/main/java/io/kubernetes/client/extended/leaderelection/resourcelock/LeaseLock.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.kubernetes.client.openapi.models.V1LeaseSpec;
2323
import io.kubernetes.client.openapi.models.V1ObjectMeta;
2424
import java.net.HttpURLConnection;
25+
import java.util.Collections;
2526
import java.util.concurrent.atomic.AtomicReference;
2627
import org.joda.time.DateTime;
2728
import org.slf4j.Logger;
@@ -62,12 +63,16 @@ public LeaderElectionRecord get() throws ApiException {
6263
@Override
6364
public boolean create(LeaderElectionRecord record) {
6465
try {
66+
V1ObjectMeta objectMeta = new V1ObjectMeta();
67+
objectMeta.setName(name);
68+
objectMeta.setNamespace(namespace);
69+
if (record.getOwnerReference() != null) {
70+
objectMeta.setOwnerReferences(Collections.singletonList(record.getOwnerReference()));
71+
}
6572
V1Lease createdLease =
6673
coordinationV1Api.createNamespacedLease(
6774
namespace,
68-
new V1Lease()
69-
.metadata(new V1ObjectMeta().namespace(namespace).name(name))
70-
.spec(getLeaseFromRecord(record)),
75+
new V1Lease().metadata(objectMeta).spec(getLeaseFromRecord(record)),
7176
null,
7277
null,
7378
null);

0 commit comments

Comments
 (0)