Using Java Kubernetes Clients to Perform CRUD Operations on Config Map Resources.

Photo by Maximilian Weisbecker on Unsplash

In this blog post we will look at how we can use the fabric 8’s Kubernetes client for java and official Kubernetes client for java to retrieve, create, update and delete a config map resource type.

Using Fabric 8’s Kubernetes Client

Configuring the client

This client can be configured using System properties, environment variables, a kubeconfig file, and service account token & mounted CA certificate. The properties that can be configured using properties and environment variables can be found here.

In order to perform CRUD operations we need to first create a client.

var client = new DefaultKubernetesClient();

We will use the following two variables to store the namespace (aka virtual cluster — the default namespace is “default”) which contains the config map resource and the name of the config map resource.

var namespace = "default";
var name = "CONFIGMAP_NAME";

Creating a Config Map Resource.

var configMapResource = client.configMaps()
.inNamespace(namespace)
.withName(name);
var configMap = configMapResource.createOrReplace(new ConfigMapBuilder().
withNewMetadata().withName(name).endMetadata().
addToData("key", "value").
build());

Retrieve a Config Map Resource

var configMap = client.configMaps()
.inNamespace(NAMESPACE)
.withName(name)
.get().

Updating a Config Map Resource

client.configMaps().inNamespace(namespace).withName(name)
.edit(
c -> new ConfigMapBuilder(c).addToData("NEW_KEY","NEW_VALUE")
.build()
);

Deleting a Config Map Resource

client.configMaps().inNamespace(namespace).withName(name)
.delete();

Using the Official Kubernetes Java client

We can perform the same operations easily using the official kubernetes API client for java. It also has a similar fluent API to the one in fabric 8 client, in addition to being more flexible.

Configuring the client

Since we are using the standard client builder, it can be configured as follows (in the order of priority),

  • If $KUBECONFIG is defined, use that config file.
  • If $HOME/.kube/config can be found, use that.
  • If the in-cluster service account can be found, assume in cluster config.
  • Default to localhost:8080 as a last resort.

If we are not using the standard client builder, we can use the kubeconfig builder as follows.

var kubeConfigPath = "<path-to-kube-config-file">
var apiClient = ClientBuilder
.kubeconfig(KubeConfig.loadKubeConfig(
new FileReader(kubeConfigPath)
)).build();

Creating a Config Map

The following code demonstrates how the fluent api can be used to create a new config map.

var apiClient = ClientBuilder.standard().build();
var api = new CoreV1Api(apiClient);

var configMap = new V1ConfigMapBuilder()
.withNewMetadata()
.withName(name)
.endMetadata()
.addToData("KEY", "VALUE")
.build();
api.createNamespacedConfigMap(
namespace,
name,
null, null, null);

Retrieve a Config Map Resource

var apiClient = ClientBuilder.standard().build();                                      var api = new CoreV1Api(apiClient);var configMapClient = new GenericKubernetesApi<>(
V1ConfigMap.class,
V1ConfigMapList.class, "", "v1", "configmaps",
apiClient
);
var response = configMapClient.get(namespace, namespace); var configmap = response.getObject();

Editing a Config Map Resource

There are different ways to do this. Here I will show how to update a config map using JSON patch as it allows granular updates.

var apiClient = ClientBuilder.standard().build();
var api = new CoreV1Api(apiClient);
var jsonPatchStr = "[{
\"op\" : \"replace\",
\"path\":\"/data/KEY\",
\"value\": \"CHANGED\"
}]";
var configMap = PatchUtils.patch(
V1ConfigMap.class,
() -> api.patchNamespacedConfigMapCall(
name,
namespace,
new V1Patch(jsonPatchStr),
null,
null,
null,
null,
null),
V1Patch.PATCH_FORMAT_JSON_PATCH,
api.getApiClient()
);

This will update the config map’s “KEY” key with the value “CHANGED”.

Deleting a Config Map using name

var apiClient = ClientBuilder.standard().build();
var api = new CoreV1Api(apiClient);
api.deleteNamespacedConfigMap(
"<config-map-name-here>",
"<namespace-here>",
null, null, null, null, null, null);

You can learn more about these two libraries by going to their GitHub repositories linked below.

Resources

--

--

--

Undergraduate at Department of Computer Science and Engineering, University of Moratuwa

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

DeHorizon & 0xAdventure 🎁🎁🎁

My Story: Before and after getting selected as Recipient of GoogleUdacityScholarshipIndia.

Logic App Standard — Working with Flat Files

Our favorite Slack integrations

MatrixETF Obtains Strategic Investment from Solana Ecological Fund

How to deploy an SPA on CloudFront

OOP — Pt. 3 — Object Relationships

Go bits: Magic with functions

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thamidu Muthukumarana

Thamidu Muthukumarana

Undergraduate at Department of Computer Science and Engineering, University of Moratuwa

More from Medium

WSL2 for Java & Kubernetes development

How to connect to multiple RabbitMQ instances using Spring Cloud Stream

How To Manage Multi JAVA Versions On Mac

How To Configure and Deploy WebApps with Wildfly Application Server in IntelliJ IDEA