Using Java Kubernetes Clients to Perform CRUD Operations on Config Map Resources.
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.