Fix and clarify usage of methods for status update.

This commit is contained in:
Michael Lipp 2025-03-01 17:00:14 +01:00
parent 5e282c4d2b
commit 62a7210117
4 changed files with 43 additions and 41 deletions

View file

@ -196,51 +196,52 @@ public class K8sGenericStub<O extends KubernetesObject,
* Updates the object's status. * Updates the object's status.
* *
* @param object the current state of the object (passed to `status`) * @param object the current state of the object (passed to `status`)
* @param status function that returns the new status * @param updater function that returns the new status
* @return the updated model or empty if the object was not found * @return the updated model or empty if the object was not found
* @throws ApiException the api exception * @throws ApiException the api exception
*/ */
@SuppressWarnings("PMD.AssignmentInOperand") @SuppressWarnings("PMD.AssignmentInOperand")
public Optional<O> updateStatus(O object, Function<O, Object> status) public Optional<O> updateStatus(O object, Function<O, Object> updater)
throws ApiException { throws ApiException {
return K8s.optional(api.updateStatus(object, status)); return K8s.optional(api.updateStatus(object, updater));
} }
/** /**
* 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.
* *
* @param status the status * @param updater the function updating the status
* @param retries the retries in case of conflict * @param retries the retries in case of conflict
* @return the updated model or empty if the object was not found * @return the updated model or empty if the object was not found
* @throws ApiException the api exception * @throws ApiException the api exception
*/ */
@SuppressWarnings({ "PMD.AssignmentInOperand", "PMD.UnusedAssignment" }) @SuppressWarnings({ "PMD.AssignmentInOperand", "PMD.UnusedAssignment" })
public Optional<O> updateStatus(Function<O, Object> status, int retries) public Optional<O> updateStatus(Function<O, Object> updater, int retries)
throws ApiException { throws ApiException {
while (true) {
try { try {
return updateStatus(api.get(namespace, name).throwsApiException() return updateStatus(api.get(namespace, name)
.getObject(), status); .throwsApiException().getObject(), updater);
} catch (ApiException e) { } catch (ApiException e) {
if (HttpURLConnection.HTTP_CONFLICT != e.getCode() if (HttpURLConnection.HTTP_CONFLICT != e.getCode()
|| retries-- <= 0) { || retries-- <= 0) {
throw e; throw e;
} }
} }
return Optional.empty(); }
} }
/** /**
* Updates the status. * Updates the status. In case of conflict, retries up to 16 times.
* *
* @param status the status * @param updater the function updating the status
* @return the kubernetes api response * @return the kubernetes api response
* the updated model or empty if not successful * the updated model or empty if not successful
* @throws ApiException the api exception * @throws ApiException the api exception
*/ */
public Optional<O> updateStatus(Function<O, Object> status) public Optional<O> updateStatus(Function<O, Object> updater)
throws ApiException { throws ApiException {
return updateStatus(status, 16); return updateStatus(updater, 16);
} }
/** /**

View file

@ -106,10 +106,9 @@ public class ConsoleTracker extends VmDefUpdater {
mainChannelClientHost = event.clientHost(); mainChannelClientHost = event.clientHost();
mainChannelClientPort = event.clientPort(); mainChannelClientPort = event.clientPort();
vmStub.updateStatus(from -> { vmStub.updateStatus(from -> {
JsonObject status = from.statusJson(); JsonObject status = updateCondition(from, "ConsoleConnected", true,
"Connected", "Connection from " + event.clientHost());
status.addProperty("consoleClient", event.clientHost()); status.addProperty("consoleClient", event.clientHost());
updateCondition(from, status, "ConsoleConnected", true, "Connected",
"Connection from " + event.clientHost());
return status; return status;
}); });
@ -141,10 +140,9 @@ public class ConsoleTracker extends VmDefUpdater {
return; return;
} }
vmStub.updateStatus(from -> { vmStub.updateStatus(from -> {
JsonObject status = from.statusJson(); JsonObject status = updateCondition(from, "ConsoleConnected", false,
status.addProperty("consoleClient", "");
updateCondition(from, status, "ConsoleConnected", false,
"Disconnected", event.clientHost() + " has disconnected"); "Disconnected", event.clientHost() + " has disconnected");
status.addProperty("consoleClient", "");
return status; return status;
}); });

View file

@ -154,7 +154,7 @@ public class StatusUpdater extends VmDefUpdater {
"displayPasswordSerial").getAsInt() == -1)) { "displayPasswordSerial").getAsInt() == -1)) {
return; return;
} }
vmStub.updateStatus(vmDef.get(), from -> { vmStub.updateStatus(from -> {
JsonObject status = from.statusJson(); JsonObject status = from.statusJson();
if (!event.configuration().hasDisplayPassword) { if (!event.configuration().hasDisplayPassword) {
status.addProperty("displayPasswordSerial", -1); status.addProperty("displayPasswordSerial", -1);
@ -183,12 +183,11 @@ public class StatusUpdater extends VmDefUpdater {
if (vmStub == null || (vmDef = vmStub.model().orElse(null)) == null) { if (vmStub == null || (vmDef = vmStub.model().orElse(null)) == null) {
return; return;
} }
vmStub.updateStatus(vmDef, from -> { vmStub.updateStatus(from -> {
JsonObject status = from.statusJson();
boolean running = event.runState().vmRunning(); boolean running = event.runState().vmRunning();
updateCondition(vmDef, vmDef.statusJson(), "Running", running, updateCondition(vmDef, "Running", running, event.reason(),
event.reason(), event.message()); event.message());
updateCondition(vmDef, vmDef.statusJson(), "Booted", JsonObject status = updateCondition(vmDef, "Booted",
event.runState() == RunState.BOOTED, event.reason(), event.runState() == RunState.BOOTED, event.reason(),
event.message()); event.message());
if (event.runState() == RunState.STARTING) { if (event.runState() == RunState.STARTING) {
@ -203,13 +202,13 @@ public class StatusUpdater extends VmDefUpdater {
if (!running) { if (!running) {
// In case console connection was still present // In case console connection was still present
status.addProperty("consoleClient", ""); status.addProperty("consoleClient", "");
updateCondition(from, status, "ConsoleConnected", false, updateCondition(from, "ConsoleConnected", false, "VmStopped",
"VmStopped", "The VM is not running"); "The VM is not running");
// In case we had an irregular shutdown // In case we had an irregular shutdown
status.remove("osinfo"); status.remove("osinfo");
updateCondition(vmDef, vmDef.statusJson(), "VmopAgentConnected", updateCondition(vmDef, "VmopAgentConnected", false, "VmStopped",
false, "VmStopped", "The VM is not running"); "The VM is not running");
} }
return status; return status;
}); });
@ -340,10 +339,8 @@ public class StatusUpdater extends VmDefUpdater {
return; return;
} }
vmStub.updateStatus(from -> { vmStub.updateStatus(from -> {
JsonObject status = from.statusJson(); return updateCondition(vmDef, "VmopAgentConnected",
updateCondition(vmDef, status, "VmopAgentConnected",
true, "VmopAgentStarted", "The VM operator agent is running"); true, "VmopAgentStarted", "The VM operator agent is running");
return status;
}); });
} }

View file

@ -31,6 +31,7 @@ import java.util.Optional;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.jdrupes.vmoperator.common.K8sClient; import org.jdrupes.vmoperator.common.K8sClient;
import org.jdrupes.vmoperator.common.K8sGenericStub;
import org.jdrupes.vmoperator.common.VmDefinition; import org.jdrupes.vmoperator.common.VmDefinition;
import org.jdrupes.vmoperator.runner.qemu.events.Exit; import org.jdrupes.vmoperator.runner.qemu.events.Exit;
import org.jgrapes.core.Channel; import org.jgrapes.core.Channel;
@ -109,17 +110,21 @@ public class VmDefUpdater extends Component {
} }
/** /**
* Update condition. * Update condition. The `from` VM definition is used to determine the
* observed generation and the current status. This method is intended
* to be called in the function passed to
* {@link K8sGenericStub#updateStatus}.
* *
* @param from the VM definition * @param from the VM definition
* @param status the current status
* @param type the condition type * @param type the condition type
* @param state the new state * @param state the new state
* @param reason the reason for the change * @param reason the reason for the change
* @param message the message * @param message the message
* @return the updated status
*/ */
protected void updateCondition(VmDefinition from, JsonObject status, protected JsonObject updateCondition(VmDefinition from, String type,
String type, boolean state, String reason, String message) { boolean state, String reason, String message) {
JsonObject status = from.statusJson();
// Optimize, as we can get this several times // Optimize, as we can get this several times
var current = status.getAsJsonArray("conditions").asList().stream() var current = status.getAsJsonArray("conditions").asList().stream()
.map(cond -> (JsonObject) cond) .map(cond -> (JsonObject) cond)
@ -127,7 +132,7 @@ public class VmDefUpdater extends Component {
.findFirst() .findFirst()
.map(cond -> "True".equals(cond.get("status").getAsString())); .map(cond -> "True".equals(cond.get("status").getAsString()));
if (current.isPresent() && current.get() == state) { if (current.isPresent() && current.get() == state) {
return; return status;
} }
// Do update // Do update
@ -150,5 +155,6 @@ public class VmDefUpdater extends Component {
newConds.addAll(toReplace); newConds.addAll(toReplace);
status.add("conditions", status.add("conditions",
apiClient.getJSON().getGson().toJsonTree(newConds)); apiClient.getJSON().getGson().toJsonTree(newConds));
return status;
} }
} }