Created
May 24, 2023 13:45
-
-
Save ImIOImI/8df66496548a95aa4b75bc62b7ffa446 to your computer and use it in GitHub Desktop.
Migrate HPAs in a helm chart from autoscaling/v2beta1 or autoscaling/v2beta2 to autoscaling/v2
This file contains hidden or 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 | |
# This script will migrate a broken release that has an HPA with a autoscaler that uses the beta1 or beta2 api to | |
# autoscaling v2. This script is necessary to run if you have upgraded your cluster to 1.26 or higher and you can't use | |
# mapkubeapis to fix the issue. | |
COLUMNS=1 | |
# Prompt the user to select one of their namespaces. | |
echo -e "\e[33mPlease select a namespace:\e[0m" | |
readarray -t NAMESPACES < <(kubectl get namespaces -o json | jq -r '.items[].metadata.name') | |
select NAMESPACE in "${NAMESPACES[@]}"; do | |
[[ -n $NAMESPACE ]] || { echo "Invalid choice. Please try again." >&2; continue; } | |
break # valid choice was made; exit prompt. | |
done | |
echo -e "\e[33mPlease select a release:\e[0m" | |
readarray -t RELEASES < <(helm list -n ${NAMESPACE} -o json | jq -r '.[].name') | |
select RELEASE in "${RELEASES[@]}"; do | |
[[ -n $RELEASE ]] || { echo "Invalid choice. Please try again." >&2; continue; } | |
break # valid choice was made; exit prompt. | |
done | |
echo -e "\e[32mSelected release ${RELEASE} in namespace: ${NAMESPACE}\e[0m" | |
# Remove any files that might screw things up | |
[ ! -e release.yaml ] || rm release.yaml | |
[ ! -e release.yaml.bak ] || rm release.yaml.bak | |
[ ! -e release.data.original.decoded ] || rm release.data.original.decoded | |
[ ! -e release.data.encoded ] || rm release.data.encoded | |
[ ! -e release.data.decoded ] || rm release.data.decoded | |
echo -e "\e[33mShould we filter secrets by status deployed? If the current status is deployed, it will likely narrow down | |
your options to one choice. If not and its status is, for example, 'uninstalling' you will have to find the latest | |
secret from the menu: [y/n] \n\e[0m" | |
read REPLY -n 1 -r | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
# grab the release secret that holds the manifest data for the release... hopefully there's only one, but build a menu | |
# just in case | |
readarray -t SECRETS < <(kubectl get secret -l owner=helm,status=deployed,name="${RELEASE}" --namespace "${NAMESPACE}" | \ | |
awk '{print $1}' | \ | |
grep -v NAME) | |
else | |
# grab the release secret that holds the manifest data for the release... hopefully there's only one, but build a menu | |
# just in case | |
readarray -t SECRETS < <(kubectl get secret -l owner=helm,name="${RELEASE}" --namespace "${NAMESPACE}" | \ | |
awk '{print $1}' | \ | |
grep -v NAME) | |
fi | |
# Prompt the user to select one of the lines. | |
COLUMNS=1 | |
echo -e "\e[33mPlease select a secret:\e[0m" | |
select SECRET in "${SECRETS[@]}"; do | |
[[ -n $SECRET ]] || { echo "Invalid choice. Please try again." >&2; continue; } | |
break # valid choice was made; exit prompt. | |
done | |
echo -e "\e[32mSelected secret ${SECRET}\e[0m" | |
# fill in the secret name | |
kubectl get secret "${SECRET}" -n panthers -o yaml > release.yaml | |
cp release.yaml release.yaml.bak | |
cat release.yaml | grep -oP '(?<=release: ).*' > release.data.original.decoded | |
cat release.data.original.decoded | base64 -d | base64 -d | gzip -d > release.data.decoded | |
sed -i 's/beta2//g' release.data.decoded | |
sed -i 's/beta1//g' release.data.decoded | |
cat release.data.decoded | gzip | base64 | base64 > release.data.encoded | |
NEW=$(tr --delete '\n' < release.data.encoded) | |
sed -i "/release:/c\ release: ${NEW}" release.yaml | |
echo -e "\e[33mAre you sure you want to proceed? (this is a great time to check release.yaml for any errors): [y/n] \n\e[0m" | |
read REPLY -n 1 -r | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
kubectl apply -f release.yaml | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment