Last active
July 30, 2020 21:44
-
-
Save pgilad/c843bcbb3495e4464fe5ee91ba6086a5 to your computer and use it in GitHub Desktop.
Example of how to patch a deployment image on Kubernetes using Java client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.io.IOException; | |
import java.util.Collections; | |
import java.util.List; | |
public class DeploymentImagePatch extends DefaultTask { | |
@Input | |
public String namespace = "ns-pelgi01"; | |
@Input | |
public String deploymentName = "dpl-generic-dagger-57-pelgi01-gilad-dev"; | |
@Input | |
public String ecrUri = "755498891413.dkr.ecr.us-east-1.amazonaws.com"; | |
@Input | |
public String imageName = "bm-data-query-service"; | |
@Input | |
public String roleSelector = "role=generic-dagger"; | |
@Input | |
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); | |
} | |
@SuppressWarnings("SameParameterValue") | |
private static String getFinalImageName(String ecrUri, String imageName, String newTag) { | |
return String.format("%s/%s:%s", ecrUri, imageName, newTag); | |
} | |
@TaskAction | |
public void main() throws IOException { | |
ApiClient client = Config.defaultClient(); | |
Configuration.setDefaultApiClient(client); | |
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) { | |
e.printStackTrace(); | |
return; | |
} | |
deployments | |
.getItems() | |
.stream() | |
.filter(d -> d.getMetadata().getName().equals(deploymentName)) | |
.findFirst() | |
.ifPresent((AppsV1beta1Deployment deployment) -> { | |
final String deploymentName = deployment.getMetadata().getName(); | |
final String currentName = deployment | |
.getSpec() | |
.getTemplate() | |
.getSpec() | |
.getContainers() | |
.get(0) | |
.getImage(); | |
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) { | |
e.printStackTrace(); | |
return; | |
} | |
try { | |
final AppsV1beta1Deployment response = api.patchNamespacedDeployment(deploymentName, namespace, body, "true"); | |
System.out.println(response.getStatus()); | |
} catch (ApiException e) { | |
System.out.println(e.getResponseBody()); | |
} | |
}); | |
} | |
private static class ReplaceImageOp { | |
@JsonProperty | |
String op = "replace"; | |
@JsonProperty | |
String path = "/spec/template/spec/containers/0/image"; | |
@JsonProperty | |
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