Define some more constants.

This commit is contained in:
Michael Lipp 2025-03-01 22:51:51 +01:00
parent 41ae658e0c
commit f8cc26e657
6 changed files with 41 additions and 22 deletions

View file

@ -50,6 +50,19 @@ public class Constants {
* Status related constants. * Status related constants.
*/ */
public static class Status { public static class Status {
/** The Constant CPUS. */
public static final String CPUS = "cpus";
/** The Constant RAM. */
public static final String RAM = "ram";
/** The Constant OSINFO. */
public static final String OSINFO = "osinfo";
/** The Constant DISPLAY_PASSWORD_SERIAL. */
public static final String DISPLAY_PASSWORD_SERIAL
= "displayPasswordSerial";
/** The Constant LOGGED_IN_USER. */ /** The Constant LOGGED_IN_USER. */
public static final String LOGGED_IN_USER = "loggedInUser"; public static final String LOGGED_IN_USER = "loggedInUser";
@ -58,6 +71,9 @@ public class Constants {
/** The Constant CONSOLE_USER. */ /** The Constant CONSOLE_USER. */
public static final String CONSOLE_USER = "consoleUser"; public static final String CONSOLE_USER = "consoleUser";
/** The Constant ASSIGNMENT. */
public static final String ASSIGNMENT = "assignment";
} }
/** /**

View file

@ -220,7 +220,7 @@ public class VmDefinition extends K8sDynamicModel {
* @return the optional * @return the optional
*/ */
public Optional<String> assignedFrom() { public Optional<String> assignedFrom() {
return fromStatus("assignment", "pool"); return fromStatus(Status.ASSIGNMENT, "pool");
} }
/** /**
@ -229,7 +229,7 @@ public class VmDefinition extends K8sDynamicModel {
* @return the optional * @return the optional
*/ */
public Optional<String> assignedTo() { public Optional<String> assignedTo() {
return fromStatus("assignment", "user"); return fromStatus(Status.ASSIGNMENT, "user");
} }
/** /**
@ -238,7 +238,7 @@ public class VmDefinition extends K8sDynamicModel {
* @return the optional * @return the optional
*/ */
public Optional<Instant> assignmentLastUsed() { public Optional<Instant> assignmentLastUsed() {
return this.<String> fromStatus("assignment", "lastUsed") return this.<String> fromStatus(Status.ASSIGNMENT, "lastUsed")
.map(Instant::parse); .map(Instant::parse);
} }
@ -389,7 +389,7 @@ public class VmDefinition extends K8sDynamicModel {
* @return the optional * @return the optional
*/ */
public Optional<Long> displayPasswordSerial() { public Optional<Long> displayPasswordSerial() {
return this.<Number> fromStatus("displayPasswordSerial") return this.<Number> fromStatus(Status.DISPLAY_PASSWORD_SERIAL)
.map(Number::longValue); .map(Number::longValue);
} }

View file

@ -30,6 +30,7 @@ import java.nio.file.Path;
import java.time.Instant; import java.time.Instant;
import java.util.logging.Level; import java.util.logging.Level;
import org.jdrupes.vmoperator.common.Constants.Crd; import org.jdrupes.vmoperator.common.Constants.Crd;
import org.jdrupes.vmoperator.common.Constants.Status;
import org.jdrupes.vmoperator.common.K8sClient; import org.jdrupes.vmoperator.common.K8sClient;
import org.jdrupes.vmoperator.common.K8sDynamicStub; import org.jdrupes.vmoperator.common.K8sDynamicStub;
import org.jdrupes.vmoperator.common.VmDefinitionStub; import org.jdrupes.vmoperator.common.VmDefinitionStub;
@ -230,7 +231,7 @@ public class Controller extends Component {
vmDef.namespace(), vmDef.name()); vmDef.namespace(), vmDef.name());
if (vmStub.updateStatus(vmDef, from -> { if (vmStub.updateStatus(vmDef, from -> {
JsonObject status = from.statusJson(); JsonObject status = from.statusJson();
var assignment = GsonPtr.to(status).to("assignment"); var assignment = GsonPtr.to(status).to(Status.ASSIGNMENT);
assignment.set("pool", event.usedPool()); assignment.set("pool", event.usedPool());
assignment.set("user", event.toUser()); assignment.set("user", event.toUser());
assignment.set("lastUsed", Instant.now().toString()); assignment.set("lastUsed", Instant.now().toString());

View file

@ -29,6 +29,7 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.jdrupes.vmoperator.common.Constants.Crd; import org.jdrupes.vmoperator.common.Constants.Crd;
import org.jdrupes.vmoperator.common.Constants.Status;
import org.jdrupes.vmoperator.common.K8s; import org.jdrupes.vmoperator.common.K8s;
import org.jdrupes.vmoperator.common.K8sClient; import org.jdrupes.vmoperator.common.K8sClient;
import org.jdrupes.vmoperator.common.K8sDynamicModel; import org.jdrupes.vmoperator.common.K8sDynamicModel;
@ -187,7 +188,7 @@ public class PoolMonitor extends
vmStub.updateStatus(from -> { vmStub.updateStatus(from -> {
// TODO // TODO
JsonObject status = from.statusJson(); JsonObject status = from.statusJson();
var assignment = GsonPtr.to(status).to("assignment"); var assignment = GsonPtr.to(status).to(Status.ASSIGNMENT);
assignment.set("lastUsed", ccChange.get().toString()); assignment.set("lastUsed", ccChange.get().toString());
return status; return status;
}); });

View file

@ -160,13 +160,13 @@ public class StatusUpdater extends VmDefUpdater {
if (vmDef.metadata().getGeneration() == observedGeneration if (vmDef.metadata().getGeneration() == observedGeneration
&& (event.configuration().hasDisplayPassword && (event.configuration().hasDisplayPassword
|| vmDef.statusJson().getAsJsonPrimitive( || vmDef.statusJson().getAsJsonPrimitive(
"displayPasswordSerial").getAsInt() == -1)) { Status.DISPLAY_PASSWORD_SERIAL).getAsInt() == -1)) {
return; return;
} }
vmStub.updateStatus(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(Status.DISPLAY_PASSWORD_SERIAL, -1);
} }
status.getAsJsonArray("conditions").asList().stream() status.getAsJsonArray("conditions").asList().stream()
.map(cond -> (JsonObject) cond).filter(cond -> "Running" .map(cond -> (JsonObject) cond).filter(cond -> "Running"
@ -200,12 +200,12 @@ public class StatusUpdater extends VmDefUpdater {
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) {
status.addProperty("ram", GsonPtr.to(from.data()) status.addProperty(Status.RAM, GsonPtr.to(from.data())
.getAsString("spec", "vm", "maximumRam").orElse("0")); .getAsString("spec", "vm", "maximumRam").orElse("0"));
status.addProperty("cpus", 1); status.addProperty(Status.CPUS, 1);
} else if (event.runState() == RunState.STOPPED) { } else if (event.runState() == RunState.STOPPED) {
status.addProperty("ram", "0"); status.addProperty(Status.RAM, "0");
status.addProperty("cpus", 0); status.addProperty(Status.CPUS, 0);
status.remove(Status.LOGGED_IN_USER); status.remove(Status.LOGGED_IN_USER);
} }
@ -216,7 +216,7 @@ public class StatusUpdater extends VmDefUpdater {
"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(Status.OSINFO);
updateCondition(vmDef, "VmopAgentConnected", false, "VmStopped", updateCondition(vmDef, "VmopAgentConnected", false, "VmStopped",
"The VM is not running"); "The VM is not running");
} }
@ -258,7 +258,7 @@ public class StatusUpdater extends VmDefUpdater {
} }
vmStub.updateStatus(from -> { vmStub.updateStatus(from -> {
JsonObject status = from.statusJson(); JsonObject status = from.statusJson();
status.addProperty("ram", status.addProperty(Status.RAM,
new Quantity(new BigDecimal(event.size()), Format.BINARY_SI) new Quantity(new BigDecimal(event.size()), Format.BINARY_SI)
.toSuffixedString()); .toSuffixedString());
return status; return status;
@ -278,7 +278,7 @@ public class StatusUpdater extends VmDefUpdater {
} }
vmStub.updateStatus(from -> { vmStub.updateStatus(from -> {
JsonObject status = from.statusJson(); JsonObject status = from.statusJson();
status.addProperty("cpus", event.usedCpus().size()); status.addProperty(Status.CPUS, event.usedCpus().size());
return status; return status;
}); });
} }
@ -297,8 +297,8 @@ public class StatusUpdater extends VmDefUpdater {
} }
vmStub.updateStatus(from -> { vmStub.updateStatus(from -> {
JsonObject status = from.statusJson(); JsonObject status = from.statusJson();
status.addProperty("displayPasswordSerial", status.addProperty(Status.DISPLAY_PASSWORD_SERIAL,
status.get("displayPasswordSerial").getAsLong() + 1); status.get(Status.DISPLAY_PASSWORD_SERIAL).getAsLong() + 1);
return status; return status;
}); });
} }
@ -329,7 +329,7 @@ public class StatusUpdater extends VmDefUpdater {
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(Status.OSINFO, asGson);
return status; return status;
}); });

View file

@ -38,6 +38,7 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.Set; import java.util.Set;
import org.jdrupes.vmoperator.common.Constants.Status;
import org.jdrupes.vmoperator.common.K8sObserver; import org.jdrupes.vmoperator.common.K8sObserver;
import org.jdrupes.vmoperator.common.VmDefinition; import org.jdrupes.vmoperator.common.VmDefinition;
import org.jdrupes.vmoperator.common.VmDefinition.Permission; import org.jdrupes.vmoperator.common.VmDefinition.Permission;
@ -243,8 +244,8 @@ public class VmMgmt extends FreeMarkerConlet<VmMgmt.VmsModel> {
DataPath.<String> get(vmSpec, "currentRam").orElse("0")).getNumber() DataPath.<String> get(vmSpec, "currentRam").orElse("0")).getNumber()
.toBigInteger()); .toBigInteger());
var status = DataPath.deepCopy(vmDef.status()); var status = DataPath.deepCopy(vmDef.status());
status.put("ram", Quantity.fromString( status.put(Status.RAM, Quantity.fromString(
DataPath.<String> get(status, "ram").orElse("0")).getNumber() DataPath.<String> get(status, Status.RAM).orElse("0")).getNumber()
.toBigInteger()); .toBigInteger());
// Build result // Build result
@ -383,10 +384,10 @@ public class VmMgmt extends FreeMarkerConlet<VmMgmt.VmsModel> {
Summary summary = new Summary(); Summary summary = new Summary();
for (var vmDef : channelTracker.associated()) { for (var vmDef : channelTracker.associated()) {
summary.totalVms += 1; summary.totalVms += 1;
summary.usedCpus += vmDef.<Number> fromStatus("cpus") summary.usedCpus += vmDef.<Number> fromStatus(Status.CPUS)
.map(Number::intValue).orElse(0); .map(Number::intValue).orElse(0);
summary.usedRam = summary.usedRam summary.usedRam = summary.usedRam
.add(vmDef.<String> fromStatus("ram") .add(vmDef.<String> fromStatus(Status.RAM)
.map(r -> Quantity.fromString(r).getNumber().toBigInteger()) .map(r -> Quantity.fromString(r).getNumber().toBigInteger())
.orElse(BigInteger.ZERO)); .orElse(BigInteger.ZERO));
if (vmDef.conditionStatus("Running").orElse(false)) { if (vmDef.conditionStatus("Running").orElse(false)) {