diff --git a/org.jdrupes.vmoperator.manager/resources/org/jdrupes/vmoperator/manager/runnerSts.ftl.yaml b/org.jdrupes.vmoperator.manager/resources/org/jdrupes/vmoperator/manager/runnerSts.ftl.yaml index 707d35e..eebf900 100644 --- a/org.jdrupes.vmoperator.manager/resources/org/jdrupes/vmoperator/manager/runnerSts.ftl.yaml +++ b/org.jdrupes.vmoperator.manager/resources/org/jdrupes/vmoperator/manager/runnerSts.ftl.yaml @@ -14,6 +14,7 @@ metadata: kind: ${ constants.VM_OP_KIND_VM } name: ${ cr.metadata.name.asString } uid: ${ cr.metadata.uid.asString } + blockOwnerDeletion: true controller: false spec: diff --git a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/CmReconciler.java b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/CmReconciler.java index 7b30213..5475ed3 100644 --- a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/CmReconciler.java +++ b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/CmReconciler.java @@ -32,7 +32,6 @@ import java.io.IOException; import java.io.StringWriter; import java.util.Map; import java.util.logging.Logger; -import org.jdrupes.vmoperator.manager.VmDefChanged.Type; import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.SafeConstructor; @@ -69,18 +68,9 @@ import org.yaml.snakeyaml.constructor.SafeConstructor; public DynamicKubernetesObject reconcile(VmDefChanged event, Map model, VmChannel channel) throws IOException, TemplateException, ApiException { - // Get API and check if exists + // Get API DynamicKubernetesApi cmApi = new DynamicKubernetesApi("", "v1", "configmaps", channel.client()); - var existing = K8s.get(cmApi, event.object().getMetadata()); - - // If deleted, delete - if (event.type() == Type.DELETED) { - if (existing.isPresent()) { - K8s.delete(cmApi, existing.get()); - } - return null; - } // Combine template and data and parse result var fmTemplate = fmConfig.getTemplate("runnerConfig.ftl.yaml"); diff --git a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/Reconciler.java b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/Reconciler.java index b23a747..e9dffa0 100644 --- a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/Reconciler.java +++ b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/Reconciler.java @@ -119,30 +119,27 @@ public class Reconciler extends Component { @SuppressWarnings("PMD.ConfusingTernary") public void onVmDefChanged(VmDefChanged event, VmChannel channel) throws ApiException, TemplateException, IOException { - // Get complete VM (CR) definition + var defMeta = event.object().getMetadata(); + + // Ownership relationships takes care of deletions + if (event.type() == Type.DELETED) { + logger.fine(() -> "VM \"" + defMeta.getName() + "\" deleted"); + return; + } + + // Get full definition and associate with channel var apiVersion = K8s.version(event.object().getApiVersion()); DynamicKubernetesApi vmCrApi = new DynamicKubernetesApi(VM_OP_GROUP, apiVersion, event.crd().getName(), channel.client()); - var defMeta = event.object().getMetadata(); - - // Update the "buffered" definition, if it still exists. - if (event.type() != Type.DELETED) { - K8s.get(vmCrApi, defMeta).ifPresent(def -> channel - .setVmDefinition(patchCr(def.getRaw().deepCopy()))); - } + K8s.get(vmCrApi, defMeta).ifPresent(def -> channel + .setVmDefinition(patchCr(def.getRaw().deepCopy()))); // Reconcile Map model = prepareModel(channel.vmDefinition()); - if (event.type() != Type.DELETED) { - var configMap = cmReconciler.reconcile(event, model, channel); - model.put("cm", configMap.getRaw()); - stsReconciler.reconcile(event, model, channel); - serviceReconciler.reconcile(event, model, channel); - } else { - serviceReconciler.reconcile(event, model, channel); - stsReconciler.reconcile(event, model, channel); - cmReconciler.reconcile(event, model, channel); - } + var configMap = cmReconciler.reconcile(event, model, channel); + model.put("cm", configMap.getRaw()); + stsReconciler.reconcile(event, model, channel); + serviceReconciler.reconcile(event, model, channel); } private Map prepareModel(JsonObject vmDef) diff --git a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/ServiceReconciler.java b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/ServiceReconciler.java index 0a1c6f9..061ddcc 100644 --- a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/ServiceReconciler.java +++ b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/ServiceReconciler.java @@ -27,7 +27,6 @@ import java.io.IOException; import java.io.StringWriter; import java.util.Map; import java.util.logging.Logger; -import org.jdrupes.vmoperator.manager.VmDefChanged.Type; import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.SafeConstructor; @@ -63,18 +62,9 @@ import org.yaml.snakeyaml.constructor.SafeConstructor; public void reconcile(VmDefChanged event, Map model, VmChannel channel) throws IOException, TemplateException, ApiException { - // Get API and check if exists + // Get API DynamicKubernetesApi svcApi = new DynamicKubernetesApi("", "v1", "services", channel.client()); - var existing = K8s.get(svcApi, event.object().getMetadata()); - - // If deleted, delete - if (event.type() == Type.DELETED) { - if (existing.isPresent()) { - K8s.delete(svcApi, existing.get()); - } - return; - } // Combine template and data and parse result var fmTemplate = fmConfig.getTemplate("runnerService.ftl.yaml"); diff --git a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/StsReconciler.java b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/StsReconciler.java index a2fb7f1..42fcb23 100644 --- a/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/StsReconciler.java +++ b/org.jdrupes.vmoperator.manager/src/org/jdrupes/vmoperator/manager/StsReconciler.java @@ -29,7 +29,6 @@ import java.io.IOException; import java.io.StringWriter; import java.util.Map; import java.util.logging.Logger; -import org.jdrupes.vmoperator.manager.VmDefChanged.Type; import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.SafeConstructor; @@ -69,22 +68,6 @@ import org.yaml.snakeyaml.constructor.SafeConstructor; "statefulsets", channel.client()); var metadata = event.object().getMetadata(); - // Maybe delete - if (event.type() == Type.DELETED) { - // First set replicas to 0 ... - PatchOptions opts = new PatchOptions(); - opts.setFieldManager("kubernetes-java-kubectl-apply"); - stsApi.patch(metadata.getNamespace(), metadata.getName(), - V1Patch.PATCH_FORMAT_JSON_PATCH, - new V1Patch("[{\"op\": \"replace\", \"path\": " - + "\"/spec/replicas\", \"value\": 0}]"), - opts).throwsApiException(); - // ... then delete - stsApi.delete(metadata.getNamespace(), metadata.getName()) - .throwsApiException(); - return; - } - // Combine template and data and parse result var fmTemplate = fmConfig.getTemplate("runnerSts.ftl.yaml"); StringWriter out = new StringWriter();