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.
*/
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. */
public static final String LOGGED_IN_USER = "loggedInUser";
@ -58,6 +71,9 @@ public class Constants {
/** The Constant CONSOLE_USER. */
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
*/
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
*/
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
*/
public Optional<Instant> assignmentLastUsed() {
return this.<String> fromStatus("assignment", "lastUsed")
return this.<String> fromStatus(Status.ASSIGNMENT, "lastUsed")
.map(Instant::parse);
}
@ -389,7 +389,7 @@ public class VmDefinition extends K8sDynamicModel {
* @return the optional
*/
public Optional<Long> displayPasswordSerial() {
return this.<Number> fromStatus("displayPasswordSerial")
return this.<Number> fromStatus(Status.DISPLAY_PASSWORD_SERIAL)
.map(Number::longValue);
}

View file

@ -30,6 +30,7 @@ import java.nio.file.Path;
import java.time.Instant;
import java.util.logging.Level;
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.K8sDynamicStub;
import org.jdrupes.vmoperator.common.VmDefinitionStub;
@ -230,7 +231,7 @@ public class Controller extends Component {
vmDef.namespace(), vmDef.name());
if (vmStub.updateStatus(vmDef, from -> {
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("user", event.toUser());
assignment.set("lastUsed", Instant.now().toString());

View file

@ -29,6 +29,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
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.K8sClient;
import org.jdrupes.vmoperator.common.K8sDynamicModel;
@ -187,7 +188,7 @@ public class PoolMonitor extends
vmStub.updateStatus(from -> {
// TODO
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());
return status;
});

View file

@ -160,13 +160,13 @@ public class StatusUpdater extends VmDefUpdater {
if (vmDef.metadata().getGeneration() == observedGeneration
&& (event.configuration().hasDisplayPassword
|| vmDef.statusJson().getAsJsonPrimitive(
"displayPasswordSerial").getAsInt() == -1)) {
Status.DISPLAY_PASSWORD_SERIAL).getAsInt() == -1)) {
return;
}
vmStub.updateStatus(from -> {
JsonObject status = from.statusJson();
if (!event.configuration().hasDisplayPassword) {
status.addProperty("displayPasswordSerial", -1);
status.addProperty(Status.DISPLAY_PASSWORD_SERIAL, -1);
}
status.getAsJsonArray("conditions").asList().stream()
.map(cond -> (JsonObject) cond).filter(cond -> "Running"
@ -200,12 +200,12 @@ public class StatusUpdater extends VmDefUpdater {
event.runState() == RunState.BOOTED, event.reason(),
event.message());
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"));
status.addProperty("cpus", 1);
status.addProperty(Status.CPUS, 1);
} else if (event.runState() == RunState.STOPPED) {
status.addProperty("ram", "0");
status.addProperty("cpus", 0);
status.addProperty(Status.RAM, "0");
status.addProperty(Status.CPUS, 0);
status.remove(Status.LOGGED_IN_USER);
}
@ -216,7 +216,7 @@ public class StatusUpdater extends VmDefUpdater {
"The VM is not running");
// In case we had an irregular shutdown
status.remove("osinfo");
status.remove(Status.OSINFO);
updateCondition(vmDef, "VmopAgentConnected", false, "VmStopped",
"The VM is not running");
}
@ -258,7 +258,7 @@ public class StatusUpdater extends VmDefUpdater {
}
vmStub.updateStatus(from -> {
JsonObject status = from.statusJson();
status.addProperty("ram",
status.addProperty(Status.RAM,
new Quantity(new BigDecimal(event.size()), Format.BINARY_SI)
.toSuffixedString());
return status;
@ -278,7 +278,7 @@ public class StatusUpdater extends VmDefUpdater {
}
vmStub.updateStatus(from -> {
JsonObject status = from.statusJson();
status.addProperty("cpus", event.usedCpus().size());
status.addProperty(Status.CPUS, event.usedCpus().size());
return status;
});
}
@ -297,8 +297,8 @@ public class StatusUpdater extends VmDefUpdater {
}
vmStub.updateStatus(from -> {
JsonObject status = from.statusJson();
status.addProperty("displayPasswordSerial",
status.get("displayPasswordSerial").getAsLong() + 1);
status.addProperty(Status.DISPLAY_PASSWORD_SERIAL,
status.get(Status.DISPLAY_PASSWORD_SERIAL).getAsLong() + 1);
return status;
});
}
@ -329,7 +329,7 @@ public class StatusUpdater extends VmDefUpdater {
objectMapper.convertValue(event.osinfo(), Object.class));
vmStub.updateStatus(from -> {
JsonObject status = from.statusJson();
status.add("osinfo", asGson);
status.add(Status.OSINFO, asGson);
return status;
});

View file

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