How could you implement `kubectl apply -f stuff.yaml` with the java Kubernetes Client

11/5/2018

I would like to execute an operation on Kubernetes like kubectl apply -f stuff.yaml from a java program. I don't want to invoke kubectl from my Java program, instead, I would like to use the Java Kubernetes client. After looking at the API classes in the project I wasn't able to figure what methods I could use to achieve functionality similar to kubectly apply.

Does anyone have any pointers on how to achieve it?

-- ilooner
kubernetes

1 Answer

11/6/2018

There are no methods per se or silver bullet really, essentially what you are trying to do is almost trying to rewrite kubectl in Java.

You should be able to achieve it decoding the YAML using something like Jackson or SnakeYAML and use all the different components in the Kubernetes client, like Create namespaces, pods, deployments, etc.

You can also do a brute force approach through the kube-apiserver on https://kube-apiserver-address:6443/api/... by sending an authenticated/authorized GET/POST/DELETE request with a JSON payload which you can get from converting the YAML to JSON (and perhaps a bit of cleanup) You can use something like the Apache HTTP client library or Jersey.

-- Rico
Source: StackOverflow