脚本如下
#!/usr/bin/env bash
[[ -n $DEBUG ]] && set -x
set -eou pipefail
useage() {
cat <<HELP
USAGE:
kubenv.sh TYPE NAME [OPTS]
HELP
}
exit_err() {
echo >&2 "${1}"
exit 1
}
if [ $# -lt 2 ]; then
useage
exit 1
fi
TYPE=$1
NAME=$2
shift 2
OPTS=$@
cm_pipline(){
sed -e 's/^map[//g' | sed -e 's/]$//g' | sed -e 's/ /
/g' | sed -e 's/:/=/' | sed -e 's/^/export /g'
}
secret_pipline(){
sed -e 's/^map[//g' | sed -e 's/]$//g' | sed -e 's/ /
/g' | awk -F ":" '{printf "export %s=", $1;cmd=sprintf("echo %s | base64 -d", $2);system(cmd);print "";}'
}
if [ x"${TYPE}" == x"secret" ]; then
kubectl ${OPTS} get "${TYPE}" "${NAME}" -o jsonpath="{.data}" | secret_pipline
else
kubectl ${OPTS} get "${TYPE}" "${NAME}" -o jsonpath="{.data}" | cm_pipline
echo ""
fi
使用方法
kubenv.sh TYPE NAME [OPTS]
configmap举例
❯ kubectl create configmap some-config --from-literal=NAME=futuretea
configmap/some-config created
❯ kubenv cm some-config
export NAME=futuretea
❯ kubectl create secret generic some-secret --from-literal=PASSWORD=123456
secret/some-secret created
❯ kubenv secret some-secret
export PASSWORD=123456