Developing a Kubernetes Operator in Java
Key point
Build a Petclinic operator by implementing CRDs, Reconciler, and Dependent Resources with the Java Operator SDK.
Details
Building on a Spring Boot 3.3.1, Java 21-based project, Java Operator SDK 5.5.0 and fabric8 crd-generator-apt 6.13.0 are added to implement a Kubernetes Operator in Java.
The core structure is divided into Primary Resource, Spec, Status, Reconciler, and KubernetesDependentResource. PetclinicSpec holds image, size, and port, while PetclinicStatus extends ObservedGenerationAwareStatus to support change tracking. The Petclinic CR is defined using @Group("spring.my.domain") and @Version("v1").
The dependent resources consist of Deployment, Service, and Ingress.
PetclinicDeploymentResourcecreates aDeployment, reading replicas, image, and container port fromspecand applying them.PetclinicServiceResourceexposes the service asNodePort.PetclinicIngressResourceconnects thenginxingress class with the/path to route to the service.
PetclinicReconciler wires the three resources into the dependents of @ControllerConfiguration, and in reconcile() it returns UpdateControl.updateResourceAndPatchStatus(petclinic). On error, it patches the status with updateErrorStatus(), and on deletion, it uses DeleteControl.defaultDelete().
After registering PetclinicReconciler and Operator as Beans in a separate PetclinicOperatorConfig, the generated CRD YAML under META-INF/fabric8 in the build output is applied to the cluster with kubectl apply. Then, deploying a Petclinic resource on a kind-based local cluster confirms that a Deployment, Service, and Pod named sample are automatically created and that the application works as expected.
This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.
Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.