vidya k.s.
vidya k.s.

Reputation: 141

Kubernetes API for configmap

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?

Upvotes: 1

Views: 4122

Answers (2)

weibeld
weibeld

Reputation: 15312

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.

Upvotes: 1

derkoe
derkoe

Reputation: 6306

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).

Upvotes: 1

Related Questions