Featured image for Java topics.

Fabric8 Kubernetes Client provides two ways of interacting with CustomResources running in any Kubernetes cluster:

  • Typed API (covered in Part 2)
  • Typeless API (covered in Part 3)

While typeless API provides a way to deal with CustomResources generically, it’s not always a preferable option for a strongly typed language like Java. Most developers want to use typed API to have complete and type-safe control over their CustomResources.

However, there is one additional step involved in using typed API. It provides POJOs for Kubernetes CustomResource objects. This article showcases how you can automatically generate CustomResourceDefinition-related code using tools offered by the Fabric8 Kubernetes Client library.

This is the fourth installment in the following series of articles:

2 CRD generation options

There are two possible approaches to generating code while using custom resources:

  1. Users coming from a Java background would have more confidence in writing Java classes than in writing CRD YAML, which is error-prone. They would prefer to generate CRD Yaml from the Java POJO they wrote.
  2. It is also possible that there is an existing CRD YAML file for which you can generate Java POJOs.

Fabric8 Kubernetes Client provides tooling for both of these approaches via CRD Generator and Java Generator

Let’s take an example of the Book CustomResource we used in part 2, Programming Kubernetes custom resources in Java, to see both approaches to generating Java POJOs from CRD YAML and vice versa.

Generating CRD POJOs from CRD YAML

Consider a scenario with only CRD YAML manifest for the Book CustomResource. We want to automatically generate Java POJOs for Book CustomResource. We’ll be using Fabric8 Java Generator to do this. We’ll be using its Java Generator Maven Plugin to generate Java POJOs from YAML during build time.

Let’s start with a basic maven project and place our Book CustomResource YAML in our project’s resources folder. If you don’t have an existing project, you can clone this GitHub repository:

fabric8-crd-java-generator-demo : $ tree src/main/resources/
└── crd
    └── book-crd.yaml

In our project’s pom.xml let’s add Fabric8 Java Generator Maven Plugin configuration to generate Java POJOs for CRD YAML present in src/main/resources folder:

        <!-- 1 -->
         <!-- 2 -->

By providing this plugin configuration, we’re instructing Java Generator Maven Plugin to do the following:

  • Pick book-crd.yaml file as a source file for generating CustomResource POJOs.
  • Add extra annotations to generated classes that can be processed by Sundrio to add additional helper builders and fluent classes. This would also require Sundrio and lombok dependencies added to the project.

Compile the project as follows:

fabric8-crd-java-generator-demo : $ mvn clean install

Upon running mvn clean install, you can see that Book CustomResource POJOs are automatically generated intarget/generated-sources/java/. This is the default location of generated sources. If you want to generate these POJOs in some other directory, you can provide target in Java Generator Maven Plugin configuration section or via fabric8.java-generator.target property.

Java Generator Maven Plugin also automatically adds these generated classes to maven source paths. So you don’t have to worry about adding extra configuration to add these generated classes to the maven source path.

There are also some helper classes generated in target/generated-sources/java/:

fabric8-crd-java-generator-demo : $ tree target/generated-sources/java/
└── io
    └── fabric8
        └── demo
            └── generator
                └── v1alpha1
                    ├── Book.java
                    ├── BookSpec.java
                    └── BookStatus.java

5 directories, 3 files

fabric8-crd-java-generator-demo : $ tree target/generated-sources/annotations/
└── io
    └── fabric8
        └── demo
            └── generator
                └── v1alpha1
                    ├── BookBuilder.java
                    ├── BookFluentImpl.java
                    ├── BookFluent.java
                    ├── BookSpecBuilder.java
                    ├── BookSpecFluentImpl.java
                    ├── BookSpecFluent.java
                    ├── BookStatusBuilder.java
                    ├── BookStatusFluentImpl.java
                    └── BookStatusFluent.java

5 directories, 9 files

You can go ahead and start writing code based on these generated Book POJOs. Java Generator Maven Plugin allows various configuration options to configure source generation. You can take a look here at the source code or documentation for more details.

You can find the source code for this in this GitHub repository.

Generating CRD Yaml from POJOs

Now let’s take a look at the opposite scenario. We already have Java sources for the Book CustomResource but we want to generate CustomResourceDefinition YAML manifests.

We have our POJOs in src/main/java folder as usual (see GitHub repository for source code).

fabric8-java-crd-yaml-generator-demo : $ tree src/main/java/
└── io
    └── fabric8
        └── demo
            └── crd
                └── v1alpha1
                    ├── Book.java
                    ├── BookSpec.java
                    └── BookStatus.java

5 directories, 3 files

We’ll be using Fabric8 Crd Generator to generate CRD Yaml files for Book CustomResource. It’s available as a Java Annotation Processor, so you can simply add it as a dependency in your project, and you should be good to go.

Add Fabric8 Crd Generator dependency to your pom.xml:


Compile project:

fabric8-java-crd-yaml-generator-demo : $ mvn clean install


[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ fabric8-java-crd-yaml-generator-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /home/rokumar/work/repos/kubernetes-client-demo/fabric8-java-crd-yaml-generator-demo/target/classes
[INFO] Generating CRD books.testing.fabric8.io:
[INFO]   - v1beta1 -> /home/rokumar/work/repos/kubernetes-client-demo/fabric8-java-crd-yaml-generator-demo/target/classes/META-INF/fabric8/books.testing.fabric8.io-v1beta1.yml
[INFO]   - v1 -> /home/rokumar/work/repos/kubernetes-client-demo/fabric8-java-crd-yaml-generator-demo/target/classes/META-INF/fabric8/books.testing.fabric8.io-v1.yml

You will notice that during compilation, the Fabric8 Crd Annotation processor generated CustomResourceDefinition YAML files in target/classes/META-INF/fabric8 folder. It picks up all of the classes in the project, which extend io.fabric8.kubernetes.client.CustomResource and generate CustomResourceDefinition YAML files for each of them.

fabric8-java-crd-yaml-generator-demo : $ ls target/classes/META-INF/fabric8/

books.testing.fabric8.io-v1beta1.yml  books.testing.fabric8.io-v1.yml

You will notice that there are two YAML files generated (one with v1 and one with v1beta1 suffix). The v1beta1 is kept for backward compatibility for older Kubernetes Clusters, which only supported apiextensions.k8s.io/v1beta1 CustomResourceDefinitions. For most cases, you would most likely be working with the v1 YAML file.

You can go ahead and inspect the contents of generated CustomResourceDefinition YAML files and use them in your Kubernetes clusters.

For more information about the different configuration options available, check out Fabric8 CRD Generator Documentation.

You can find code for this example in this GitHub repository.

Learn more about Fabric8 Kubernetes Client

In this blog post, you learned about the code generation capabilities of Fabric8 Kubernetes Client’s tooling in form of Java Generator and CRD Generator. You can find the code in this GitHub repository. The next article in my series, How to write tests with Fabric8 Kubernetes Client, discusses Fabric8 Kubernetes Client testing libraries, Fabric8 Kubernetes Mock Server, and Fabric8 JUnit5 Extension.

For more information, check out the Fabric8 Kubernetes Client GitHub page. Feel free to follow us on these channels: