Kubernetes operators can be developed with Java. This article describes one of the key capabilities developers need to understand, which is how to access Kubernetes resources.
I have published a little sample I’m working on. For the sample I use the Operator SDK and the Java Operator SDK. Read my previous blog Developing and Debugging Kubernetes Operators in Java for more backend information.
To access Kubernetes resources the Java SDK uses the Kubernetes Client provided by fabric8.io. This client provides access to the full Kubernetes & OpenShift REST APIs via a fluent DSL.
Rather than using yaml/json files and kubectl/oc CLIs you can create, modify and delete all Kubernetes resources easily from Java with a nice API.
To get started it helped me to read the cheatsheet and look at some samples.
Let’s take a look how to create a deployed used in my sample. This is the yaml we want to create:
1
2
3
4
5
6
7
8
9
apiVersion: apps/v1
kind: Deployment
metadata:
name: service-backend
spec:
replicas: 1
selector:
matchLabels:
app: service-backend
And here is a snippet how to generate this definition.
1
2
3
4
5
6
7
8
9
10
11
Deployment deployment = new DeploymentBuilder()
.withNewMetadata()
.withName("service-backend")
.withNamespace(namespace)
.addToLabels("test", "deployment")
.endMetadata()
.withNewSpec()
.withReplicas(1)
.withNewSelector()
.addToMatchLabels("app","service-backend")
.endSelector()
I like the design of the API, since you can easily mirror the structure of the yaml format. Check out especially the indents in the snippets above.
Keep an eye on this blog to find out more about how to build operators.