Files
kustomize/plugin/someteam.example.com/v1/chartinflator/ChartInflator

112 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# Copyright 2019 The Kubernetes Authors.
# SPDX-License-Identifier: Apache-2.0
# Helm chart inflator
#
# Reads a file like this
#
# apiVersion: kustomize.config.k8s.io/v1
# kind: ChartInflator
# metadata:
# name: notImportantHere
# chartName: nameOfStableChart
# values: /abs/path/to/local/values/file
# chartHome: /abs/path/local/chart/storage
# helmHome: /abs/path/to/helm/config
# helmBin: /abs/path/to/helmBin
# releaseNam: nameOfHelmRelease
# releaseNamespace: namespaceWhereHelmWouldApply
#
# fetches the given chart from stable/$chartName,
# and inflates it to stdout, using the given values file.
#
# chartDir default: $TMP_DIR/charts
#
# Example execution:
# ./plugin/someteam.example.com/v1/ChartInflator configFile.yaml
# TODO: allow specification of a specific chart VERSION
# so this test doesn't break every time minecraft is upgraded :P
# See https://github.com/helm/helm/issues/4008
set -e
# Yaml parsing is a ridiculous thing to do in bash,
# but let's try:
function parseYaml {
local file=$1
while read -r line
do
local k=${line%:*}
local v=${line#*:}
[ "$k" == "chartName" ] && chartName=$v
[ "$k" == "chartHome" ] && chartHome=$v
[ "$k" == "values" ] && valuesFile=$v
[ "$k" == "helmHome" ] && helmHome=$v
[ "$k" == "helmBin" ] && helmBin=$v
[ "$k" == "releaseName" ] && releaseName=$v
[ "$k" == "releaseNamespace" ] && releaseNamespace=$v
done <"$file"
# Trim leading space
chartName="${chartName#"${chartName%%[![:space:]]*}"}"
chartHome="${chartHome#"${chartHome%%[![:space:]]*}"}"
valuesFile="${valuesFile#"${valuesFile%%[![:space:]]*}"}"
helmBin="${helmBin#"${helmBin%%[![:space:]]*}"}"
releaseName="${releaseName#"${releaseName%%[![:space:]]*}"}"
releaseNamespace="${releaseNamespace#"${releaseNamespace%%[![:space:]]*}"}"
}
TMP_DIR=$(mktemp -d)
parseYaml $1
# Where all the files generated by 'helm init' live.
if [ -z "$helmHome" ]; then
helmHome=$TMP_DIR/dotHelm
fi
# Where helm charts are unpacked.
if [ -z "$chartHome" ]; then
chartHome=$TMP_DIR/charts
fi
if [ -z "$helmBin" ]; then
helmBin=helm
fi
if [ -z "$valuesFile" ]; then
valuesFile=$chartHome/$chartName/values.yaml
fi
if [ -z "$releaseName" ]; then
releaseName=release-name
fi
if [ -z "$releaseNamespace" ]; then
releaseNamespace=default
fi
function doHelm {
$helmBin --home $helmHome $@
}
# The init command is extremely chatty
doHelm init --client-only >& /dev/null
if [ ! -d "$chartHome/$chartName" ]; then
doHelm fetch --untar \
--untardir $chartHome \
stable/$chartName
fi
doHelm template \
--name $releaseName \
--namespace $releaseNamespace \
--values $valuesFile \
$chartHome/$chartName
/bin/rm -rf $TMP_DIR