fabric8io Kubernetes Client
repository·main·Indexed 25 days ago
https://github.com/fabric8io/kubernetes-clientA 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.
What's inside fabric8io-kubernetes-client
- 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.
Overview of Vert.x Client for Fabric8
mainThehttpclient-vertxmodule 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.Understand Chaos Mesh test flow for SharedInformer
mainThe 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 thatConfigMapto reach a target valueN. - Control: A Pod that increments the counter in the
ConfigMapup to the target valueN.
The Experiment:
- The Checker and Control applications start.
- A Chaos Experiment (using 3 different network disruption scenarios) is launched targeting the Checker app.
- The experiment runs for 12 minutes.
- 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 aSuccessfulstate.
- Checker: A Pod running a SharedInformer on a specific
Understand the Non-Blocking Kubernetes Client API design
mainThe 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 thanCompletableFuture<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.
- Return Types: The preferred public return type for asynchronous operations is
Understand the non-blocking capabilities of the Kubernetes Client
mainThe Kubernetes Client is internally non-blocking from the HTTP layer down. All supported
HttpClientimplementations (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 callOperationSupport.waitForResult(...), which invokesfuture.get()on the internal asynchronous result.While the main DSL is blocking, some specific asynchronous public APIs already exist:
Watchable.streamingList(Consumer<T>)returnsCompletableFuture<String>Informable.informOnCondition(...)returnsCompletableFuture<List<T>>SharedIndexInformer.start()/stopped()returnsCompletionStage<Void>ExecWatch.exitCode()returnsCompletableFuture<Integer>LogWatch.onClose()returnsCompletionStage<Throwable>WebSocket.Builder.buildAsync(...)is available for WebSocket connections.
Understand the blocking nature of the Kubernetes Client DSL
mainMost public terminal methods in the Kubernetes Client DSL (e.g.,
get(),list(),create(),patch()) are synchronous and blocking. This is because they funnel throughOperationSupport.waitForResult, which callsfuture.get()on an internalCompletableFuture.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.
Use CRD Generator v2 for Custom Resource Definitions
mainThe 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.
- CRD Generator API v2 (
Install the CRD-Generator CLI
mainYou 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:
- Fetch the latest version tag from GitHub.
- Download the
.shfile from Sonatype. - Make the file executable.
Alternatively, if you have
jbanginstalled, 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>Understand Jandex indexing for CRD generation
mainThe plugin uses Jandex to find Custom Resource classes. A class is identified if it:
- Implements the
HasMetadatainterface (the abstract classCustomResourceimplements this). - Is annotated with both
@Groupand@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
forceIndextotrue.- Implements the
Configure Java Generation with Gradle
mainTo use the Java generator in Gradle, apply the
io.fabric8.java-generatorplugin. Use thejavaGenclosure to specify the source directory. If usingextraAnnotations, you must explicitly configureannotationProcessorandcompileOnlydependencies forbuilder-annotationsandlombok. You can trigger the generation using thecrd2Javatask.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 crd2JavaMigrate from CRD Generator Annotation Processor to v2
mainThecrd-generator-aptannotation processor is deprecated since version 7.0.0. To continue using CRD generation, you should migrate to the new CRD Generator v2. Please refer to the CRD Generator v2 Migration Guide for instructions on how to replace it.Configure project dependencies for Fabric8 Kubernetes Client
mainDepending on your needs, you can choose between two dependency patterns:
- Full Client: Use
kubernetes-clientoropenshift-clientascompiledependencies. This is required if you use internal classes. - API-only Compile Time: Use
kubernetes-client-apioropenshift-client-apiforcompiledependencies, andkubernetes-clientoropenshift-clientas aruntimedependency. 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), excludekubernetes-httpclient-vertxand include your preferred runtime dependency.- Full Client: Use