Skip to content

Instantly share code, notes, and snippets.

@dene14
Created October 4, 2020 19:07
Show Gist options
  • Save dene14/7253685cdedde2b6d16e8fc1df450d4f to your computer and use it in GitHub Desktop.
Save dene14/7253685cdedde2b6d16e8fc1df450d4f to your computer and use it in GitHub Desktop.
Migrate releases from helm2 to helm3
#!/bin/bash
# You need both helm v2 and v3, also helm "2to3" plugin should be installed in helm3
HELM2_BINARY=/usr/local/bin/helm
HELM3_BINARY=/usr/local/bin/helm3
KUBE_CONTEXT="mycontext"
kubectl config use-context ${KUBE_CONTEXT}
# Get available resource definitions in the CONTEXT
RESOURCES=$(kubectl api-resources --no-headers | awk '{print $1}' | xargs | sed 's; ;,;g')
comm -3 \
<(${HELM2_BINARY} list | grep -v 'NAMESPACE' | awk -F"\t" '$7 != "" {print $7 "\t" $1}' | sort -k1,2) \
<(${HELM3_BINARY} list --all-namespaces | grep -v 'NAMESPACE' | awk -F"\t" '{print $2 "\t" $1}' | sort -k1,2) |
awk '{print $1 "\t" $2}' |
while IFS= read -r line
do
while IFS=$'\t' read -r NAMESPACE RELEASE
do
echo "$NAMESPACE $RELEASE"
helm3 2to3 convert $RELEASE
# Label resources from charts
kubectl label $RESOURCES \
-n $NAMESPACE \
--overwrite \
-l "heritage=Tiller, release=$RELEASE" \
"app.kubernetes.io/managed-by=Helm"
# Annotate resources from chart
kubectl annotate $RESOURCES \
-n $NAMESPACE \
--overwrite \
-l "heritage=Tiller, release=$RELEASE" \
"meta.helm.sh/release-name=$RELEASE" \
"meta.helm.sh/release-namespace=$NAMESPACE"
done < <(echo "$line")
done
### If something went wrong you can delete all generated helm3 releases with command
# kubectl --context ${KUBE_CONTEXT} get secrets --all-namespaces \
# | grep sh.helm.release \
# | awk '{print "kubectl --context ${KUBE_CONTEXT} -n " $1 " delete secrets " $2}' \
# | parallel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment