This commit is contained in:
Michael Lipp 2024-10-26 20:47:00 +02:00
parent 1bc63abadf
commit 80fe921e6e

View file

@ -18,6 +18,7 @@
package org.jdrupes.vmoperator.vmconlet; package org.jdrupes.vmoperator.vmconlet;
import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import freemarker.core.ParseException; import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException; import freemarker.template.MalformedTemplateNameException;
@ -33,8 +34,6 @@ import java.time.Instant;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import org.jdrupes.json.JsonBeanDecoder;
import org.jdrupes.json.JsonDecodeException;
import org.jdrupes.vmoperator.common.K8sObserver; import org.jdrupes.vmoperator.common.K8sObserver;
import org.jdrupes.vmoperator.common.VmDefinitionModel; import org.jdrupes.vmoperator.common.VmDefinitionModel;
import org.jdrupes.vmoperator.manager.events.ChannelTracker; import org.jdrupes.vmoperator.manager.events.ChannelTracker;
@ -160,10 +159,9 @@ public class VmConlet extends FreeMarkerConlet<VmConlet.VmsModel> {
sendVmInfos = true; sendVmInfos = true;
} }
if (sendVmInfos) { if (sendVmInfos) {
for (var vmDef : channelTracker.associated()) { for (var item : channelTracker.values()) {
var def Gson gson = item.channel().client().getJSON().getGson();
= JsonBeanDecoder.create(vmDef.data().toString()) var def = gson.fromJson(item.associated().data(), Object.class);
.readObject();
channel.respond(new NotifyConletView(type(), channel.respond(new NotifyConletView(type(),
conletId, "updateVm", def)); conletId, "updateVm", def));
} }
@ -185,7 +183,7 @@ public class VmConlet extends FreeMarkerConlet<VmConlet.VmsModel> {
"PMD.AvoidInstantiatingObjectsInLoops", "PMD.AvoidDuplicateLiterals", "PMD.AvoidInstantiatingObjectsInLoops", "PMD.AvoidDuplicateLiterals",
"PMD.ConfusingArgumentToVarargsMethod" }) "PMD.ConfusingArgumentToVarargsMethod" })
public void onVmDefChanged(VmDefChanged event, VmChannel channel) public void onVmDefChanged(VmDefChanged event, VmChannel channel)
throws JsonDecodeException, IOException { throws IOException {
var vmName = event.vmDefinition().getMetadata().getName(); var vmName = event.vmDefinition().getMetadata().getName();
if (event.type() == K8sObserver.ResponseType.DELETED) { if (event.type() == K8sObserver.ResponseType.DELETED) {
channelTracker.remove(vmName); channelTracker.remove(vmName);
@ -196,11 +194,11 @@ public class VmConlet extends FreeMarkerConlet<VmConlet.VmsModel> {
} }
} }
} else { } else {
var vmDef = new VmDefinitionModel(channel.client().getJSON() var gson = channel.client().getJSON().getGson();
.getGson(), cleanup(event.vmDefinition().data())); var vmDef = new VmDefinitionModel(gson,
cleanup(event.vmDefinition().data()));
channelTracker.put(vmName, channel, vmDef); channelTracker.put(vmName, channel, vmDef);
var def = JsonBeanDecoder.create(vmDef.data().toString()) var def = gson.fromJson(vmDef.data(), Object.class);
.readObject();
for (var entry : conletIdsByConsoleConnection().entrySet()) { for (var entry : conletIdsByConsoleConnection().entrySet()) {
for (String conletId : entry.getValue()) { for (String conletId : entry.getValue()) {
entry.getKey().respond(new NotifyConletView(type(), entry.getKey().respond(new NotifyConletView(type(),
@ -346,7 +344,7 @@ public class VmConlet extends FreeMarkerConlet<VmConlet.VmsModel> {
ConsoleConnection channel, VmsModel conletState) ConsoleConnection channel, VmsModel conletState)
throws Exception { throws Exception {
event.stop(); event.stop();
var vmName = event.params().asString(0); String vmName = event.param(0);
var vmChannel = channelTracker.channel(vmName).orElse(null); var vmChannel = channelTracker.channel(vmName).orElse(null);
if (vmChannel == null) { if (vmChannel == null) {
return; return;
@ -360,12 +358,12 @@ public class VmConlet extends FreeMarkerConlet<VmConlet.VmsModel> {
break; break;
case "cpus": case "cpus":
fire(new ModifyVm(vmName, "currentCpus", fire(new ModifyVm(vmName, "currentCpus",
new BigDecimal(event.params().asDouble(1)).toBigInteger(), new BigDecimal(event.param(1).toString()).toBigInteger(),
vmChannel)); vmChannel));
break; break;
case "ram": case "ram":
fire(new ModifyVm(vmName, "currentRam", fire(new ModifyVm(vmName, "currentRam",
new Quantity(new BigDecimal(event.params().asDouble(1)), new Quantity(new BigDecimal(event.param(1).toString()),
Format.BINARY_SI).toSuffixedString(), Format.BINARY_SI).toSuffixedString(),
vmChannel)); vmChannel));
break; break;