Created
April 16, 2020 13:31
-
-
Save tkellen/15b5b86fbf81fb68edc6d2f6b930f781 to your computer and use it in GitHub Desktop.
split up monolithic k8s yaml file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# split a monolithic k8s manifest file into separate files. | |
set -euo pipefail | |
REMOVE_KEYS="chart:|release:|heritage:" | |
function usage { | |
cat <<EOF | |
Usage: $(basename "$0") [output_dir] < input.yml | |
Explode a monolithic k8s manifest file into separate files during vendoring. | |
This aids in diff readability during updates. Output is further modified to | |
remove the keys chart/release/heritage (commonly found in rendered helm charts). | |
Example Usage: | |
curl -sSL https://github.com/jetstack/cert-manager/releases/download/v0.8.0/cert-manager.yaml | $(basename "$0") test | |
EOF | |
exit 1 | |
} | |
if [[ -t 0 ]]; then | |
usage | |
fi | |
# Get specified output directory. | |
OUTPUT_DIR=$PWD | |
if [[ $# -eq 1 ]]; then | |
OUTPUT_DIR=$(realpath -m $1) | |
mkdir -p $OUTPUT_DIR | |
if [[ ! -e $OUTPUT_DIR ]]; then | |
>&2 echo "Failed to create output directory ($OUTPUT_DIR)." | |
exit 1 | |
fi | |
fi | |
# Create temporary directory to explode files for modification into. | |
TEMP_DIR=$(mktemp -d) | |
if [[ ! -e $TEMP_DIR ]]; then | |
>&2 echo "Failed to create temp directory ($TEMP_DIR)." | |
exit 1 | |
fi | |
# Ensure we cleanup after ourselves. | |
trap "exit 1" HUP INT PIPE QUIT TERM | |
trap 'rm -rf "$TEMP_DIR"' EXIT | |
cd $TEMP_DIR | |
sed '/^$/d' | csplit --suppress-matched -sz - '/^---$/' '{*}' | |
for file in xx*; do | |
name=$(sed -n '/name: / {p;q}' $file | awk '{print tolower($2)}' | tr -d '\n' | tr -s -c [:alnum:][:blank:] -) | |
kind=$(sed -n '/^kind: / {p;q}' $file | awk '{print tolower($2)}' | tr -d '\n' | tr -s -c [:alnum:][:blank:] -) | |
if [[ "$kind" == *"list" ]]; then | |
# list style resources contain many resources, hash the contents for name | |
name=$(cat $file | md5sum | cut -d' ' -f1) | |
fi | |
if [[ "$kind" == "namespace" ]]; then | |
printf "\nSkipping namespace resource, this belongs in overlays\n\n$(cat $file)\n\n" | |
elif [[ -z "$name" ]]; then | |
printf "\nSkipping potentially empty file, contents are: \n\n$(cat $file)\n\n" | |
else | |
destination="$OUTPUT_DIR/$kind-$name.yml" | |
echo "Exploding to $destination." | |
awk "!/$REMOVE_KEYS/" $file > $destination | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment