Dima Kovbasa
Dima Kovbasa

Reputation: 87

Update ReplicaSet using Java Kubernetes Client

I am writing a client that manages Kubernetes objects. Is it possible to update ReplicaSets using a Java client?

Upvotes: 1

Views: 1109

Answers (2)

Rafał Leszko
Rafał Leszko

Reputation: 5531

Yes, you can update ReplicaSet using Java Kubernetes Client. Depending on which Kubernetes client you use here are the code snippets.


Kubernetes Java Client

AppsV1Api api = new AppsV1Api();
api.patchNamespacedReplicaSet(...);

Fabric8 Kubernetes & OpenShift Java Client

KubernetesClient client = new DefaultKubernetesClient();
client.apps().replicaSets().createOrReplace(...);

Upvotes: 3

Justin Tamblyn
Justin Tamblyn

Reputation: 733

I was just doing this this morning!

Yes you can do that. Checkout Fabric8 Kubernetes client for Java (https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ReplicaSetTest.java)

An example of the change would be:

        try (KubernetesClient k8sClient = new DefaultKubernetesClient()) {
            ReplicaSetList list = k8sClient.apps().replicaSets().inNamespace("default").list();
            for (ReplicaSet next : list.getItems()) {
                next.getSpec().setReplicas(10);
                k8sClient.apps().replicaSets().create(next);
            }
        } catch (Exception e) {
            //TODO: logging
        }

Just be sure to use the correct one. In this example I am changing all of them in the default namespace.

Upvotes: 1

Related Questions