Force configuration update.
This commit is contained in:
parent
6cdb27ff4e
commit
bf2c72891c
1 changed files with 49 additions and 2 deletions
|
|
@ -20,13 +20,18 @@ package org.jdrupes.vmoperator.manager;
|
|||
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.TemplateException;
|
||||
import io.kubernetes.client.custom.V1Patch;
|
||||
import io.kubernetes.client.openapi.ApiClient;
|
||||
import io.kubernetes.client.openapi.ApiException;
|
||||
import io.kubernetes.client.util.generic.dynamic.DynamicKubernetesApi;
|
||||
import io.kubernetes.client.util.generic.dynamic.DynamicKubernetesObject;
|
||||
import io.kubernetes.client.util.generic.dynamic.Dynamics;
|
||||
import io.kubernetes.client.util.generic.options.ListOptions;
|
||||
import io.kubernetes.client.util.generic.options.PatchOptions;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
import org.jdrupes.vmoperator.manager.VmDefChanged.Type;
|
||||
|
||||
/**
|
||||
|
|
@ -35,6 +40,7 @@ import org.jdrupes.vmoperator.manager.VmDefChanged.Type;
|
|||
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
|
||||
/* default */ class CmReconciler {
|
||||
|
||||
protected final Logger logger = Logger.getLogger(getClass().getName());
|
||||
private final Configuration fmConfig;
|
||||
|
||||
/**
|
||||
|
|
@ -81,8 +87,49 @@ import org.jdrupes.vmoperator.manager.VmDefChanged.Type;
|
|||
// https://github.com/kubernetes-client/java/issues/2741
|
||||
var mapDef = Dynamics.newFromYaml(out.toString());
|
||||
|
||||
// Apply
|
||||
return K8s.apply(cmApi, mapDef, out.toString());
|
||||
// Apply and maybe force pod update
|
||||
var newState = K8s.apply(cmApi, mapDef, out.toString());
|
||||
maybeForceUpdate(channel.client(), newState);
|
||||
return newState;
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers update of config map mounted in pod
|
||||
* See https://ahmet.im/blog/kubernetes-secret-volumes-delay/
|
||||
* @param client
|
||||
*
|
||||
* @param newCm
|
||||
*/
|
||||
private void maybeForceUpdate(ApiClient client,
|
||||
DynamicKubernetesObject newCm) {
|
||||
ListOptions listOpts = new ListOptions();
|
||||
listOpts.setLabelSelector(
|
||||
"app.kubernetes.io/managed-by=" + Constants.VM_OP_NAME + ","
|
||||
+ "app.kubernetes.io/name=" + Constants.APP_NAME);
|
||||
// Get pod, selected by label
|
||||
var podApi = new DynamicKubernetesApi("", "v1", "pods", client);
|
||||
var pods = podApi
|
||||
.list(newCm.getMetadata().getNamespace(), listOpts).getObject();
|
||||
if (pods == null) {
|
||||
return;
|
||||
}
|
||||
var pod = pods.getItems().get(0);
|
||||
|
||||
// Patch pod annotation
|
||||
PatchOptions patchOpts = new PatchOptions();
|
||||
patchOpts.setFieldManager("kubernetes-java-kubectl-apply");
|
||||
var podMeta = pod.getMetadata();
|
||||
var res = podApi.patch(podMeta.getNamespace(), podMeta.getName(),
|
||||
V1Patch.PATCH_FORMAT_JSON_PATCH,
|
||||
new V1Patch("[{\"op\": \"replace\", \"path\": "
|
||||
+ "\"/metadata/annotations/vmrunner.jdrupes.org~1cmVersion\", "
|
||||
+ "\"value\": \"" + newCm.getMetadata().getResourceVersion()
|
||||
+ "\"}]"),
|
||||
patchOpts);
|
||||
if (!res.isSuccess()) {
|
||||
logger.warning(
|
||||
() -> "Cannot patch pod annotations: " + res.getStatus());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue