Reputation: 67
I have an application that runs health checks on pods. Given the health check, I am attempting to patch a pod's label selector from being active: true to active: false. The following is the code for the iteration of pods to change each pod's labels.
CoreV1Api corev1Api = new CoreV1Api();
for (V1Pod pod : fetchPodsByNamespaceAndLabel.getItems()) {
String jsonPatchBody = "[{\"op\":\"replace\",\"path\":\"/spec/template/metadata/labels/active\",\"value\":\"true\"}]";
V1Patch patch = new V1Patch(jsonPatchBody);
corev1Api.patchNamespacedPodCall(pod.getMetadata.getName(), namespace, patch, null, null, null, null, null);
}
I have adapted the jsonPatchBody from the Patch Example on the Kubernetes documentation section for examples.
The output of the run spits out no errors. The expected behavior is for the labels of these pods for active to all be set to true. These changes are not reflected. I believe the issue to be caused by the syntax provided by the body of the patch. Is the above the correct syntax for accessing labels in a pod?
Upvotes: 2
Views: 2268
Reputation: 67
After researching more of the current implementation, the client provides the PatchUtils api that allows me to build a type of patch.
CoreV1Api coreV1Api = new CoreV1Api();
String body = "{\"metadata\":{\"labels\":{\"active\":\"true\"}}}";
V1Pod patch =
PatchUtils.patch(
V1Pod.class,
() ->
coreV1Api.patchNamespacedPodCall(
Objects.requireNonNull(pod.getMetadata().getName()),
namespace,
new V1Patch(body),
null,
null,
null,
null,
null),
V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH,
coreV1Api.getApiClient());
System.out.println("Pod name: " + Objects.requireNonNull(pod.getMetadata()).getName() + "Patched by json-patched: " + body);
I wanted to ensure that the patch updated the current values for a property in my labels selector, so I implemented a PATCH_FORMAT_STRATEGIC_MERGE_PATCH
from the V1Patch
api. I referenced the Kubernetes Patch Example to build the structure of the Patch.
Upvotes: 2