Skip to content

Instantly share code, notes, and snippets.

@rjchicago
Created February 2, 2023 19:04
Show Gist options
  • Save rjchicago/a508978b13676d9db5d194d8d56dad39 to your computer and use it in GitHub Desktop.
Save rjchicago/a508978b13676d9db5d194d8d56dad39 to your computer and use it in GitHub Desktop.
Function to read and format K8s ConfigMap data from file
# Function to read and format ConfigMap data from file
configmap_from_file() {
local FILE=${1:?FILE is required}
local INDENT=${2:-4}
local SPACES=$(printf ' %.0s' {1..$INDENT})
echo "|" && cat $FILE | sed "s/^/$SPACES/"
}
##################################################
# usage
# create nginx.conf.template
echo "server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location = /health {
access_log off;
add_header 'Content-Type' 'text/plain';
return 200 'OK';
}
}" > nginx.conf.template
# export configmap_from_file
export NGINX_CONF=$(configmap_from_file nginx.conf.template)
# kubectl apply
echo "apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-conf
data:
nginx.conf.template: ${NGINX_CONF}"
| envsubst | kubectl apply -f -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment