Skip to main content
AI
  • Get started with AI

    • Red Hat AI

      Accelerate the development and deployment of enterprise AI solutions.

    • AI learning hub

      Explore learning materials and tools, organized by task.

    • AI interactive demos

      Click through scenarios with Red Hat AI, including training LLMs and more.

    • AI/ML learning paths

      Expand your Red Hat OpenShift AI knowledge using these learning resources.

    • AI quickstarts

      Focused AI use cases designed for fast deployment on Red Hat AI platforms.

    • No-cost AI training

      Foundational Red Hat AI training.

  • Featured resources

    • Red Hat OpenShift AI
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

Red Hat AI Factory with NVIDIA

Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.

Learn
  • Self-guided

    • Documentation

      Find answers, get step-by-step guidance, and learn how to use Red Hat products.

    • Learning paths

      Explore curated walkthroughs for common development tasks.

    • Guided learning

      Receive custom learning paths powered by our AI assistant.

    • See all learning
  • Hands-on

    • Developer Sandbox

      Spin up Red Hat's products and technologies without setup or configuration.

    • Interactive labs

      Learn by doing in these hands-on, browser-based experiences.

    • Interactive demos

      Click through product features in these guided tours.

  • Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics
  • Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
Build
  • Get started

    • Red Hat build of Podman Desktop

      A downloadable, local development hub to experiment with our products and builds.

    • Developer Sandbox

      Spin up Red Hat's products and technologies without setup or configuration.

  • Download products

    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products
  • Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset
  • References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
Community
  • Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions
  • Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github
  • Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

Join the Red Hat Developer program

Download Red Hat products and project builds, access support documentation, learning content, and more.

ConsoleDocsSupport Search
New For you

Recommended

We'll recommend resources you may like as you browse. Try these suggestions for now.

  • Product downloads
  • Cheat sheets
  • Red Hat Developer learning portal
  • Developer blog
  • E-books
  • Developer Sandbox
Log in

Get more with a Red Hat Developer account

  • Product downloads
  • Console access
  • Event registration
  • Training & trials
Explore more account benefitsLog in or register

A subscription may be required for some services.

Contact us

How to generate code using Fabric8 Kubernetes Client

January 24, 2023
Rohan Kumar
Related topics:
JavaKubernetes
Related products:
Red Hat Enterprise Linux

    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:

    • Part 1: How to use Fabric8 Java Client with Kubernetes
    • Part 2: Programming Kubernetes custom resources in Java
    • Part 3: How to use Kubernetes dynamic client with Fabric8
    • Part 4: How to generate code using Fabric8 Kubernetes Client
    • Part 5: How to write tests with Fabric8 Kubernetes Client

    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/
    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:

    <plugin>
        <groupId>io.fabric8</groupId>
        <artifactId>java-generator-maven-plugin</artifactId>
        <version>${fabric8.version}</version>
        <configuration>
            <!-- 1 -->
            <source>${project.basedir}/src/main/resources/crd/book-crd.yaml</source> 
             <!-- 2 -->
            <extraAnnotations>true</extraAnnotations>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    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/
    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/
    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/
    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:

    <dependency>
        <groupId>io.fabric8</groupId>
        <artifactId>crd-generator-apt</artifactId>
        <version>${fabric8.version}</version>
    </dependency>
    

    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:

    • StackOverflow
    • Fabric8 Kubernetes Client CHEATSHEET
    • Twitter
    • Gitter Chat

    Related Posts

    • How to use Fabric8 Java Client with Kubernetes

    • Programming Kubernetes custom resources in Java

    • How to use Kubernetes dynamic client with Fabric8

    • Configuring Kubernetes operands through custom resources

    Recent Posts

    • Run Red Hat Enterprise Linux CoreOS on NVIDIA BlueField DPUs

    • Agent harness engineering: Separating model capabilities from enforcement

    • What's new for developers in Red Hat Enterprise Linux 10.3 and 9.9

    • Control multicluster costs with right-sizing in Red Hat Advanced Cluster Management 5

    • Lightbits is now certified for Red Hat OpenStack Services on OpenShift

    What’s up next?

    Find out how you can move your legacy Java application into a container and deploy it to Kubernetes in minutes using the Developer Sandbox for Red Hat OpenShift.

    Try Java in the sandbox
    Red Hat Developers logo

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    Red Hat Developer

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility
    Ask AI