use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class LeaderElector method tryAcquireOrRenew.
private boolean tryAcquireOrRenew() {
Date now = new Date();
Lock lock = config.getLock();
LeaderElectionRecord leaderElectionRecord = new LeaderElectionRecord(lock.identity(), Long.valueOf(config.getLeaseDuration().getSeconds()).intValue(), now, now, 0, config.getOwnerReference());
// 1. obtain or create the ElectionRecord
LeaderElectionRecord oldLeaderElectionRecord;
try {
oldLeaderElectionRecord = lock.get();
} catch (ApiException e) {
if (e.getCode() != HttpURLConnection.HTTP_NOT_FOUND) {
log.error("Error retrieving resource lock {}", lock.describe(), e);
return false;
}
if (log.isDebugEnabled()) {
log.debug("Lock not found, try to create it");
}
// No Lock resource exists, try to get leadership by creating it
return createLock(lock, leaderElectionRecord);
}
if (oldLeaderElectionRecord == null || oldLeaderElectionRecord.getAcquireTime() == null || oldLeaderElectionRecord.getRenewTime() == null || oldLeaderElectionRecord.getHolderIdentity() == null) {
// updating it
if (log.isDebugEnabled()) {
log.debug("Update lock to get lease");
}
if (oldLeaderElectionRecord != null) {
// maintain the leaderTransitions
leaderElectionRecord.setLeaderTransitions(oldLeaderElectionRecord.getLeaderTransitions() + 1);
}
return updateLock(lock, leaderElectionRecord);
}
// 2. Record obtained with LeaderElectionRecord, check the Identity & Time
if (!oldLeaderElectionRecord.equals(this.observedRecord)) {
this.observedRecord = oldLeaderElectionRecord;
this.observedTimeMilliSeconds = System.currentTimeMillis();
}
if (observedTimeMilliSeconds + config.getLeaseDuration().toMillis() > now.getTime() && !isLeader()) {
log.debug("Lock is held by {} and has not yet expired", oldLeaderElectionRecord.getHolderIdentity());
return false;
}
// here. Let's correct it before updating.
if (isLeader()) {
leaderElectionRecord.setAcquireTime(oldLeaderElectionRecord.getAcquireTime());
leaderElectionRecord.setLeaderTransitions(oldLeaderElectionRecord.getLeaderTransitions());
} else {
leaderElectionRecord.setLeaderTransitions(oldLeaderElectionRecord.getLeaderTransitions() + 1);
}
if (log.isDebugEnabled()) {
log.debug("Update lock to renew lease");
}
boolean renewalStatus = updateLock(lock, leaderElectionRecord);
if (renewalStatus && log.isDebugEnabled()) {
log.debug("TryAcquireOrRenew return success");
}
return renewalStatus;
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class ConfigMapLock method create.
@Override
public boolean create(LeaderElectionRecord record) {
try {
V1ConfigMap configMap = new V1ConfigMap();
V1ObjectMeta objectMeta = new V1ObjectMeta();
objectMeta.setName(name);
objectMeta.setNamespace(namespace);
Map<String, String> annotations = new HashMap<>();
annotations.put(LeaderElectionRecordAnnotationKey, coreV1Client.getApiClient().getJSON().serialize(record));
objectMeta.setAnnotations(annotations);
if (record.getOwnerReference() != null) {
objectMeta.setOwnerReferences(Collections.singletonList(record.getOwnerReference()));
}
configMap.setMetadata(objectMeta);
V1ConfigMap createdConfigMap = coreV1Client.createNamespacedConfigMap(namespace, configMap, null, null, null, null);
configMapRefer.set(createdConfigMap);
return true;
} catch (ApiException e) {
if (e.getCode() == HttpURLConnection.HTTP_CONFLICT) {
log.debug("received {} when creating configmap lock", e.getCode(), e);
} else {
log.error("received {} when creating configmap lock", e.getCode(), e);
}
return false;
}
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class ConfigMapLock method update.
@Override
public boolean update(LeaderElectionRecord record) {
try {
V1ConfigMap configMap = configMapRefer.get();
configMap.getMetadata().putAnnotationsItem(LeaderElectionRecordAnnotationKey, coreV1Client.getApiClient().getJSON().serialize(record));
// TODO consider to retry if receiving a 409 code
V1ConfigMap replacedConfigMap = coreV1Client.replaceNamespacedConfigMap(name, namespace, configMap, null, null, null, null);
configMapRefer.set(replacedConfigMap);
return true;
} catch (ApiException e) {
if (e.getCode() == HttpURLConnection.HTTP_CONFLICT) {
log.debug("received {} when updating configmap lock", e.getCode(), e);
} else {
log.error("received {} when updating configmap lock", e.getCode(), e);
}
return false;
}
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class DeploymentHelper method listReplicaSets.
/**
* listReplicaSets returns a list of RSes the given deployment targets. Note that this does NOT
* attempt to reconcile ControllerRef (adopt/orphan), because only the controller itself should do
* that. However, it does filter out anything whose ControllerRef doesn't match.
*/
private static List<V1ReplicaSet> listReplicaSets(V1Deployment deployment, AppsV1Api api) throws ApiException {
String namespace = deployment.getMetadata().getNamespace();
LabelSelector selector = LabelSelector.parse(deployment.getSpec().getSelector());
List<V1ReplicaSet> all = rsListFromClient(namespace, selector.toString(), api);
List<V1ReplicaSet> owned = new ArrayList<>(all.size());
for (V1ReplicaSet rs : all) {
List<V1OwnerReference> refs = rs.getMetadata().getOwnerReferences();
Optional<V1OwnerReference> ref = refs.stream().filter(o -> o.getController() != null && o.getController()).findAny();
// Only include those whose ControllerRef matches the Deployment.
if (ref.isPresent() && ref.get().getUid().equals(deployment.getMetadata().getUid())) {
owned.add(rs);
}
}
return owned;
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class EndpointsLock method update.
@Override
public boolean update(LeaderElectionRecord record) {
try {
V1Endpoints endpoints = endpointsRefer.get();
endpoints.getMetadata().putAnnotationsItem(LeaderElectionRecordAnnotationKey, coreV1Client.getApiClient().getJSON().serialize(record));
// TODO consider to retry if receiving a 409 code
V1Endpoints replacedEndpoints = coreV1Client.replaceNamespacedEndpoints(name, namespace, endpoints, null, null, null, null);
endpointsRefer.set(replacedEndpoints);
return true;
} catch (ApiException e) {
if (e.getCode() == HttpURLConnection.HTTP_CONFLICT) {
log.debug("received {} when updating endpoints lock", e.getCode(), e);
} else {
log.error("received {} when updating endpoints lock", e.getCode(), e);
}
return false;
}
}
Aggregations