I want to restart deployment pod by patching ENV variable in deployment. Here is my code:
String PATCH_STR = "[{\"op\":\"replace\",\"path\":\"/spec/template/spec/containers/0/env/8/UPDATEDON\",\"value\": \"%d\"}]";
final String patchStr = String.format(PATCH_STR, System.currentTimeMillis());
AppsV1Api api = new AppsV1Api(apiClient);
V1Deployment deploy = PatchUtils.patch(V1Deployment.class,
() -> api.patchNamespacedDeploymentCall(
deploymentName,
namespace,
new V1Patch(patchStr),
null,
null,
null, // field-manager is optional
null,
null),
V1Patch.PATCH_FORMAT_JSON_PATCH,
apiClient);
This code executes successfully but it does not start pod. Here is an equivalent kubectl command (it doesn't patch, so pod doesn't start):
kubectl -n aaaac7bg7b6nsaaaaaaaaaaoyu patch deployment aaaaaaaaxldpcswy2bl3jee6umwck72onc55wimyvldrfc442rokz3cpll2q -p '{"spec":{"containers":[{"env":[{"name":"UPDATEDON","value":"1645099482000"}]}]}}'
If I execute following command, it restarts pod:
kubectl -n aaaac7bg7b6nsaaaaaaaaaaoyu set env deployment/aaaaaaaaxldpcswy2bl3jee6umwck72onc55wimyvldrfc442rokz3cpll2q UPDATEDON=1645099482000
I thought of using V1EnvVar/V1EnvVarBuilder but I couldn't find equivalent java code.
There are a couple of issues with your example. In general, if you successfully update the environment variables in the pod template of your deployment, the Kubernetes operator will recognize the change and start a new pod to reflect the change.
When you perform the update with a JSON patch by specifying the operation (
replace), the path, and the value, the path must directly match the property in the deployment manifest. In your case, since you want to change thevalueof the environment variable, this would be:There is no need to repeat the name of the environment variable. The index, here
8, already signifies which variable you want to update, so there is no need to repeatUPDATEDON.The equivalent command with
kubectlwould beAlternatively, instead of using a JSON patch, you can used the default patch type, like you did in your example. However, you forgot to add the outermost
spec/templatelayers. Addionaly, you also need the given identify the container by specifying it's name. Here I've usedtestas the container's name.This way of updating has the advantage that you identify the container and the environment variable by their names, so and you don't need to rely on the ordering as would be the case with the index-based JSON patch path.