Skip to content

Commit

Permalink
Define some more constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnlipp committed Mar 1, 2025
1 parent 41ae658 commit f8cc26e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ public static class Crd {
* 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";

Expand All @@ -58,6 +71,9 @@ public static class Status {

/** The Constant CONSOLE_USER. */
public static final String CONSOLE_USER = "consoleUser";

/** The Constant ASSIGNMENT. */
public static final String ASSIGNMENT = "assignment";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public <T> Optional<T> fromStatus(Object... selectors) {
* @return the optional
*/
public Optional<String> assignedFrom() {
return fromStatus("assignment", "pool");
return fromStatus(Status.ASSIGNMENT, "pool");
}

/**
Expand All @@ -229,7 +229,7 @@ public Optional<String> assignedFrom() {
* @return the optional
*/
public Optional<String> assignedTo() {
return fromStatus("assignment", "user");
return fromStatus(Status.ASSIGNMENT, "user");
}

/**
Expand All @@ -238,7 +238,7 @@ public Optional<String> assignedTo() {
* @return the optional
*/
public Optional<Instant> assignmentLastUsed() {
return this.<String> fromStatus("assignment", "lastUsed")
return this.<String> fromStatus(Status.ASSIGNMENT, "lastUsed")
.map(Instant::parse);
}

Expand Down Expand Up @@ -389,7 +389,7 @@ public boolean consoleAccessible(String user, Set<Permission> permissions) {
* @return the optional
*/
public Optional<Long> displayPasswordSerial() {
return this.<Number> fromStatus("displayPasswordSerial")
return this.<Number> fromStatus(Status.DISPLAY_PASSWORD_SERIAL)
.map(Number::longValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -230,7 +231,7 @@ public void onUpdatedAssignment(UpdateAssignment event, VmChannel channel)
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -187,7 +188,7 @@ vmDef.<List<String>> fromSpec("pools")
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;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ public void onConfigureQemu(ConfigureQemu event)
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"
Expand Down Expand Up @@ -200,12 +200,12 @@ public void onRunnerStateChanged(RunnerStateChange event)
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);
}

Expand All @@ -216,7 +216,7 @@ public void onRunnerStateChanged(RunnerStateChange event)
"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");
}
Expand Down Expand Up @@ -258,7 +258,7 @@ public void onBallonChange(BalloonChangeEvent event) throws ApiException {
}
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;
Expand All @@ -278,7 +278,7 @@ public void onCpuChange(HotpluggableCpuStatus event) throws ApiException {
}
vmStub.updateStatus(from -> {
JsonObject status = from.statusJson();
status.addProperty("cpus", event.usedCpus().size());
status.addProperty(Status.CPUS, event.usedCpus().size());
return status;
});
}
Expand All @@ -297,8 +297,8 @@ public void onDisplayPasswordChanged(DisplayPasswordChanged event)
}
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;
});
}
Expand Down Expand Up @@ -329,7 +329,7 @@ public void onOsinfo(OsinfoEvent event) throws ApiException {
objectMapper.convertValue(event.osinfo(), Object.class));
vmStub.updateStatus(from -> {
JsonObject status = from.statusJson();
status.add("osinfo", asGson);
status.add(Status.OSINFO, asGson);
return status;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
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;
Expand Down Expand Up @@ -243,8 +244,8 @@ DataPath.<String> get(vmSpec, "maximumRam").orElse("0")).getNumber()
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
Expand Down Expand Up @@ -383,10 +384,10 @@ private Summary evaluateSummary(boolean force) {
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)) {
Expand Down

0 comments on commit f8cc26e

Please sign in to comment.