Last active
January 19, 2024 19:35
-
-
Save duboisf/c6c087bc9a282c44bfce183b82f80e92 to your computer and use it in GitHub Desktop.
Update deprecated kubernetes api version that's stored in a helm release that's stored in a secret
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 | |
# Strict mode | |
set -euo pipefail | |
if [ $# -ne 3 ]; then | |
echo "Usage: $0 <helm_release_name> <old_api_version> <new_api_version>" | |
exit 1 | |
fi | |
HELM_RELEASE_NAME=$1 | |
OLD_API_VERSION=$2 | |
NEW_API_VERSION=$3 | |
SECRET_NAME=$(kubectl get secret -l name=${HELM_RELEASE_NAME},owner=helm,status=deployed --no-headers -o name | sort --version-sort | tail -n 1) | |
if [[ -z "${SECRET_NAME}" ]]; then | |
echo "No deployed release found for helm release ${HELM_RELEASE_NAME}" | |
exit 1 | |
fi | |
BACKUP_FILE=${HELM_RELEASE_NAME}_secret_backup_$(date +%y%m%d_%H%M%S).json | |
# Backup the secret | |
kubectl get $SECRET_NAME -o json > ${BACKUP_FILE} | |
RELASE_VERSION=$(jq -r .metadata.labels.version ${BACKUP_FILE}) | |
BACKUP_FILE_WITH_VERSION=${HELM_RELEASE_NAME}_secret_backup_release_${RELASE_VERSION}_$(date +%y%m%d_%H%M%S).json | |
cp ${BACKUP_FILE} ${BACKUP_FILE_WITH_VERSION} | |
echo "Using release version $RELASE_VERSION" | |
jq '.data.release' -r ${BACKUP_FILE} \ | |
| base64 -d \ | |
| base64 -d \ | |
| gunzip - \ | |
| jq . > ${HELM_RELEASE_NAME}_manifest.json | |
# Update the apiVersion | |
sed -e "s|${OLD_API_VERSION}|${NEW_API_VERSION}|g" ${HELM_RELEASE_NAME}_manifest.json > ${HELM_RELEASE_NAME}_manifest_updated.json | |
diff <(cat ${HELM_RELEASE_NAME}_manifest.json | jq .manifest -r) <( cat ${HELM_RELEASE_NAME}_manifest_updated.json | jq .manifest -r) || true | |
# Update the existing secret stored in ${HELM_RELEASE_NAME}_manifest_updated.json | |
NEW_RELEASE=$(cat ${HELM_RELEASE_NAME}_manifest_updated.json \ | |
| gzip -c \ | |
| base64 -w 0 \ | |
| base64 -w 0) | |
# Update the secret | |
echo "Updating the secret ${SECRET_NAME} with the new release" | |
kubectl patch $SECRET_NAME -p '{"data":{"release":"'${NEW_RELEASE}'"}}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment