Kubernetes Clients API using Fabric8

6/8/2020

I'm using fabric8 java client library for Kubernetes. I am not able to find the best way to perform update operations on containers. Basically what I want to do is I have created a pod with container image as "nginx" now I want to update this image to "nginx:1.16.1".

What I have tried to do is

client.pods().inNamespace(podsModel.getNamespace()).withName(podsModel.getNamespace()).edit().editSpec()
				.editContainer(0).withNewImage("nginx:1.16.1").endContainer().endSpec().buildSpec();
-- Harmeet Kaur
fabric8
java
kubernetes
kubernetes-pod

1 Answer

6/8/2020

You should be able to edit Pod spec using the code above, you just need to use done() rather than buildSpec():

try (KubernetesClient client = new DefaultKubernetesClient()) {
  Pod updatedPod = client.pods().inNamespace(namespace)
        .withName(podName)
        .edit().editSpec().editContainer(0)
        .withImage("nginx:1.16.1")
        .endContainer().endSpec().done();
}

However you should not be using Pod by itself. Pods are designed as relatively ephemeral, disposable entities. You should use some controller resource (Deployment, StatefulSet etc.) which can manage Pod objects on your behalf.

There is a document provided by the maintainers for common Fabric8 Kubernetes Client operations. You can take a look at this as well: Fabric8 Kubernetes Client Cheat Sheet.

-- Rohan Kumar
Source: StackOverflow