A logo showing the text blog.marcnuri.com
Español
Home»Java»Kubernetes Client for Java: Introducing YAKC

Recent Posts

  • Fabric8 Kubernetes Client 7.2 is now available!
  • Connecting to an MCP Server from JavaScript using AI SDK
  • Connecting to an MCP Server from JavaScript using LangChain.js
  • The Future of Developer Tools: Adapting to Machine-Based Developers
  • Connecting to a Model Context Protocol (MCP) Server from Java using LangChain4j

Categories

  • Artificial Intelligence
  • Front-end
  • Go
  • Industry and business
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • January 2017
  • December 2015
  • November 2015
  • December 2014
  • March 2014
  • February 2011
  • November 2008
  • June 2008
  • May 2008
  • April 2008
  • January 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

Kubernetes Client for Java: Introducing YAKC

2020-05-02 in Java tagged Client / Cloud / Fabric8 / Java / Kubernetes / OpenShift / YAKC by Marc Nuri | Last updated: 2021-12-11
Versión en Español

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

1<dependencies>
2  <dependency>
3    <groupId>com.marcnuri.yakc</groupId>
4    <artifactId>kubernetes-api</artifactId>
5    <version>0.0.25</version>
6  </dependency>
7  <dependency>
8    <groupId>com.marcnuri.yakc</groupId>
9    <artifactId>kubernetes-client</artifactId>
10    <version>0.0.25</version>
11  </dependency>
12</dependencies>

Gradle

1dependencies {
2  compile 'com.marcnuri.yakc:kubernetes-api:0.0.25'
3  compile 'com.marcnuri.yakc:kubernetes-client:0.0.25'
4}

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

1try (KubernetesClient kc = new KubernetesClient()) {
2  /* ... */
3}

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

1final Configuration configuration = Configuration.builder()
2  .server("server-host")
3  .insecureSkipTlsVerify(true)
4  .username("user")
5  .password("password")
6  /* ... */
7  .build();
8try (KubernetesClient kc = new KubernetesClient(configuration )) {
9  /* ... */
10}

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:

1kind: Pod
2apiVersion: v1
3metadata:
4  name: yakc-test
5  labels:
6    app: yakc-kubernetes-client
7  annotations:
8    yakc: rulez!
9spec:
10  containers:
11    - name: yakc-pod
12      image: busybox

can be easily implemented as:

1Pod.builder()
2  .metadata(ObjectMeta.builder()
3    .name("yakc-test")
4    .putInLabels("app", "yakc-kubernetes-client")
5    .putInAnnotations("yakc", "rulez!")
6    .build())
7  .spec(PodSpec.builder()
8    .addToContainers(Container.builder()
9      .name("yakc-pod")
10      .image("busybox")
11      .build())
12    .build())
13  .build();

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.

1kubernetesClient.create(CoreV1Api.class).createNamespacedPod("default", Pod.builder()
2  .metadata(ObjectMeta.builder()
3    .name("yakc-pod-example")
4    .putInLabels("app", "yakc-pod-example")
5    .putInAnnotations("com.marcnuri.yakc", "pod-example")
6    .build())
7  .spec(PodSpec.builder()
8    .addToContainers(Container.builder()
9      .image("containous/whoami")
10      .name("java-test-pod")
11      .build())
12    .build())
13  .build()).get();

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.

1kubernetesClient.create(CoreV1Api.class).listPodForAllNamespaces().stream()
2  .map(Pod::getMetadata)
3  .map(ObjectMeta::getName)
4  .forEach(System.out::println);

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.

1kubernetesClient.create(CoreV1Api.class).listPodForAllNamespaces().watch()
2  .filter(we -> we.getObject().getMetadata().getCreationTimestamp().isAfter(OffsetDateTime.now()))
3  .takeUntil(we -> we.getType() == Type.MODIFIED)
4  .map(WatchEvent::getObject).map(Pod::getMetadata).map(ObjectMeta::getName)
5  .subscribe(System.out::println);

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
YAKC - Yet Another Kubernetes Client
Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation
Access the Kubernetes API from a Pod in JavaHow to Initialize a New Go Project with Modules
© 2007 - 2025 Marc Nuri