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

Examining Kubernetes internals using a REST API example

January 22, 2020
Abhishek Koserwal
Related topics:
ContainersKubernetesOperators
Related products:
Red Hat OpenShift

    In this article, we will examine the pattern when writing the REST API in any known framework vs. writing an Operator using Kubernetes' client libraries. The purpose of this article is to explain the Kubernetes internals by comparing REST API and Operator writing patterns. We will not discuss how to write a REST API.

    Prerequisites

    The following must be installed:

    • Go version 13.4
    • operator-sdk
    • CodeReady Containers (OpenShift version 4.2)
    • oc and kubectl
    • code repo

    As a developer, understanding and writing an operator will be easier for you if you have used the REST API with frameworks like Quarkus/Spring (Java), Express (Nodejs), Ruby on Rails, Flask (Python), Golang (mux), etc. We will use this experience with other languages or frameworks to build our understanding.

    By writing an operator, you implement a REST API using Kubernetes client libraries as your framework (i.e., extending the Kubernetes APIs with your custom logic). 

    If you are running a single-cluster Kubernetes instance locally using CRC/Minishift/Minikube, etc., type this command into the terminal:

    $ kubectl proxy

    The Kubernetes API endpoints are shown in Figure 1:

    Kubernetes API endpoints.
    Figure 1: Kubernetes API endpoints.

     

    As you can see, each entity within the Kubernetes domain space is an exposed API endpoint. Whenever you trigger a deployment or scale up a pod, you hit these APIs behind the scenes.

    We can start by writing a REST API in the framework that is familiar. I will use Spring Boot for building a simple Customer API. Later, we will write a customer-api-operator that extends Kubernetes' api-server with our defined Customer API. If you are not familiar with Spring Boot, you can continue. The main idea is to understand that writing the API using any language and using the Kubernetes client libraries follows nearly the same pattern.

    REST API example

    Let’s start the REST API side of this comparison by defining our Customer model class shown in Figure 2:

    Customer model class definition.
    Figure 2: Customer model class definition.

     

    Next, we need the controller, which exposes a Customer REST endpoint and handles the request/response process. Let's define three endpoints (current, desired, and reconcile) and a CustomerReconciler service that handles the business logic shown in Figure 3:

    CustomerReconciler service definition.

     

    Start your Spring Boot application by entering:

    $ mvn spring-boot:run

    This command starts the application on the default port.

    Let’s get the current state of our customers using the values from this demo. In general, we fetch this information from other APIs or databases, but for this example Figure 4 shows the current state of our Kubernetes pods:

    Artificially created current pod states
    Figure 4: The artificially-created current pod states.

     

    Now, let’s try to update the state of our Customer with the desired state using the POST method and JSON data in Figure 5:

    The JSON data.
    Figure 5: The JSON data.

     

    In Kubernetes, updating the state from current to desired is handled by the reconcile endpoint, which shows the difference between the current state and the desired state. We defined our business logic to handle this state in the reconciler loop shown in Figure 6:

    The reconciler state data.
    Figure

    Now we have a Customer API that can get the customer's current state, update the state, and reconcile it to handle the change.

    Custom operator example

    Now let's see how to implement a similar API by extending the Kubernetes API server using the Operator SDK. First, generate the Operator:

    $ operator-sdk new customer-api-operator

    This action generates basic boilerplate code for our Customer API Operator shown in Figure 7:

    Basic boilerplate code for our new API Operator.

     

    Start by adding the model data type (i.e., Customer), where kind is an entity like a pod, node, deployment, etc. Next, add a custom API for the Kubernetes platform, which requires generating a new Custom Resource Definition (CRD) with an API version:

    $ operator-sdk add api --api-version=akoserwal.io/v1alpha1 --kind=Customer

    This command generates a basic scaffolding for our Customer API. Half of the work for defining a basic model is now already done by the SDK. You can look into the file customer_types.go (the same as your model class, customer.java, defined in the first example).

    In Figure 8, you can see that Spec and Status are already pre-defined with the generated Customer struct and that we add the new entities from the Customer model class (i.e., FirstName, LastName, and Email):

    The pre-defined Customer struct.
    Figure 8: The pre-defined Customer struct.

     

    After modifying customer_types.go, run these commands for deep copy and generating an OpenAPI spec for the Customer API:

    $ operator-sdk generate k8s
    $ operator-sdk generate openapi

    Now, generate the controller:

    $ operator-sdk add controller --api-version=akoserwal.io/v1alpha1 --kind=Customer
    

    The generated boilerplate controller code adds our Customer API schema to the Kubernetes schema. Next, we add a couple of watchers to look out for any changes or events, which triggers the reconciler loop for handling the changes as you can see in Figure 9:

    The controller boilerplate code.
    Figure 9: The controller boilerplate code.

     

    We can relate this result to the Spring Boot example's CustomerController and reconciler endpoint. Although, this version does more than our Spring Boot controller.

    You can see the simplified flow in Figure 10:

    The simplified API flow.
    Figure 10: The simplified flow.

     

    So, to reiterate, creating a custom Operator—instead of using a REST API in your chosen framework—flows as follows:

    1. Define a namespace for our operator (i.e.,Customer).
    2. Create the Customer Resource Definition (CRD), so define the "kind" (i.e., the customer as a resource in OpenShift/Kubernetes).
    3. Let the controller analyze the CRD and add that information to the Kubernetes API schema.
    4. Expose a new API endpoint for the CRD.
    5. Add watchers for the namespace to observe.
    6. Run a process/loop (i.e., reconciler loop) for acting based on the desired change.
    7. Store the state in the EtcD.

    Now that the custom Operator is defined, let's continue. Deploy the Operator:

    $ crc start

    Log into the CRC instance:

    $ oc login -u kubeadmin -p <secret>

    Create the custom CRD:

    $ oc create -f deploy/crds/customer.github.io_customers_crd.yaml

    Add the roles, role_binding, and service account for the customer. These settings define the permissions and rules regarding access to our Operator:

    $ kubectl create -f deploy/role.yaml
    $ kubectl create -f deploy/role_binding.yaml
    $ kubectl create -f deploy/service_account.yaml
    

    Run our Operator locally:

    $ export NAMESPACE=customer
    $ operator-sdk up local

    Now our controller is in the running state. It watches the NAMESPACE for any changes.

    Create an instance of the CRD by creating a Custom Resource object:

    $ oc create -f deploy/crds/customer.github.io_v1alpha1_customer_cr.yaml

    Once created, the CR looks like this:

    apiVersion: customer.github.io/v1alpha1
    kind: Customer
    metadata:
      name: customer-api
    spec:
      # Add fields here
      size: 3
    firstName: Abhishek
    lastName: Koserwal
    email: ak..@redhat.com

    When we create a CR object, an event is triggered that gives control to the reconciler loop. Reconcile reads the state of that cluster for a custom object and makes changes based on the state it reads.

    Figure 11 shows the Customer Custom Resource in the OpenShift UI:

    The new Custom Resource in the OpenShift API.
    Figure 11: The new Custom Resource in the OpenShift API.

     

    Figure 12 shows the Customer Custom Resource in its raw API format:

    The new Custom Resource as a raw API.
    Figure 12: The new Custom Resource as a raw API.

     

    Based on the logic inside our reconciler loop, this CR object creates a pod for the instance customer-api:

    // Define a new Pod object
    pod := newPodForCR(instance)

    Figure 13 shows such an instance:

    Additional update logic.
    Figure 14:

     

    Now, we can add update logic for defining the desired state for our existing Custom Resource. Let’s update the reconciler so it will update our CR (firstName, lastName, and Email) as shown in Figure 14:

    customer_controller.go [func (r *ReconcileCustomer) Reconcile]

    Additional update logic.
    Figure 14:

     

    Re-run the Operator:

    $ operator-sdk up local

    Finally, in Figure 15, we can see that the reconciler has updated the desired state for the Custom Resource:

    The updated desired state.
    Figure 15:

    Conclusion

    Operator patterns allow you to use Kubernetes platform capabilities to run custom business logic. These patterns are powerful and go beyond what I have covered here. In this article, I focused on Kubernetes internals and Operator patterns with a relatable approach via the REST API. Writing these types of extensions is not the best use case for Operator patterns. You can run a Spring Boot—or any other language—application in a container for use cases such as "building a REST API application/service." For actual use cases, visit operatorhub.io.

    Thank you for reading, I hope you find this article helpful. You can find the code here. Happy coding.

    References

    • operator-sdk 
    • awesome-operators
    • operator-framework
    • getting-started
    • Operator
    • OperatorHub
    Last updated: September 19, 2023

    Related Posts

    • Where can I find REST API client libraries for OpenShift?

    • How to call the OpenShift REST API from C#

    • Where can I find REST API client libraries for OpenShift?

    Recent Posts

    • A day in the life of an NVIDIA H100 on Red Hat OpenShift 5

    • Migrate BuildConfigs to builds for Red Hat OpenShift with one plug-in install

    • 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

    What’s up next?

    The Red Hat OpenShift Sandbox is a platform for developers, testers and operations to build, test and deploy applications using container technology. It's a great place to get started with Kubernetes.

    Learn by doing
    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