<p>I would like to execute an operation on Kubernetes like <code>kubectl apply -f stuff.yaml</code> from a java program. I don't want to invoke kubectl from my Java program, instead, I would like to use the Java <a href="https://github.com/kubernetes-client/java" rel="nofollow noreferrer">Kubernetes client</a>. 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 <code>kubectly apply</code>.</p>

<p>Does anyone have any pointers on how to achieve it?</p>

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

<p>You should be able to achieve it decoding the YAML using something like <a href="https://stackoverflow.com/a/38274671/2989261">Jackson</a> or <a href="https://bitbucket.org/asomov/snakeyaml/src/default/" rel="nofollow noreferrer">SnakeYAML</a> and use all the different components in the <a href="https://github.com/kubernetes-client/java" rel="nofollow noreferrer">Kubernetes client</a>, like Create namespaces, pods, deployments, etc. </p>

<p>You can also do a <a href="https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/" rel="nofollow noreferrer">brute force</a> approach through the <code>kube-apiserver</code> on <code>https://kube-apiserver-address:6443/api/...</code> 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 <a href="http://hc.apache.org/httpclient-3.x/" rel="nofollow noreferrer">Apache HTTP client library</a> or <a href="https://jersey.github.io/" rel="nofollow noreferrer">Jersey</a>.</p>
