Skip to content

Instantly share code, notes, and snippets.

@philipbankier
Last active April 2, 2021 14:12
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 philipbankier/b298e0ac8076e0b45476f248d6f7f8cf to your computer and use it in GitHub Desktop.
Save philipbankier/b298e0ac8076e0b45476f248d6f7f8cf to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script fixes helmcharts secrets that was deployed with API objects paths, that was deprecated.
# More info here https://github.com/helm/helm/issues/7219
# Use it own risks!
# After executing you need to redeploy
NamespaceName=$1
echo "${NamespaceName}"
echo "Starting to fix k8s objects $(date -R)"
mkdir -p /tmp/"${NamespaceName}"
cd /tmp/"${NamespaceName}" || exit
echo "Getting latest deployed helm release secret"
kubectl config set-context --current --namespace="${NamespaceName}"
kubectl get secret -l owner=helm,status=deployed -o yaml > "${NamespaceName}".release.bak
cp "${NamespaceName}".release.bak "${NamespaceName}".release
grep -oP '(?<=release: ).*' "${NamespaceName}".release | base64 -d | base64 -d | gzip -d - > "${NamespaceName}".release.data
# Change that sed replacement to you case
echo "Replacing wrong path in k8s API"
sed -i '' "s#web-deployment.yaml\\\napiVersion: apps/v1beta1#web-deployment.yaml\\\napiVersion: apps/v1#g" "${NamespaceName}".release.data
echo "Encoding release back"
gzip "${NamespaceName}".release.data --to-stdout | base64 | base64 > "${NamespaceName}".release.data.fixed
FIXED_DATA=$(cat "${NamespaceName}".release.data.fixed)
echo "Replacing release in ${NamespaceName}.release yaml file"
sed -i '' "s#release: .*#release: ${FIXED_DATA}#g" "${NamespaceName}".release
echo "Applying fixed release"
kubectl apply -f "${NamespaceName}".release
echo "Helm release fixed. You need to perform redeploy"
@philipbankier
Copy link
Author

#!/bin/bash

set -e
set -o pipefail

This script fixes helmcharts secrets that was deployed with API objects paths, that was deprecated.

More info here helm/helm#7219

Use it own risks and only if your know what for is this!

After exeecuting you need to perform redeploy

This is fixed by Victor Yagofarov version of:

helm/helm#7219 (comment)

NamespaceName=$1
ReleaseName="$2"
ResourceName="$3" # for example: deployment.yaml

echo "${NamespaceName}"

if [[ -z "${NamespaceName}" ]]; then
echo wrong input "(namespace)"
exit 2
fi
if [[ -z "${ReleaseName}" ]]; then
echo wrong input "(release name)"
exit 2
fi
if [[ -z "${ResourceName}" ]]; then
echo wrong input "(resource name to modify)"
exit 2
fi

echo "Starting to fix k8s objects $(date -R)"
mkdir -p /tmp/"${NamespaceName}"
cd /tmp/"${NamespaceName}" || exit
echo "Getting latest deployed helm release secret"
kubectl config set-context --current --namespace="${NamespaceName}"
kubectl --namespace="${NamespaceName}" get secret -l owner=helm,status=deployed,name="${ReleaseName}" -o yaml > "${NamespaceName}"."${ReleaseName}".release.bak
cp "${NamespaceName}"."${ReleaseName}".release.bak "${NamespaceName}"."${ReleaseName}".release
ggrep -oP '(?<=release: ).*' "${NamespaceName}"."${ReleaseName}".release | base64 -d | base64 -d | gzip -d - > "${NamespaceName}"."${ReleaseName}".release.data

Change that sed replacement to you case

echo "Replacing wrong path in k8s API"
sed -i '' "s#${ResourceName}\\napiVersion: extensions/v1beta1#${ResourceName}\\napiVersion: apps/v1#g" "${NamespaceName}"."${ReleaseName}".release.data

echo "Encoding release back"
gzip "${NamespaceName}"."${ReleaseName}".release.data --to-stdout | base64 | base64 > "${NamespaceName}"."${ReleaseName}".release.data.fixed
FIXED_DATA=$(cat "${NamespaceName}"."${ReleaseName}".release.data.fixed)

echo "Replacing release in ${NamespaceName}."${ReleaseName}".release yaml file"
sed -i '' "s#release: .*#release: ${FIXED_DATA}#g" "${NamespaceName}"."${ReleaseName}".release

echo "Applying fixed release"
kubectl --namespace="${NamespaceName}" apply -f "${NamespaceName}"."${ReleaseName}".release

echo "Helm release fixed. You need to perform redeploy"

@philipbankier
Copy link
Author

example of script in last comment ./helm_rewrite_history.sh "mynamespace" "helm-release-name" "deployment.yaml"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment