Skip to content

Instantly share code, notes, and snippets.

@ianmuscat
Last active February 6, 2019 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianmuscat/9ee0325ae05e738ffee29f234cfc138f to your computer and use it in GitHub Desktop.
Save ianmuscat/9ee0325ae05e738ffee29f234cfc138f to your computer and use it in GitHub Desktop.
dehelm
#!/bin/bash
source "$(dirname "$0")/include-color.sh"
source "$(dirname "$0")/include-log.sh"
function usage {
echo -e "
Usage: ${0#\.\/} [ARGUMENT...][OPPERAND]
Download, extract and render the contents of a Helm chart into to an opinionated,
Kustomize-friendly directory structure
Options:
-h, --help print this help message
Arguments:
-c, --chart Helm chart to fetch
-n, --namespace Kubernetes namespace to render the Helm chart to
-o, --overlays comma-separated list of overlay directories to create (e.g. dev,test,prod)
-r, --repo chart repository url where to locate the requested chart
-v, --version chart version
-b, --base location of the 'base' directory (overrides -n, --namespace)
"
exit
}
function main {
# check that a Helm chart has been specified
if [ -z "$dehelm_chart" ]; then
log "ER" "missing arguments '-c, --chart'"
usage
exit 1
fi
# set defaults if not passed
if [ -z "$dehelm_namespace" ]; then
dehelm_namespace="default"
fi
if [ -z "$dehelm_repo" ]; then
dehelm_repo="https://kubernetes-charts.storage.googleapis.com"
fi
if [ -z "$dehelm_version" ]; then
dehelm_version=$(helm inspect chart --repo ${dehelm_repo} ${dehelm_chart} | grep -w "version:" | cut -d ' ' -f 2)
log "IN" "assuming latest major Helm chart version '${dehelm_version}'"
fi
if [ -z "$dehelm_base" ]; then
dehelm_base="../manifests/${dehelm_namespace}/${dehelm_chart}/base"
fi
# make a '.helm' directory to serve as a working directory and store
# the chart's 'values.yaml' file
mkdir -p ${dehelm_base}/.helm
# fetch the Helm chart and extract it
helm fetch \
--untar \
--untardir ${dehelm_base}/.helm \
--repo ${dehelm_repo} \
--version ${dehelm_version} \
${dehelm_chart}
if [ $? -ne 0 ]; then
log "ER" "failed to fetch Helm chart"
exit 1
fi
# if a 'values.yaml' file does not already exist, copy it from the Helm chart
if [ ! -f "${dehelm_base}/.helm/values.yaml" ]; then
cp ${dehelm_base}/.helm/${dehelm_chart}/values.yaml ${dehelm_base}/.helm/values.yaml
else
log "WR" "not overwriting 'values.yaml' found at '${dehelm_base}/.helm/values.yaml'"
fi
# render the Helm chart to 'kubectl' compatible YAML
helm template \
--values ${dehelm_base}/.helm/values.yaml \
--output-dir ${dehelm_base} \
${dehelm_base}/.helm/${dehelm_chart}
mv ${dehelm_base}/${dehelm_chart}/templates/* ${dehelm_base}/
cd ${dehelm_base} # try to prevent nasty accidents with `rm -rf`
rm -rf ${PWD}/${dehelm_chart}
cd - >/dev/null # bounce back to where we were before
# create Kustomize directory structure
mkdir -p ${dehelm_base}/.kustomize/patches
cat << EOF > ${dehelm_base}/.gitignore
/*
!.gitignore
!.helm/
.helm/*
!.helm/values.yaml
!.kustomize
!kustomization.yaml
EOF
cat << EOF > ${dehelm_base}/kustomization.yaml
apiVersion: v1beta1
kind: Kustomization
namespace: ${dehelm_namespace}
EOF
# create overlays
if [ ! -z "$dehelm_overlays" ]; then
for overlay in $(echo $dehelm_overlays | tr ',' '\n'); do
log "IN" "creating overlay '${overlay}'"
mkdir -p ${dehelm_base}/../overlays/${overlay}
#TODO: Automatically add kustomization.yaml with base (../base) defined
done
fi
}
# check that 'helm' is installed
if ! which helm >/dev/null; then
log "ER" "could not find 'helm' installation"
fi
# show usage if no arguments are passed
if [ "$1" = "" ]; then
usage
fi
# command line parameters
while [ "$1" != "" ]; do
case $1 in
-c | --chart ) shift
dehelm_chart=$1
;;
-n | --namespace ) shift
dehelm_namespace=$1
;;
-o | --overlays ) shift
dehelm_overlays=$1
;;
-r | --repo ) shift
dehelm_repo=$1
;;
-v | --version ) shift
dehelm_version=$1
;;
-b | --base ) shift
dehelm_base=$1
;;
-h | --help | * ) usage
esac
shift
done
main
#!/bin/bash
# ANSI color codes
IN="\033[36m" # Cyan
ER="\033[31m" # Red
WR="\033[33m" # Yellow
OK="\033[32m" # Green
DB="\033[35m" # Purple
NC="\033[0m" # No color
#!/bin/bash
function log {
TYP=$1 # ER, WR, OK
MSG=$2
NOW=$(date +"%m-%d-%Y %T")
case "$TYP" in
"IN")
echo -e "${IN}${0#\.\/}: [${NOW}] INFO ${MSG}${NC}"
;;
"ER")
echo -e "${ER}${0#\.\/}: [${NOW}] ERROR ${MSG}${NC}"
;;
"WR")
echo -e "${WR}${0#\.\/}: [${NOW}] WARNING ${MSG}${NC}"
;;
"OK")
echo -e "${OK}${0#\.\/}: [${NOW}] SUCCESS ${MSG}${NC}"
;;
"DB")
echo -e "${DB}${0#\.\/}: [${NOW}] DEBUG ${MSG}${NC}"
;;
*)
echo -e "${DB}${0#\.\/}: [${NOW}] DEBUG Something went wrong when trying to print this error:
>>> TYP(${TYP}),
>>> MSG($MSG),
>>> NOW(${NOW})${NC}"
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment