fabric8io Kubernetes Client

repository·main·Indexed 25 days ago

https://github.com/fabric8io/kubernetes-client

A comprehensive Java client for interacting with Kubernetes and OpenShift REST APIs using a fluent DSL. It supports extensions for cloud-native technologies including Istio, Tekton, and Knative. The project includes tools such as the bom-generator-plugin for Maven, a CRD-Generator CLI and annotation processor for creating Custom Resource Definitions, and a framework for running Chaos Mesh tests to verify SharedInformer resiliency.

Tokens
78.7K
Snippets
137
Records
258
Agent score
87%

What's inside fabric8io-kubernetes-client

  1. Overview of Kubernetes & OpenShift Java Client

    main
    The Fabric8 Kubernetes & OpenShift Java Client provides access to the full Kubernetes and OpenShift REST APIs using a fluent Domain Specific Language (DSL). It is available in several modules and extensions for specific ecosystems like Knative, Tekton, Istio, and more.
  2. Overview of Vert.x Client for Fabric8

    main
    The httpclient-vertx module provides a Vert.x-based HTTP client implementation for the Fabric8 Kubernetes Client. This allows developers to leverage the non-blocking, reactive capabilities of the Vert.x ecosystem when interacting with Kubernetes or OpenShift APIs through the Fabric8 client.
  3. Understand Chaos Mesh test flow for SharedInformer

    main

    The chaos tests simulate network disruptions to ensure SharedInformers recover appropriately. The test flow involves two main components:

    • Checker: A Pod running a SharedInformer on a specific ConfigMap. It waits for a counter in that ConfigMap to reach a target value N.
    • Control: A Pod that increments the counter in the ConfigMap up to the target value N.

    The Experiment:

    1. The Checker and Control applications start.
    2. A Chaos Experiment (using 3 different network disruption scenarios) is launched targeting the Checker app.
    3. The experiment runs for 12 minutes.
    4. Success Criteria: After the experiment, the Checker's SharedInformer must recover, reach the target value N, and both the Control and Checker Pods must reach a Successful state.
  4. Understand the Non-Blocking Kubernetes Client API design

    main

    The Fabric8 Kubernetes Client is undergoing research for a non-blocking API to support callers with non-blocking dispatchers (such as event loops or reactive pipelines).

    Key Design Principles:

    • Return Types: The preferred public return type for asynchronous operations is CompletionStage<T> rather than CompletableFuture<T>.
    • Usage Recommendation: If you are using Java 21+ with a virtual thread executor and simply need concurrent simple calls, it is recommended to continue using the existing blocking API. The async surface is specifically intended for reactive/non-blocking environments.
    • Current Limitations: The async API is considered "best-effort" until certain blocking components (like the OIDC interceptor) are addressed.
  5. Understand the non-blocking capabilities of the Kubernetes Client

    main

    The Kubernetes Client is internally non-blocking from the HTTP layer down. All supported HttpClient implementations (JDK, OkHttp, Jetty, Vert.x) use asynchronous operations. However, the public DSL terminal methods (e.g., get, list, create, update, delete, patch) are currently blocking because they call OperationSupport.waitForResult(...), which invokes future.get() on the internal asynchronous result.

    While the main DSL is blocking, some specific asynchronous public APIs already exist:

    • Watchable.streamingList(Consumer<T>) returns CompletableFuture<String>
    • Informable.informOnCondition(...) returns CompletableFuture<List<T>>
    • SharedIndexInformer.start()/stopped() returns CompletionStage<Void>
    • ExecWatch.exitCode() returns CompletableFuture<Integer>
    • LogWatch.onClose() returns CompletionStage<Throwable>
    • WebSocket.Builder.buildAsync(...) is available for WebSocket connections.
  6. Understand the blocking nature of the Kubernetes Client DSL

    main

    Most public terminal methods in the Kubernetes Client DSL (e.g., get(), list(), create(), patch()) are synchronous and blocking. This is because they funnel through OperationSupport.waitForResult, which calls future.get() on an internal CompletableFuture.

    While the underlying HTTP backends (JDK, OkHttp, Jetty, and Vert.x) are natively asynchronous, the high-level DSL is designed to be blocking for ease of use. If you require non-blocking behavior, you must look for specific async-shaped methods or use alternative patterns like Virtual Threads.

  7. Use CRD Generator v2 for Custom Resource Definitions

    main

    The CRD Generator v2 is the current recommended way to generate Kubernetes Custom Resource Definitions (CRDs) from Java classes. It is based on Jackson/jsonSchema and consists of several modules depending on your build environment or execution method.

    Available modules for v2:

    • CRD Generator API v2 (io.fabric8:crd-generator-api-v2): The core implementation.
    • CRD Generator Collector (io.fabric8:crd-generator-collector): Used to find and load compiled Custom Resource classes from directories and Jar files.
    • CRD Generator Maven Plugin (io.fabric8:crd-generator-maven-plugin): For automatic generation during the Maven build process.
    • CRD Generator CLI (io.fabric8:crd-generator-cli): A standalone command-line tool for manual generation.
  8. Install the CRD-Generator CLI

    main

    You can install the CRD-Generator CLI by downloading the shell script from Sonatype or by using jbang.

    To download and install the latest version via shell script:

    1. Fetch the latest version tag from GitHub.
    2. Download the .sh file from Sonatype.
    3. Make the file executable.

    Alternatively, if you have jbang installed, you can run the CLI directly without a manual installation.

    # Download and install via shell script
    export VERSION=$(wget -q -O - https://github.com/fabric8io/kubernetes-client/releases/latest --header "Accept: application/json" | jq -r '.tag_name' | cut -c 2-)
    wget -O crd-gen https://oss.sonatype.org/content/repositories/releases/io/fabric8/crd-generator-cli/$VERSION/crd-generator-cli-$VERSION.sh
    chmod a+x crd-gen
    ./crd-gen --version
    
    # Or run via jbang
    jbang io.fabric8:crd-generator-cli:<version>
  9. Understand Jandex indexing for CRD generation

    main

    The plugin uses Jandex to find Custom Resource classes. A class is identified if it:

    1. Implements the HasMetadata interface (the abstract class CustomResource implements this).
    2. Is annotated with both @Group and @Version.

    Important: If your Custom Resource extends a custom abstract class or interface that indirectly implements HasMetadata, ensure that intermediate class is included in the scan/index.

    To improve performance, the plugin will use existing serialized Jandex indices found in the project or dependencies. To force a new index creation, set forceIndex to true.

  10. Configure Java Generation with Gradle

    main

    To use the Java generator in Gradle, apply the io.fabric8.java-generator plugin. Use the javaGen closure to specify the source directory. If using extraAnnotations, you must explicitly configure annotationProcessor and compileOnly dependencies for builder-annotations and lombok. You can trigger the generation using the crd2Java task.

    plugins {
      id 'io.fabric8.java-generator' version "${kubernetesClientVersion}"
    }
    
    dependencies {
      // extraAnnotations requires explicit annotation processor configuration
      annotationProcessor "io.sundr:builder-annotations:${sundrioVersion}"
      compileOnly         "io.sundr:builder-annotations:${sundrioVersion}"
      compileOnly         "org.projectlombok:lombok:${lombokVersion}"
    }
    
    javaGen {
      source = file('src/main/resources/kubernetes')
    }
    
    // Run via command line:
    // gradle crd2Java
  11. Configure project dependencies for Fabric8 Kubernetes Client

    main

    Depending on your needs, you can choose between two dependency patterns:

    1. Full Client: Use kubernetes-client or openshift-client as compile dependencies. This is required if you use internal classes.
    2. API-only Compile Time: Use kubernetes-client-api or openshift-client-api for compile dependencies, and kubernetes-client or openshift-client as a runtime dependency. This provides a cleaner compile-time classpath.

    HttpClient Selection: By default, the client uses Vert.x (kubernetes-httpclient-vertx). To use a different implementation (like OkHttp), exclude kubernetes-httpclient-vertx and include your preferred runtime dependency.