Deletions happen automatically due to owner references.
This commit is contained in:
parent
cf1432b973
commit
477db06f8d
5 changed files with 18 additions and 57 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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<String, Object> 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");
|
||||
|
|
|
|||
|
|
@ -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())));
|
||||
}
|
||||
|
||||
// Reconcile
|
||||
Map<String, Object> 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);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Object> prepareModel(JsonObject vmDef)
|
||||
|
|
|
|||
|
|
@ -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<String, Object> 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");
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue