Optimize status update.

This commit is contained in:
Michael Lipp 2025-03-01 17:44:52 +01:00
parent 4a242c4657
commit e822d472f9
2 changed files with 61 additions and 21 deletions

View file

@ -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 updater function that returns the new status
@ -206,6 +206,39 @@ public class K8sGenericStub<O extends KubernetesObject,
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
* up to `retries` times.
@ -218,17 +251,23 @@ public class K8sGenericStub<O extends KubernetesObject,
@SuppressWarnings({ "PMD.AssignmentInOperand", "PMD.UnusedAssignment" })
public Optional<O> updateStatus(Function<O, Object> updater, int retries)
throws ApiException {
while (true) {
try {
return updateStatus(api.get(namespace, name)
.throwsApiException().getObject(), updater);
} catch (ApiException e) {
if (HttpURLConnection.HTTP_CONFLICT != e.getCode()
|| retries-- <= 0) {
throw e;
}
}
}
return updateStatus(updater, null, retries);
}
/**
* 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
* @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)
throws ApiException {
return updateStatus(updater, 16);
return updateStatus(updater, null);
}
/**

View file

@ -153,11 +153,13 @@ public class StatusUpdater extends VmDefUpdater {
// 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
// password is configured by a file, not by the CR.
var vmDef = vmStub.model();
if (vmDef.isPresent()
&& vmDef.get().metadata().getGeneration() == observedGeneration
var vmDef = vmStub.model().orElse(null);
if (vmDef == null) {
return;
}
if (vmDef.metadata().getGeneration() == observedGeneration
&& (event.configuration().hasDisplayPassword
|| vmDef.get().statusJson().getAsJsonPrimitive(
|| vmDef.statusJson().getAsJsonPrimitive(
"displayPasswordSerial").getAsInt() == -1)) {
return;
}
@ -172,7 +174,7 @@ public class StatusUpdater extends VmDefUpdater {
.forEach(cond -> cond.addProperty("observedGeneration",
from.getMetadata().getGeneration()));
return status;
});
}, vmDef);
}
/**
@ -219,7 +221,7 @@ public class StatusUpdater extends VmDefUpdater {
"The VM is not running");
}
return status;
});
}, vmDef);
// Maybe stop VM
if (event.runState() == RunState.TERMINATING && !event.failed()
@ -325,7 +327,6 @@ public class StatusUpdater extends VmDefUpdater {
}
var asGson = gson.toJsonTree(
objectMapper.convertValue(event.osinfo(), Object.class));
vmStub.updateStatus(from -> {
JsonObject status = from.statusJson();
status.add("osinfo", asGson);
@ -349,7 +350,7 @@ public class StatusUpdater extends VmDefUpdater {
vmStub.updateStatus(from -> {
return updateCondition(vmDef, "VmopAgentConnected",
true, "VmopAgentStarted", "The VM operator agent is running");
});
}, vmDef);
}
/**