Kubernetes API for configmap

3/9/2020

I could get the kubectl working for creating configmap with a set of files with below command

kubectl create configmap configmap2 --from-file foldername

where foldername contains files with keys and values

Now I want to create a K8s config map with the same folder using Java. I dont want to use any libraries. Is there any way I can achieve this?

-- vidya k.s.
configmap
creation
java
kubectl
kubernetes

2 Answers

3/9/2020

Yes you can do that but it is not that easy - you will have to:

  • List all files (you can use java.nio.file.Files)
  • Create base64 representation of the files (use java.util.Base64 for this)
  • Create a JSON representing the files and the meta-data (you will have to use String concatenation for this). The JSON should look like the output of:

    kubectl create configmap configmap2 --from-file foldername --dry-run -o json
  • Post that to the Kubernetes API Server with the correct authentication (using the new HTTP client)

It would be easier to use a Kubernetes Java client (e.g.: fabric8io client, official Kubernetes client).

-- koe
Source: StackOverflow

3/9/2020

Here is the API request that you have to make to the Kubernetes API server to create a ConfigMap:

POST /api/v1/namespaces/{namespace}/configmaps

The data must be a ConfigMap JSON object as specified here.

If you don't use a library, you must also include the correct credentials in the request, which is usually a token (Authorization: Bearer <token>) or a certificate.

Tip:

You can use kubectl create configmap -v 10 configmap2 --from-file foldername (note -v 10) to see the exact HTTP request that kubectl makes to the API server.

-- weibeld
Source: StackOverflow