Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save content of a configmap to a file with kubectl and jsonpath?

I'm trying to save the contents of a configmap to a file on my local hard drive. Kubectl supports selecting with JSONPath but I can't find the expression I need to select just the file contents.

The configmap was created using the command

kubectl create configmap my-configmap --from-file=my.configmap.json=my.file.json

When I run

kubectl describe configmap my-configmap

I see the following output:

Name:         my-configmap 
Namespace:    default 
Labels:       <none> 
Annotations:  <none>

Data
==== 
my.file.json:
---- 
{
    "key": "value" 
} 
Events:  <none>

The furthest I've gotten so selecting only the file contents is this:

 kubectl get configmap my-configmap -o jsonpath="{.data}"

Which outputs

map[my.file.json:{
    "key": "value"
}]

The output that I want is

{
  "key": "value"
}

What is the last piece of the JSONPath puzzle?

like image 395
PeterH Avatar asked Aug 09 '18 13:08

PeterH


People also ask

How do I create a ConfigMap file?

You can use kubectl create configmap to create a ConfigMap from multiple files in the same directory. When you are creating a ConfigMap based on a directory, kubectl identifies files whose basename is a valid key in the directory and packages each of those files into the new ConfigMap.

What command can create a ConfigMap object called app config with the contents of the file app config Yaml?

Mapping keys from ConfigMaps to pods There, we will define the ConfigMaps as YAML files and then use the kubectl create command to generate the ConfigMaps. In this configuration, we are mapping environmental variables to values within each ConfigMap.

How do I create a ConfigMap Yaml file?

The simplest way to create a ConfigMap is to store a bunch of key-value strings in a ConfigMap YAML file and inject them as environment variables into your Pods. After that, you can reference the environment variables in your applications using whatever methods is necessary for your programming language.

How to create a configmap using kubectl?

There can be two possible scenarios to create your ConfigMap. You already have dp-app.conf file with some content and you want to create ConfigMap using existing file. In this scenario we will use kubectl input arguments to create a configmap using an existing file.

Can I select just the file contents with jsonpath in kubectl?

Kubectl supports selecting with JSONPath but I can't find the expression I need to select just the file contents. I see the following output: The furthest I've gotten so selecting only the file contents is this: What is the last piece of the JSONPath puzzle? Show activity on this post.

How to create a configmap in Kubernetes using YAML?

For example, to create a ConfigMap under the name example-configmap from the example-configmap.yaml file, you would run: Kubernetes allows creating a ConfigMap from one or multiple files in any plaintext format (as long as the files contain key-value pairs).

How do I access my Kubernetes config values?

The Pod mounts the volume to /etc/config-data. Your containers can read the files within the directory to access your config values. Each ConfigMap key will have its own file within the mount point. As a ConfigMap is a standard Kubernetes API resource, you can update values at any time by modifying your manifest and re-applying it to your cluster.


Video Answer


2 Answers

There’s an open issue at the Kubernetes GitHub repo with a list of things that needs to be fixed in regards to kubectl (and JSONpath), one of them are issue 16707 jsonpath template output should be json.

Edit:

How about this:

kubectl get cm my-configmap -o jsonpath='{.data.my\.file\.json}'

I just realized i had answered another question related (kind of) to this one. The above command should output what you had in mind!

like image 159
mikejoh Avatar answered Sep 17 '22 13:09

mikejoh


If you have the ability to use jq, then you can use the following approach to e.g. "list" all config maps by selector, and extract the files:

readarray -d $'\0' -t a < <(kubectl get cm -l grafana=dashboards -o json | jq -cj '.items[] | . as $cm | .data | to_entries[] | [ ($cm.metadata.name + "-" + .key), .value ][]+"\u0000"') ; count=0; while [ $count -lt ${#a[@]} ]; do echo "${a[$((count + 1))]}" > ${a[$count]}; count=$(( $count + 2)); done

This uses kubectl (using -l for a label selector) to get all configmaps. Next it pipes them through jq, creating key value pairs with a null byte termination (the key also contains the name of the configmap, this way I ensured that duplicate file names are not an issue). Then it reads this into a bash array, iterating over the array in steps of 2. Creating files with the content.

This also works file config map values that contain newlines.

like image 39
ctron Avatar answered Sep 17 '22 13:09

ctron