SpringOne 2021:
Session Title: A Series of Fortunate Events: Building an Operator in Java
Speakers: Alberto C. Ríos, Staff Engineer at VMware; Bella Bai, Software Engineer at VMware
Odoo Development Company in India | Devintelle Consulting Service
A Series of Fortunate Events: Building an Operator in Java
1. A series of fortunate events
Building an operator in Java
September 1–2, 2021
springone.io
1
Bella Bai
@bellalleb_bai
LittleBaiBai
Alberto C. Ríos
@Albertoimpl
Albertoimpl
Sample app:
https://github.com/building-k8s-operator/kubernetes-java-operator-sample
3. Operators are software extensions to
Kubernetes that make use of custom
resources to manage applications and
their components. Operators follow
Kubernetes principles, notably the control
loop.
3
-- Kubernetes Documentation
Quote source: https://kubernetes.io/docs/concepts/extend-kubernetes/operator/
4. Control loop of operators
4
Image source: https://github.com/cncf/tag-app-delivery/blob/master/operator-wg/whitepaper/Operator-WhitePaper_v1-0.md#operator-components-in-kubernetes
5. An operator is a Kubernetes controller that
understands two domains: Kubernetes and
something else.
By combining knowledge of both domains, it
can automate tasks that usually require a human
operator that understands both domains.
5
-- Jimmy Zelinskie
Quote source: https://github.com/kubeflow/tf-operator/issues/300#issuecomment-357527937
6. The “business” context in our sample app
6
apiVersion: "operator.example.com/v1alpha1"
kind: AdoptionCenter
metadata:
name: kittens-dream-land
apiVersion: "operator.example.com/v1alpha1"
kind: CatForAdoption
metadata:
name: my-precious-fluffy-ball
spec:
name: Chocobo
dateOfBirth: 2014-09-17
description: She is chubby, lazy ...
adoptionCenterName: kittens-dream-land
The AdoptionCenter icon is made by Freepik: https://www.flaticon.com/
7. The “business” context in our sample app
7
The AdoptionCenter icons are made by Freepik: https://www.flaticon.com/
9. When do we want to use an operator
● Deploying applications on demand
● Backing up and restoring an application's
state
● Automating application upgrades
● Provide a decentralized way to manage
centralized resources
9
10. When NOT to use an operator
● It is just an app with some configuration
● You don't need some special business logic for
handling most of the operational work and can
deploy it as it is
● When you can just deploy everything with Helm or
Kustomize
● No need for any kind of persistence nor status to
be backed
10
12. Starting from scratch
12
● Pick an operator library with Spring support
○ Kubernetes Java Client
○ Java Operator SDK
● Generate models based on your CRDs
○ Or generate your CRDs based on your model
○ Maintain single source of truth
● Setup your controller with resource listeners
Kubernetes Java Client: https://github.com/kubernetes-client/java
Java Operator SDK: https://github.com/java-operator-sdk/java-operator-sdk
14. Starting from scratch
14
● Pick an operator library with Spring support
○ Kubernetes Java Client
○ Java Operator SDK
● Generate models based on your CRDs
○ Or generate your CRDs based on your model
○ Maintain single source of truth
● Setup your controller with resource listeners
Kubernetes Java Client: https://github.com/kubernetes-client/java
Java Operator SDK: https://github.com/java-operator-sdk/java-operator-sdk
● Start reconciling 💪
15. Example reconciler
15
@Override
public Result reconcile(Request request) {
V1alpha1AdoptionCenter center = adoptionCenterLister.get(request.getName())
try {
V1OwnerReference owner = toOwnerReference(center);
configMapUpdater.createConfigMap(owner);
deploymentEditor.createDeployment(owner);
return new Result(false);
}
catch (Exception e) {
return new Result(true);
}
}
20. Managing status
% kubectl get cats
NAME READY REASON
my-precious-fluffy-ball True CatAddedToConfigMap
2
0
21. Managing status
schema:
openAPIV3Schema :
type: object
properties :
spec:
...
status:
type: object
properties:
conditions:
type: array
items:
type: object
properties:
type:
type: string
description: The unique identifier of a condition, used to
distinguish between other conditions in the resource.
status:
type: string
description: The status of the condition.
21
https://github.com/building-k8s-operator/kubernetes-java-operator-sample/blob/main/
crds/cat-custom-resource-definition.yaml
33. Finalizers are namespaced keys that tell
Kubernetes to wait until specific
conditions are met before it fully deletes
resources marked for deletion.
You can use finalizers to control garbage
collection of resources.
33
-- Kubernetes Documentation
Quote source: https://kubernetes.io/docs/concepts/overview/working-with-objects/finalizers/
34. Removing CatForAdoption without finalizers
34
The AdoptionCenter icons are made by Freepik: https://www.flaticon.com/
Operator
Desired State Actual State
35. Removing CatForAdoption without finalizers
3
5
The AdoptionCenter icons are made by Freepik: https://www.flaticon.com/
Operator
Desired State Actual State
Return “Null”
36. Actual State
Desired State
Return “Null”
Removing CatForAdoption without finalizers
3
6
The AdoptionCenter icons are made by Freepik: https://www.flaticon.com/
Operator
37. Removing CatForAdoption with finalizers
37
The AdoptionCenter icons are made by Freepik: https://www.flaticon.com/
Operator
Desired State Actual State
metadata.deletionTimeStamp != null
40. Watch out when you uninstall
4
0
● Resources pending delete without operator
● What to do?
○ Pre-delete hook in operator
○ Pre-delete hook in Helm or other
packaging options
○ Cancel uninstallation
○ Carefully order the uninstall process
○ Just document as known issue
43. Syntactic validation
schema:
openAPIV3Schema:
…
name:
type: string
description: The name of the cat
dateOfBirth:
type: string
format: date
description: Date of birth of the cat in the format defined in RFC 3339
description:
type: string
description: The description of the cat
adoptionCenterName:
type: string
description: Name of the adoption center to register this cat to
required: ["adoptionCenterName", "name"]
43
51. Testing
● Unit Tests
● Component Tests: Closed-box test from the Operator's
perspective
We do not assert correctness of the deployed application
but assert on the values it was deployed with.
51
54. Testing
● Unit Tests
● Component Tests
● Acceptance Tests: Testing the system as a whole
Using a real environment and installing the product as a
user would.
5
4
61. Closing notes
● Generate your Java classes from CRD:
https://github.com/building-k8s-operator/kubernetes-java-operator-sample#gen
erating-java-classes-from-crd
● Use Statuses, Finalizers, Readiness gates and Owner references.
● They can be TDDed!
61
62. Now it’s your turn to try!
https://github.com/building-k8s-operator/kubernetes-java-
operator-sample