Does Fabric8io K8s java client support patch() or rollingupdate() using YAML snippets? Does Fabric8io K8s java client support patch() or rollingupdate() using YAML snippets? kubernetes kubernetes

Does Fabric8io K8s java client support patch() or rollingupdate() using YAML snippets?


Here is the related bug in Fabric8io rolling API: https://github.com/fabric8io/kubernetes-client/issues/1868

As of now, one way I found to achieve patching with fabri8io APIs is to :

  1. Get the running deployment object
  2. add/replace containers in it with new containers
  3. use the createOrReplace() API to redeploy the deployment object

But your patch understandably could be more than just an update to the containers field. In that case, processing each editable field becomes messy.

I went ahead with using the official K8s client's patchNamespacedDeployment() API to implement patching. https://github.com/kubernetes-client/java/blob/356109457499862a581a951a710cd808d0b9c622/examples/src/main/java/io/kubernetes/client/examples/PatchExample.java


With latest improvements in Fabric8 Kubernetes Client, you can do it both via patch() and rolling() API apart from using createOrReplace() which is mentioned in older answer.

Patching JSON/Yaml String using patch() call:

As per latest release v5.4.0, Fabric8 Kubernetes Client does support patch via raw string. It can be either YAML or JSON, see PatchTest.java. Here is an example using raw JSON string to update image of a Deployment:

try (KubernetesClient kubernetesClient = new DefaultKubernetesClient()) {  kubernetesClient.apps().deployments()    .inNamespace(deployment.getMetadata().getNamespace())    .withName(deployment.getMetadata().getName())    .patch("{\"spec\":{\"template\":{\"spec\":{\"containers\":[{\"name\":\"patch-demo-ctr-2\",\"image\":\"redis\"}]}}}}");}

Rolling Update to change container image:

However, if you just want to do rolling update; You might want to use rolling() API instead. Here is how it would look like for updating image of an existing Deployment:

try (KubernetesClient client = new DefaultKubernetesClient()) {    // ... Create Deployment     // Update Deployment for a single container Deployment    client.apps().deployments()            .inNamespace(namespace)            .withName(deployment.getMetadata().getName())            .rolling()            .updateImage("gcr.io/google-samples/hello-app:2.0");}

Rolling Update to change multiple images in multi-container Deployment:

If you want to update Deployment with multiple containers. You would need to use updateImage(Map<String, String>) method instead. Here is an example of it's usage:

try (KubernetesClient client = new DefaultKubernetesClient()) {    Map<String, String> containerToImageMap = new HashMap<>();    containerToImageMap.put("nginx", "stable-perl");    containerToImageMap.put("hello", "hello-world:linux");    client.apps().deployments()            .inNamespace(namespace)            .withName("multi-container-deploy")            .rolling()            .updateImage(containerToImageMap);}

Rolling Update Restart an existing Deployment

If you need to restart your existing Deployment you can just use the rolling().restart() DSL method like this:

try (KubernetesClient client = new DefaultKubernetesClient()) {    client.apps().deployments()            .inNamespace(namespace)            .withName(deployment.getMetadata().getName())            .rolling()            .restart();}