Created
March 4, 2019 20:10
-
-
Save alexey-igrychev/06a8a9705b7cb122b24cc5a620e5e4d6 to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/bash | |
function helm::adopt() { | |
local chart_name=${1} | |
local release_name=${2} | |
local manifest_file_path=${3} | |
local tiller_namespace=${4:-kube-system} | |
# Check that the release has not been created yet | |
if [[ "$(kubectl get -n ${tiller_namespace} cm -l NAME=${release_name},OWNER=TILLER -o name)" != "" ]] ; then | |
>&2 echo "Error! Helm release '${release_name}' already exists!" | |
return 1 | |
fi | |
# Determine the length of specified manifest for stubbing | |
local manifest_file_length=$(cat ${manifest_file_path} | wc -c) | |
# Create chart with stub manifest | |
local chart_dir=$(mktemp -d) | |
cat > ${chart_dir}/Chart.yaml <<-YAML | |
apiVersion: v1 | |
appVersion: 1.0 | |
name: ${chart_name} | |
version: 0.1.0 | |
YAML | |
mkdir ${chart_dir}/templates | |
cat > ${chart_dir}/templates/stub.yaml <<- YAML | |
{{ repeat ${manifest_file_length} "#" }} | |
--- | |
apiVersion: v1 | |
kind: ConfigMap | |
metadata: | |
name: helm-${release_name}-stub | |
YAML | |
# Install chart | |
if ! helm install -n ${release_name} ${chart_dir} ; then | |
>&2 echo "Error! Installation of stub chart failed!" | |
helm delete --purge ${release_name} | |
rm -rf ${chart_dir} | |
exit 1 | |
fi | |
# Patch release cm storage | |
local stub=$(printf '#%.0s' $(seq 1 ${manifest_file_length})) | |
local release_data=$(kubectl get -n ${tiller_namespace} cm/${release_name}.v1 -o json | jq .data.release -r) | |
local updated_release_data=$(echo ${release_data} | base64 -d | zcat | sed "s/${stub}/$(sed -z 's/\n/\\n/g' ${manifest_file_path} | sed -z 's/\//\\\//g')/" | gzip -9 | base64 -w0) | |
kubectl patch -n ${tiller_namespace} cm/${release_name}.v1 -p '{"data":{"release":"'${updated_release_data}'"}}' | |
# Check the release | |
if ! helm list -q | grep '^'${release_name}'$' > /dev/null ; then | |
>&2 echo "Error! Adoption of ${release_name} failed!" | |
kubectl delete cm helm-adoption-${release_name} | |
kubectl delete cm -l NAME=${release_name},OWNER=TILLER | |
return 1 | |
fi | |
# Remove stub manifest and related resource, and prepare final chart | |
local manifest_file_name=$(basename ${manifest_file_path}) | |
rm ${chart_dir}/templates/stub.yaml | |
cp ${manifest_file_path} ${chart_dir}/templates/${manifest_file_name} | |
if helm upgrade ${release_name} ${chart_dir} ; then | |
rm -rf ${release_name} | |
mv ${chart_dir} ${chart_name} | |
else | |
helm delete --purge ${release_name} | |
rm -rf ${chart_dir} | |
exit 1 | |
fi | |
} | |
function usage() { | |
local script_name=$(basename "$0") | |
printf "Example:\n ./$script_name foo bar-prod manifest.yaml\n\n" | |
printf "Usage:\n ./$script_name CHART_NAME RELEASE_NAME MANIFEST_FILE_TO_ADOPT [TILLER_NAMESPACE]\n" | |
} | |
if [[ "$#" -gt 2 && "$#" -lt 5 ]] ; then | |
helm::adopt $1 $2 $3 $4 | |
elif [[ "$#" -eq 0 ]]; then | |
usage | |
else | |
usage | |
echo | |
>&2 echo "Error! Illegal number of parameters!" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment