Handling Kubernetes Custom Resources in Java using Fabric8 Kubernetes Client (Part 1)
Note: Fabric8 Kubernetes Client raw API has been removed from 6.x version of KubernetesClient. Users are advised to use GenericKubernetesResource API instead.
Custom resources are extensions of Kubernetes API that are not available in default Kubernetes installation. Kubernetes allows you to register and manipulate your own resources into it, making Kubernetes more modular.
In this blog, I would be talking about handling custom resources using Java with the Fabric8 Kubernetes Client. It is one of the most popular Kubernetes Java libraries available on Github. For this blog I would be going through this code in one of my demo repositories. It can be found at:
First, you need to add it into your project’s pom.xml:
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>6.1.1</version>
</dependency>
For using custom resources via this library, let’s take a simple example of a custom resource called animal. It’s Custom Resource Definition looks like this:
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: animals.jungle.example.com
spec:
group: jungle.example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
image:
type: string
scope: Namespaced
names:
plural: animals
singular: animal
kind: Animal
shortNames:
- al
As you can see, we have configured basic things for our custom resource, like naming, whether it’s a global resource or namespaced, API group etc. For a custom resource definition like this, we can have a simple custom resource like this:
apiVersion: jungle.example.com/v1
kind: Animal
metadata:
name: walrus
spec:
image: my-awesome-walrus-image
- Creating Custom Resource Definition in cluster:
In order to create this custom resource definition into the cluster, you simply need to do this:
2. Creating, Updating, Deleting Custom Resources:
All right, once our Custom Resource Definition is applied onto our cluster, we are good to go with creating and manipulating custom resources. Earlier in Fabric8 Client, we required POJOs related to Custom Resource classes, see CRDExample.java. But I find it slightly tedious to write POJOs for doing basic custom resource operations. If you’re interested in typed API, you can have a look at this blogpost:
Here we are going to look into the new typeless api which doesn’t require any POJOs, we just need to declare one object called CustomResourceDefinitionContext
which simply contains some information about the Custom Resource Definition which is needed by client during resource URL creation. Here is how our CRD context would look for our animals CRD:
Once we have initialized CustomResourceDefinitionContext
we can simply start creating custom resources. Fabric8 Kubernetes Client has introduced an equivalent of client-go’s Unstructured
in java : GenericKubernetesResource . We can use GenericKubernetesResource to store any CustomResource. It only contains apiVersion
, kind
and metadata
fields. Any additional field which are mostly specific to each CustomResource are stored in additionalProperties
map.
Now if you want to list custom resources created in your cluster, this is also very easy with Fabric8 client:
Let’s take a look at examples of updating custom resource . In case you want to update your existing custom resource . This can be done something like this:
Deletion is also in a similar fashion:
3. Watching custom resources:
With this API, you can also watch for events related to your created Custom Resources in your cluster. Right now watch events are sent as plain strings, so you need to de-serialize them on your own. Here is a simple example of how we can setup a watch for animals custom resource:
and that’s it, here is how it would look when we run this program:
I hope with this you get an overview of new custom resource API of Fabric8 Kubernetes Client. Please try it out and provide feedback. We 💚 contributions!