Add used by information.

This commit is contained in:
Michael Lipp 2024-11-14 18:46:19 +01:00
parent 4ea568ea17
commit 0ba8d922ef
7 changed files with 47 additions and 7 deletions

View file

@ -1467,6 +1467,12 @@ spec:
The hostname of the currently connected client.
type: string
default: ""
consoleUser:
description: >-
The id of the user who has last requested a console
connection.
type: string
default: ""
displayPasswordSerial:
description: >-
Counts changes of the display password. Set to -1

View file

@ -29,14 +29,17 @@ import org.jgrapes.core.Event;
public class GetDisplayPassword extends Event<String> {
private final VmDefinition vmDef;
private final String user;
/**
* Instantiates a new returns the display secret.
* Instantiates a new request for the display secret.
*
* @param vmDef the vm name
* @param user the requesting user
*/
public GetDisplayPassword(VmDefinition vmDef) {
public GetDisplayPassword(VmDefinition vmDef, String user) {
this.vmDef = vmDef;
this.user = user;
}
/**
@ -48,6 +51,15 @@ public class GetDisplayPassword extends Event<String> {
return vmDef;
}
/**
* Return the id of the user who has requested the password.
*
* @return the string
*/
public String user() {
return user;
}
/**
* Return the password. May only be called when the event is completed.
*

View file

@ -18,6 +18,8 @@
package org.jdrupes.vmoperator.manager;
import com.google.gson.JsonObject;
import io.kubernetes.client.apimachinery.GroupVersionKind;
import io.kubernetes.client.custom.V1Patch;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1Secret;
@ -37,10 +39,13 @@ import java.util.Optional;
import java.util.Scanner;
import java.util.logging.Level;
import static org.jdrupes.vmoperator.common.Constants.APP_NAME;
import static org.jdrupes.vmoperator.common.Constants.VM_OP_GROUP;
import static org.jdrupes.vmoperator.common.Constants.VM_OP_KIND_VM;
import static org.jdrupes.vmoperator.common.Constants.VM_OP_NAME;
import org.jdrupes.vmoperator.common.K8sClient;
import org.jdrupes.vmoperator.common.K8sV1PodStub;
import org.jdrupes.vmoperator.common.K8sV1SecretStub;
import org.jdrupes.vmoperator.common.VmDefinitionStub;
import static org.jdrupes.vmoperator.manager.Constants.COMP_DISPLAY_SECRET;
import static org.jdrupes.vmoperator.manager.Constants.DATA_DISPLAY_PASSWORD;
import static org.jdrupes.vmoperator.manager.Constants.DATA_PASSWORD_EXPIRY;
@ -181,12 +186,22 @@ public class DisplaySecretMonitor
+ "app.kubernetes.io/instance="
+ event.vmDefinition().metadata().getName());
var stubs = K8sV1SecretStub.list(client(),
event.vmDefinition().metadata().getNamespace(), options);
event.vmDefinition().namespace(), options);
if (stubs.isEmpty()) {
return;
}
var stub = stubs.iterator().next();
// Valid request, update console user in status
var vmStub = VmDefinitionStub.get(client(),
new GroupVersionKind(VM_OP_GROUP, "", VM_OP_KIND_VM),
event.vmDefinition().namespace(), event.vmDefinition().name());
vmStub.updateStatus(from -> {
JsonObject status = from.status();
status.addProperty("consoleUser", event.user());
return status;
});
// Check validity
var model = stub.model().get();
@SuppressWarnings("PMD.StringInstantiation")

View file

@ -11,6 +11,7 @@ nodeName = Node
requestedCpus = Requested CPUs
requestedRam = Requested RAM
running = Running
usedBy = Used by
usedFrom = Used from
vmActions = Actions
vmname = Name

View file

@ -15,6 +15,7 @@ maximumRam = Maximales RAM
nodeName = Knoten
requestedCpus = Angeforderte CPUs
requestedRam = Angefordertes RAM
usedBy = Benutzt durch
usedFrom = Benutzt von
vmActions = Aktionen
vmname = Name

View file

@ -112,7 +112,8 @@ window.orgJDrupesVmOperatorVmConlet.initView = (viewDom: HTMLElement,
["currentCpus", "currentCpus"],
["currentRam", "currentRam"],
["nodeName", "nodeName"],
["usedFrom", "usedFrom"]
["usedFrom", "usedFrom"],
["usedBy", "usedBy"]
], {
sortKey: "name",
sortOrder: "up"
@ -181,6 +182,7 @@ JGConsole.registerConletFunction("org.jdrupes.vmoperator.vmconlet.VmConlet",
vmDefinition.currentCpus = vmDefinition.status.cpus;
vmDefinition.currentRam = Number(vmDefinition.status.ram);
vmDefinition.usedFrom = vmDefinition.status.consoleClient || "";
vmDefinition.usedBy = vmDefinition.status.consoleUser || "";
for (const condition of vmDefinition.status.conditions) {
if (condition.type === "Running") {
vmDefinition.running = condition.status === "True";

View file

@ -527,9 +527,12 @@ public class VmViewer extends FreeMarkerConlet<VmViewer.ViewerModel> {
break;
case "openConsole":
if (perms.contains(Permission.ACCESS_CONSOLE)) {
var pwQuery = Event.onCompletion(new GetDisplayPassword(vmDef),
e -> openConsole(vmName, channel, model,
e.password().orElse(null)));
var user = WebConsoleUtils.userFromSession(channel.session())
.map(ConsoleUser::getName).orElse("");
var pwQuery
= Event.onCompletion(new GetDisplayPassword(vmDef, user),
e -> openConsole(vmName, channel, model,
e.password().orElse(null)));
fire(pwQuery, vmChannel);
}
break;