forked from kubernetes-client/java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeaseLock.java
157 lines (142 loc) · 5.5 KB
/
LeaseLock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package io.kubernetes.client.extended.leaderelection.resourcelock;
import io.kubernetes.client.extended.leaderelection.LeaderElectionRecord;
import io.kubernetes.client.extended.leaderelection.Lock;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoordinationV1Api;
import io.kubernetes.client.openapi.models.V1Lease;
import io.kubernetes.client.openapi.models.V1LeaseSpec;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import java.net.HttpURLConnection;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Collections;
import java.util.Date;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LeaseLock implements Lock {
private static final Logger log = LoggerFactory.getLogger(LeaseLock.class);
// Namespace and name describes the endpoint object
// that the LeaderElector will attempt to lead.
private final String namespace;
private final String name;
private final String identity;
private CoordinationV1Api coordinationV1Api;
private AtomicReference<V1Lease> leaseRefer = new AtomicReference<>(null);
public LeaseLock(String namespace, String name, String identity) {
this(namespace, name, identity, Configuration.getDefaultApiClient());
}
public LeaseLock(String namespace, String name, String identity, ApiClient apiClient) {
this.namespace = namespace;
this.name = name;
this.identity = identity;
this.coordinationV1Api = new CoordinationV1Api(apiClient);
}
@Override
public LeaderElectionRecord get() throws ApiException {
V1Lease lease = coordinationV1Api.readNamespacedLease(name, namespace, null, null, null);
leaseRefer.set(lease);
return getRecordFromLease(lease.getSpec());
}
@Override
public boolean create(LeaderElectionRecord record) {
try {
V1ObjectMeta objectMeta = new V1ObjectMeta();
objectMeta.setName(name);
objectMeta.setNamespace(namespace);
if (record.getOwnerReference() != null) {
objectMeta.setOwnerReferences(Collections.singletonList(record.getOwnerReference()));
}
V1Lease createdLease =
coordinationV1Api.createNamespacedLease(
namespace,
new V1Lease().metadata(objectMeta).spec(getLeaseFromRecord(record)),
null,
null,
null);
leaseRefer.set(createdLease);
return true;
} catch (ApiException e) {
if (e.getCode() == HttpURLConnection.HTTP_CONFLICT) {
log.debug("received {} when creating lease lock", e.getCode(), e);
} else {
log.error("received {} when creating lease lock", e.getCode(), e);
}
return false;
}
}
@Override
public boolean update(LeaderElectionRecord record) {
try {
V1Lease latest = leaseRefer.get();
latest.setSpec(getLeaseFromRecord(record));
V1Lease updatedLease =
coordinationV1Api.replaceNamespacedLease(name, namespace, latest, null, null, null);
leaseRefer.set(updatedLease);
return true;
} catch (ApiException e) {
if (e.getCode() == HttpURLConnection.HTTP_CONFLICT) {
log.debug("received {} when updating lease lock", e.getCode(), e);
} else {
log.error("received {} when updating lease lock", e.getCode(), e);
}
return false;
}
}
@Override
public String identity() {
return identity;
}
@Override
public String describe() {
return namespace + "/" + name;
}
private LeaderElectionRecord getRecordFromLease(V1LeaseSpec lease) {
LeaderElectionRecord record = new LeaderElectionRecord();
if (lease.getAcquireTime() != null) {
record.setAcquireTime(new Date(lease.getAcquireTime().toInstant().toEpochMilli()));
}
if (lease.getRenewTime() != null) {
record.setRenewTime(new Date(lease.getRenewTime().toInstant().toEpochMilli()));
}
record.setHolderIdentity(lease.getHolderIdentity());
record.setLeaderTransitions(lease.getLeaseTransitions());
record.setLeaseDurationSeconds(lease.getLeaseDurationSeconds());
return record;
}
private V1LeaseSpec getLeaseFromRecord(LeaderElectionRecord record) {
V1LeaseSpec spec =
new V1LeaseSpec()
.holderIdentity(record.getHolderIdentity())
.leaseDurationSeconds(record.getLeaseDurationSeconds())
.leaseTransitions(record.getLeaderTransitions());
if (record.getAcquireTime() != null) {
spec =
spec.acquireTime(
OffsetDateTime.ofInstant(
Instant.ofEpochMilli(record.getAcquireTime().getTime()), ZoneOffset.UTC));
}
if (record.getRenewTime() != null) {
spec =
spec.renewTime(
OffsetDateTime.ofInstant(
Instant.ofEpochMilli(record.getRenewTime().getTime()), ZoneOffset.UTC));
}
return spec;
}
}