Skip to main content
  1. DevOps Landing Page/

k8s: Extract Configmap or Secret to Env File

·2 mins· ·
Blog En Devops Kubernetes
Liu Zhe You
Author
Liu Zhe You
Skilled in full-stack development and DevOps, currently focusing on Backend.
Table of Contents

Kubernetes Cheat Sheet:
Extract ConfigMap or Secret to .env file
#

Extract from ConfigMap
#

kubectl get configmap my-map --output json | jq '.data' | jq -r 'to_entries | map(.key + "=" + (.value)) | .[]' >> .env

Explanation:

kubectl get configmap my-map --output json |
    # Extract the data section.
    jq '.data' |
    # Replace each "key": "value" pair with "key=value"
    jq -r 'to_entries | map(.key + "=" + (.value)) | .[]' >> .env

Extract from Secret
#

kubectl get secret my-secret --output json | jq '.data' | jq 'map_values(@base64d)' | jq -r 'to_entries | map(.key + "=" + (.value)) | .[]' >> .env

Explanation:

kubectl get secret my-secret --output json |
    # Extract the data section.
    jq '.data' |
    # Decode the value of each keys.
    jq 'map_values(@base64d)' |
    # Replace each "key": "value" pair with "key=value"
    jq -r 'to_entries | map(.key + "=" + (.value)) | .[]' >> .env

Extract from helm
#

# Extract the values with a `awk` script: we print everything starting from the line that contains only `configmap` until the first empty line.
awk '{if ($0 ~ /^configmap:$/) {triggered=1;}if (triggered) {print; if ($0 ~ /^$/) { exit;}}}' "./project/values.yaml" |
    # Keep only the indented lines that contains our configuration values.
    grep '^  ' |
    # Transform key: value into key=value
    sed 's/  //;s/: /=/' >> .env

Refenece
#

Related

NCKU CSIE Freshman First Semester
·5 mins
Blog En
What did I do in the first semester of freshman year?
Other Test Article
New-Article En
Test Article
New-Article Backend En
Backend
Links to backend-related articles
Internship
Links to internship-related articles
Notes
Links to my notes