Make result of guest shutdown configurable.

This commit is contained in:
Michael Lipp 2024-02-25 15:49:56 +01:00
parent bbe2d6efbc
commit fb4a0206f1
5 changed files with 22 additions and 1 deletions

View file

@ -999,6 +999,12 @@ spec:
type: string type: string
enum: [ "Stopped", "Running" ] enum: [ "Stopped", "Running" ]
default: "Stopped" default: "Stopped"
guestShutdownStops:
description: >-
If true, sets the state to "Stopped" when
the VM terminates due to a shutdown by the guest.
type: boolean
default: false
machineUuid: machineUuid:
description: >- description: >-
The machine's uuid. If none is specified, a uuid The machine's uuid. If none is specified, a uuid

View file

@ -63,6 +63,8 @@ data:
</#if> </#if>
</#if> </#if>
guestShutdownStops: ${ cr.spec.guestShutdownStops!false?string('true', 'false') }
# Define the VM (required) # Define the VM (required)
vm: vm:
# The VM's name (required) # The VM's name (required)

View file

@ -41,6 +41,11 @@
# config file's modification timestamp. .userData and .networkConfig # config file's modification timestamp. .userData and .networkConfig
# are optional. # are optional.
# Whether a guest initiated shutdown event patches the state
# property in the CRD.
# "guestShutdownStops":
# false
# Define the VM (required) # Define the VM (required)
"vm": "vm":
# The VM's name (required) # The VM's name (required)

View file

@ -76,6 +76,9 @@ public class Configuration implements Dto {
/** Optional cloud-init data. */ /** Optional cloud-init data. */
public CloudInit cloudInit; public CloudInit cloudInit;
/** If guest shutdown changes CRD .vm.state to "Stopped". */
public boolean guestShutdownStops;
/** The vm. */ /** The vm. */
@SuppressWarnings("PMD.ShortVariable") @SuppressWarnings("PMD.ShortVariable")
public Vm vm; public Vm vm;

View file

@ -78,6 +78,7 @@ public class StatusUpdater extends Component {
private DynamicKubernetesApi vmCrApi; private DynamicKubernetesApi vmCrApi;
private EventsV1Api evtsApi; private EventsV1Api evtsApi;
private long observedGeneration; private long observedGeneration;
private boolean guestShutdownStops;
private boolean shutdownByGuest; private boolean shutdownByGuest;
/** /**
@ -217,6 +218,9 @@ public class StatusUpdater extends Component {
@Handler @Handler
public void onRunnerConfigurationUpdate(RunnerConfigurationUpdate event) public void onRunnerConfigurationUpdate(RunnerConfigurationUpdate event)
throws ApiException { throws ApiException {
guestShutdownStops = event.configuration().guestShutdownStops;
// Remainder applies only if we have a connection to k8s.
if (vmCrApi == null) { if (vmCrApi == null) {
return; return;
} }
@ -274,7 +278,8 @@ public class StatusUpdater extends Component {
// Maybe stop VM // Maybe stop VM
if (event.state() == State.TERMINATING && !event.failed() if (event.state() == State.TERMINATING && !event.failed()
&& shutdownByGuest) { && guestShutdownStops && shutdownByGuest) {
logger.info(() -> "Stopping VM because of shutdown by guest.");
PatchOptions patchOpts = new PatchOptions(); PatchOptions patchOpts = new PatchOptions();
patchOpts.setFieldManager("kubernetes-java-kubectl-apply"); patchOpts.setFieldManager("kubernetes-java-kubectl-apply");
var res = vmCrApi.patch(namespace, vmName, var res = vmCrApi.patch(namespace, vmName,