Skip to content

Commit

Permalink
Add used by information.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnlipp committed Nov 14, 2024
1 parent 4ea568e commit 0ba8d92
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
6 changes: 6 additions & 0 deletions deploy/crds/vms-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@
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;
}

/**
Expand All @@ -48,6 +51,15 @@ public VmDefinition vmDefinition() {
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,10 +39,13 @@
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;
Expand Down Expand Up @@ -181,12 +186,22 @@ public void onGetDisplaySecrets(GetDisplayPassword event, VmChannel channel)
+ "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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ nodeName = Node
requestedCpus = Requested CPUs
requestedRam = Requested RAM
running = Running
usedBy = Used by
usedFrom = Used from
vmActions = Actions
vmname = Name
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand 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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,12 @@ protected void doUpdateConletState(NotifyConletModel event,
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;
Expand Down

0 comments on commit 0ba8d92

Please sign in to comment.