Optimize status update.
This commit is contained in:
parent
4a242c4657
commit
e822d472f9
2 changed files with 61 additions and 21 deletions
|
|
@ -193,7 +193,7 @@ public class K8sGenericStub<O extends KubernetesObject,
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the object's status.
|
* Updates the object's status. Does not retry in case of conflict.
|
||||||
*
|
*
|
||||||
* @param object the current state of the object (passed to `status`)
|
* @param object the current state of the object (passed to `status`)
|
||||||
* @param updater function that returns the new status
|
* @param updater function that returns the new status
|
||||||
|
|
@ -206,6 +206,39 @@ public class K8sGenericStub<O extends KubernetesObject,
|
||||||
return K8s.optional(api.updateStatus(object, updater));
|
return K8s.optional(api.updateStatus(object, updater));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the status of the given object. In case of conflict,
|
||||||
|
* get the current version of the object and tries again. Retries
|
||||||
|
* up to `retries` times.
|
||||||
|
*
|
||||||
|
* @param updater the function updating the status
|
||||||
|
* @param current the current state of the object, used for the first
|
||||||
|
* attempt to update
|
||||||
|
* @param retries the retries in case of conflict
|
||||||
|
* @return the updated model or empty if the object was not found
|
||||||
|
* @throws ApiException the api exception
|
||||||
|
*/
|
||||||
|
@SuppressWarnings({ "PMD.AssignmentInOperand", "PMD.UnusedAssignment" })
|
||||||
|
public Optional<O> updateStatus(Function<O, Object> updater, O current,
|
||||||
|
int retries) throws ApiException {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
if (current == null) {
|
||||||
|
current = api.get(namespace, name)
|
||||||
|
.throwsApiException().getObject();
|
||||||
|
}
|
||||||
|
return updateStatus(current, updater);
|
||||||
|
} catch (ApiException e) {
|
||||||
|
if (HttpURLConnection.HTTP_CONFLICT != e.getCode()
|
||||||
|
|| retries-- <= 0) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
// Get current version for new attempt
|
||||||
|
current = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the object and updates the status. In case of conflict, retries
|
* Gets the object and updates the status. In case of conflict, retries
|
||||||
* up to `retries` times.
|
* up to `retries` times.
|
||||||
|
|
@ -218,17 +251,23 @@ public class K8sGenericStub<O extends KubernetesObject,
|
||||||
@SuppressWarnings({ "PMD.AssignmentInOperand", "PMD.UnusedAssignment" })
|
@SuppressWarnings({ "PMD.AssignmentInOperand", "PMD.UnusedAssignment" })
|
||||||
public Optional<O> updateStatus(Function<O, Object> updater, int retries)
|
public Optional<O> updateStatus(Function<O, Object> updater, int retries)
|
||||||
throws ApiException {
|
throws ApiException {
|
||||||
while (true) {
|
return updateStatus(updater, null, retries);
|
||||||
try {
|
}
|
||||||
return updateStatus(api.get(namespace, name)
|
|
||||||
.throwsApiException().getObject(), updater);
|
/**
|
||||||
} catch (ApiException e) {
|
* Updates the status of the given object. In case of conflict,
|
||||||
if (HttpURLConnection.HTTP_CONFLICT != e.getCode()
|
* get the current version of the object and tries again. Retries
|
||||||
|| retries-- <= 0) {
|
* up to `retries` times.
|
||||||
throw e;
|
*
|
||||||
}
|
* @param updater the function updating the status
|
||||||
}
|
* @param current the current
|
||||||
}
|
* @return the kubernetes api response
|
||||||
|
* the updated model or empty if not successful
|
||||||
|
* @throws ApiException the api exception
|
||||||
|
*/
|
||||||
|
public Optional<O> updateStatus(Function<O, Object> updater, O current)
|
||||||
|
throws ApiException {
|
||||||
|
return updateStatus(updater, current, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -241,7 +280,7 @@ public class K8sGenericStub<O extends KubernetesObject,
|
||||||
*/
|
*/
|
||||||
public Optional<O> updateStatus(Function<O, Object> updater)
|
public Optional<O> updateStatus(Function<O, Object> updater)
|
||||||
throws ApiException {
|
throws ApiException {
|
||||||
return updateStatus(updater, 16);
|
return updateStatus(updater, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -153,11 +153,13 @@ public class StatusUpdater extends VmDefUpdater {
|
||||||
// by a new version of the CR. So we update only if we have
|
// by a new version of the CR. So we update only if we have
|
||||||
// a new version of the CR. There's one exception: the display
|
// a new version of the CR. There's one exception: the display
|
||||||
// password is configured by a file, not by the CR.
|
// password is configured by a file, not by the CR.
|
||||||
var vmDef = vmStub.model();
|
var vmDef = vmStub.model().orElse(null);
|
||||||
if (vmDef.isPresent()
|
if (vmDef == null) {
|
||||||
&& vmDef.get().metadata().getGeneration() == observedGeneration
|
return;
|
||||||
|
}
|
||||||
|
if (vmDef.metadata().getGeneration() == observedGeneration
|
||||||
&& (event.configuration().hasDisplayPassword
|
&& (event.configuration().hasDisplayPassword
|
||||||
|| vmDef.get().statusJson().getAsJsonPrimitive(
|
|| vmDef.statusJson().getAsJsonPrimitive(
|
||||||
"displayPasswordSerial").getAsInt() == -1)) {
|
"displayPasswordSerial").getAsInt() == -1)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -172,7 +174,7 @@ public class StatusUpdater extends VmDefUpdater {
|
||||||
.forEach(cond -> cond.addProperty("observedGeneration",
|
.forEach(cond -> cond.addProperty("observedGeneration",
|
||||||
from.getMetadata().getGeneration()));
|
from.getMetadata().getGeneration()));
|
||||||
return status;
|
return status;
|
||||||
});
|
}, vmDef);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -219,7 +221,7 @@ public class StatusUpdater extends VmDefUpdater {
|
||||||
"The VM is not running");
|
"The VM is not running");
|
||||||
}
|
}
|
||||||
return status;
|
return status;
|
||||||
});
|
}, vmDef);
|
||||||
|
|
||||||
// Maybe stop VM
|
// Maybe stop VM
|
||||||
if (event.runState() == RunState.TERMINATING && !event.failed()
|
if (event.runState() == RunState.TERMINATING && !event.failed()
|
||||||
|
|
@ -325,7 +327,6 @@ public class StatusUpdater extends VmDefUpdater {
|
||||||
}
|
}
|
||||||
var asGson = gson.toJsonTree(
|
var asGson = gson.toJsonTree(
|
||||||
objectMapper.convertValue(event.osinfo(), Object.class));
|
objectMapper.convertValue(event.osinfo(), Object.class));
|
||||||
|
|
||||||
vmStub.updateStatus(from -> {
|
vmStub.updateStatus(from -> {
|
||||||
JsonObject status = from.statusJson();
|
JsonObject status = from.statusJson();
|
||||||
status.add("osinfo", asGson);
|
status.add("osinfo", asGson);
|
||||||
|
|
@ -349,7 +350,7 @@ public class StatusUpdater extends VmDefUpdater {
|
||||||
vmStub.updateStatus(from -> {
|
vmStub.updateStatus(from -> {
|
||||||
return updateCondition(vmDef, "VmopAgentConnected",
|
return updateCondition(vmDef, "VmopAgentConnected",
|
||||||
true, "VmopAgentStarted", "The VM operator agent is running");
|
true, "VmopAgentStarted", "The VM operator agent is running");
|
||||||
});
|
}, vmDef);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue