Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example of how to patch a deployment image on Kubernetes using Java client
package com.blazemeter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.Configuration;
import io.kubernetes.client.apis.AppsV1beta1Api;
import io.kubernetes.client.models.AppsV1beta1Deployment;
import io.kubernetes.client.models.AppsV1beta1DeploymentList;
import io.kubernetes.client.util.Config;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;
import java.util.Collections;
import java.util.List;
public class DeploymentImagePatch extends DefaultTask {
public String namespace = "ns-pelgi01";
public String deploymentName = "dpl-generic-dagger-57-pelgi01-gilad-dev";
public String ecrUri = "";
public String imageName = "bm-data-query-service";
public String roleSelector = "role=generic-dagger";
public String newTag = "0.10.0";
private static byte[] getPatchBody(String newImage) throws JsonProcessingException {
final ObjectMapper objectMapper = new ObjectMapper();
final List<ReplaceImageOp> ops = Collections.singletonList(new ReplaceImageOp(newImage));
return objectMapper.writeValueAsBytes(ops);
private static String getFinalImageName(String ecrUri, String imageName, String newTag) {
return String.format("%s/%s:%s", ecrUri, imageName, newTag);
public void main() throws IOException {
ApiClient client = Config.defaultClient();
AppsV1beta1Api api = new AppsV1beta1Api();
System.out.println("Finding deployment");
AppsV1beta1DeploymentList deployments;
try {
deployments = api.listNamespacedDeployment(namespace, null, null, null, null, roleSelector, null, null, null, null);
} catch (ApiException e) {
.filter(d -> d.getMetadata().getName().equals(deploymentName))
.ifPresent((AppsV1beta1Deployment deployment) -> {
final String deploymentName = deployment.getMetadata().getName();
final String currentName = deployment
System.out.println("Deployment: " + deploymentName);
System.out.println("Current image: " + currentName);
final String newImage = getFinalImageName(ecrUri, imageName, newTag);
System.out.println("Patching deployment image to " + newImage);
// use bytes because this java api sucks
final byte[] body;
try {
body = getPatchBody(newImage);
} catch (JsonProcessingException e) {
try {
final AppsV1beta1Deployment response = api.patchNamespacedDeployment(deploymentName, namespace, body, "true");
} catch (ApiException e) {
private static class ReplaceImageOp {
String op = "replace";
String path = "/spec/template/spec/containers/0/image";
String value;
ReplaceImageOp(String value) {
this.value = value;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.