Skip to content

Instantly share code, notes, and snippets.

@chancez
Created March 31, 2020 16:38
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 chancez/6be17d741a73bf879278a2e553c41e3d to your computer and use it in GitHub Desktop.
Save chancez/6be17d741a73bf879278a2e553c41e3d to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
DIR="$( cd "$(dirname "$0")" ; pwd -P )"
HELM_BIN=${HELM_BIN:-"helm"}
FAQ_BIN=${FAQ_BIN:-"faq"}
FETCH_GIT_REPO="${FETCH_GIT_REPO:-false}"
if ! [ -x "$(command -v "$HELM_BIN")" ]; then
echo "Error $HELM_BIN is not installed"
exit 1
fi
if ! [ -x "$(command -v "$FAQ_BIN")" ]; then
echo "Error $FAQ_BIN is not installed"
exit 1
fi
if [ $# -ne 7 ]; then
echo "Usage: $0 REPO_URL CHART_NAME CHART_VERSION NAMESPACE RELEASE_NAME VALUES_FILE OUTPUT_DIR"
exit 1
fi
REPO_URL=$1
CHART_NAME=$2
CHART_VERSION=$3
NAMESPACE=$4
RELEASE_NAME=$5
VALUES_FILE=$6
OUTPUT_DIR=$7
if [ -e "$OUTPUT_DIR" ]; then
echo "OUTPUT_DIR $OUTPUT_DIR already exists, must be a path that does not yet exist."
exit 1
fi
TMP_DIR="$(mktemp -d)"
trap 'rm -rf $TMP_DIR' EXIT
echo Fetching Chart
if [ "$FETCH_GIT_REPO" == "true" ]; then
git clone --branch "$CHART_VERSION" "$REPO_URL" "$TMP_DIR/$CHART_NAME"
else
"$HELM_BIN" fetch "$CHART_NAME" \
--repo "$REPO_URL" \
--version "$CHART_VERSION" \
--destination "$TMP_DIR" \
--untar
fi
RESOURCE_FILES_DIR="$TMP_DIR/resource_manifests"
mkdir "$RESOURCE_FILES_DIR"
CHART_DIR="$TMP_DIR/$CHART_NAME"
TEMPLATE_FILES="$(find "$CHART_DIR" -path '*templates/*.yaml' -type f)"
echo Rendering Chart templates
HELM_TEMPLATE_ARGS=(\
--namespace "$NAMESPACE" \
--name "$RELEASE_NAME" \
"$CHART_DIR" \
)
if [ "$VALUES_FILE" != "-" ]; then
HELM_TEMPLATE_ARGS+=(-f "$VALUES_FILE")
fi
while read -r template_file_path; do
template=${template_file_path#"$CHART_DIR/"}
OUTPUT_FILE="$RESOURCE_FILES_DIR/$template"
echo "Templating $template"
mkdir -p "$(dirname "$OUTPUT_FILE")"
export NAMESPACE
"$HELM_BIN" template "${HELM_TEMPLATE_ARGS[@]}" -x "$template_file_path" \
| faq -f yaml -o yaml -M -s '.[] | select(. != null) | . * (if .kind != "Namespace" and .metadata.namespace == null then {"metadata": {"namespace": $ENV.NAMESPACE}} else {} end)' \
> "$OUTPUT_FILE"
if [[ ! $(sed -f "$DIR/remove-helm-template-header.sed" "$OUTPUT_FILE" | grep -v '^#' | tr -d '[:space:]') ]]; then
echo "Templating $template resulted in an empty file, not storing."
rm "$OUTPUT_FILE"
fi
done <<< "$TEMPLATE_FILES"
mkdir -p "$(dirname "$OUTPUT_DIR")"
mv "$RESOURCE_FILES_DIR" "$OUTPUT_DIR"
# This script post-processes helm template output
# Removes headers of the following form
# Example helm template header:
# ---
# # Source: foo-chart/templates/deployment.yaml
s/^---$//g
s/# Source.*$//g
# Delete trailing whitespace (spaces, tabs) from end of each line
s/[ ]*$//
# Deletes all blank lines from top and end of file
/./,/^$/!d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment