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

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