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
enum: [ "Stopped", "Running" ]
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:
description: >-
The machine's uuid. If none is specified, a uuid

View file

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

View file

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

View file

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

View file

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