Kubernetes Client for Java: Introducing YAKC


Introduction

As some of you may know, I’m a proud member of the team maintaining Fabric8 Kubernetes Client (for a while now). Fabric8 is one of the most popular Java clients for Kubernetes and OpenShift. Fabric8 Kubernetes Client is great because it allows to perform most of the kubectl supported operations from Java with a very neat and fluent API. However, accessing low-level REST API operations or specific API versions is hard because some of these decisions are taken for you (for now). This is the main reason that drove me to implement Yet Another Kubernetes Client (YAKC) as a side project during these Easter holidays.

Fabric8’s Kubernetes client aim is to provide a complete substitute of kubectl for Java and it’s mostly a port for Java of the official client-go. On the other hand, YAKC is a declarative Java REST client for the Kubernetes API. YAKC provides access to the documented OpenAPI and model definitions with some  helpers to detect the security configuration and extensions for non-documented features. It also provides a reactive interface to access resources and support for Streams.

YAKC – Yet Another Kubernetes Client

Now let’s see how you can make use of YAKC to perform some basic operations. You can also check the quickstarts folder in the repository for a list of increasing example projects.

Setup

The client is separated into several modules that provide support for future extensibility. In order to be able to access the complete features of the project you will need to depend on kubernetes-client and kubernetes-api modules.

Maven

Gradle

Once your project is setup, getting an instance of the client with autodetected settings is relatively easy:

You can also provide a manual configuration for the client if you don’t want to use autodetect features:

Resource object handling

Although the project doesn’t provide a completely fluent interface, writing a model object is relatively simple and effort-less as there are builders available for all Model types.

The following YAML resource definition:

can be easily implemented as:

Basic examples

We can now see some examples describing how to perform some simple operations:

Create a Pod

The following code creates a pod in the “default” namespace with a label, an annotation and a single container with containous/whoami image.

List Pods in all namespaces

The following code will list Pods in all namespaces using the Java 8 Streams interface and print their names to the standard output.

Watch Pod resources in all namespaces

This example highlights how we can use the ReactiveX Observable class to watch some resources applying client-side filtering and stopping the subscription once the first MODIFIED event type is received.

Conclusion

In this post I’ve shown you how to perform a basic setup of a Java project to make use of YAKC (Yet Another Kubernetes Client). I also included some code snippets describing how to perform basic operations in our cluster.

More posts are coming documenting and describing what you can already see in the Quick start projects section of the GitHub repository.

YAKC - Yet Another Kubernetes Client

Leave a comment

Your email address will not be published. Required fields are marked *