Move api client to base class.
This commit is contained in:
parent
f1d973502d
commit
811164f7b9
3 changed files with 23 additions and 22 deletions
|
|
@ -44,7 +44,6 @@ import org.jgrapes.core.events.Start;
|
|||
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
|
||||
public class ConsoleTracker extends VmDefUpdater {
|
||||
|
||||
private final K8sClient apiClient;
|
||||
private VmDefinitionStub vmStub;
|
||||
private String mainChannelClientHost;
|
||||
private long mainChannelClientPort;
|
||||
|
|
@ -109,7 +108,7 @@ public class ConsoleTracker extends VmDefUpdater {
|
|||
vmStub.updateStatus(from -> {
|
||||
JsonObject status = from.status();
|
||||
status.addProperty("consoleClient", event.clientHost());
|
||||
updateCondition(apiClient, from, status, "ConsoleConnected",
|
||||
updateCondition(from, status, "ConsoleConnected",
|
||||
true, "Connection from " + event.clientHost(), null);
|
||||
return status;
|
||||
});
|
||||
|
|
@ -144,7 +143,7 @@ public class ConsoleTracker extends VmDefUpdater {
|
|||
vmStub.updateStatus(from -> {
|
||||
JsonObject status = from.status();
|
||||
status.addProperty("consoleClient", "");
|
||||
updateCondition(apiClient, from, status, "ConsoleConnected",
|
||||
updateCondition(from, status, "ConsoleConnected",
|
||||
false, event.clientHost() + " has disconnected", null);
|
||||
return status;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -33,7 +33,6 @@ 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 org.jdrupes.vmoperator.common.K8s;
|
||||
import org.jdrupes.vmoperator.common.K8sClient;
|
||||
import org.jdrupes.vmoperator.common.VmDefinitionModel;
|
||||
import org.jdrupes.vmoperator.common.VmDefinitionStub;
|
||||
import org.jdrupes.vmoperator.runner.qemu.events.BalloonChangeEvent;
|
||||
|
|
@ -59,7 +58,6 @@ public class StatusUpdater extends VmDefUpdater {
|
|||
private static final Set<RunState> RUNNING_STATES
|
||||
= Set.of(RunState.RUNNING, RunState.TERMINATING);
|
||||
|
||||
private K8sClient apiClient;
|
||||
private long observedGeneration;
|
||||
private boolean guestShutdownStops;
|
||||
private boolean shutdownByGuest;
|
||||
|
|
@ -73,15 +71,6 @@ public class StatusUpdater extends VmDefUpdater {
|
|||
@SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
|
||||
public StatusUpdater(Channel componentChannel) {
|
||||
super(componentChannel);
|
||||
try {
|
||||
apiClient = new K8sClient();
|
||||
io.kubernetes.client.openapi.Configuration
|
||||
.setDefaultApiClient(apiClient);
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, e,
|
||||
() -> "Cannot access events API, terminating.");
|
||||
fire(new Exit(1));
|
||||
}
|
||||
attach(new ConsoleTracker(componentChannel));
|
||||
}
|
||||
|
||||
|
|
@ -187,8 +176,8 @@ public class StatusUpdater extends VmDefUpdater {
|
|||
vmStub.updateStatus(vmDef, from -> {
|
||||
JsonObject status = from.status();
|
||||
boolean running = RUNNING_STATES.contains(event.runState());
|
||||
updateCondition(apiClient, vmDef, vmDef.status(), "Running",
|
||||
running, event.reason(), event.message());
|
||||
updateCondition(vmDef, vmDef.status(), "Running", running,
|
||||
event.reason(), event.message());
|
||||
if (event.runState() == RunState.STARTING) {
|
||||
status.addProperty("ram", GsonPtr.to(from.data())
|
||||
.getAsString("spec", "vm", "maximumRam").orElse("0"));
|
||||
|
|
@ -201,8 +190,8 @@ public class StatusUpdater extends VmDefUpdater {
|
|||
// In case console connection was still present
|
||||
if (!running) {
|
||||
status.addProperty("consoleClient", "");
|
||||
updateCondition(apiClient, from, status, "ConsoleConnected",
|
||||
false, "VM has stopped", null);
|
||||
updateCondition(from, status, "ConsoleConnected", false,
|
||||
"VM has stopped", null);
|
||||
}
|
||||
return status;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@
|
|||
package org.jdrupes.vmoperator.runner.qemu;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import io.kubernetes.client.openapi.ApiClient;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
|
@ -31,7 +30,9 @@ import java.util.Map;
|
|||
import java.util.Optional;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
import org.jdrupes.vmoperator.common.K8sClient;
|
||||
import org.jdrupes.vmoperator.common.VmDefinitionModel;
|
||||
import org.jdrupes.vmoperator.runner.qemu.events.Exit;
|
||||
import org.jgrapes.core.Channel;
|
||||
import org.jgrapes.core.Component;
|
||||
import org.jgrapes.core.annotation.Handler;
|
||||
|
|
@ -46,15 +47,28 @@ public class VmDefUpdater extends Component {
|
|||
|
||||
protected String namespace;
|
||||
protected String vmName;
|
||||
protected K8sClient apiClient;
|
||||
|
||||
/**
|
||||
* Instantiates a new status updater.
|
||||
*
|
||||
* @param componentChannel the component channel
|
||||
* @throws IOException
|
||||
*/
|
||||
@SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
|
||||
public VmDefUpdater(Channel componentChannel) {
|
||||
super(componentChannel);
|
||||
if (apiClient == null) {
|
||||
try {
|
||||
apiClient = new K8sClient();
|
||||
io.kubernetes.client.openapi.Configuration
|
||||
.setDefaultApiClient(apiClient);
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, e,
|
||||
() -> "Cannot access events API, terminating.");
|
||||
fire(new Exit(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -104,9 +118,8 @@ public class VmDefUpdater extends Component {
|
|||
* @param state the new state
|
||||
* @param reason the reason for the change
|
||||
*/
|
||||
protected void updateCondition(ApiClient apiClient, VmDefinitionModel from,
|
||||
JsonObject status, String type, boolean state, String reason,
|
||||
String message) {
|
||||
protected void updateCondition(VmDefinitionModel from, JsonObject status,
|
||||
String type, boolean state, String reason, String message) {
|
||||
// Optimize, as we can get this several times
|
||||
var current = status.getAsJsonArray("conditions").asList().stream()
|
||||
.map(cond -> (JsonObject) cond)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue