2022-10-12 16:30:17 +01:00

81 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
#####
# Script that moves the specified Helm release (NOT resources!) from one namespace to another
#####
set -eo pipefail
RELEASE_NAME=
FROM_NAMESPACE=
TO_NAMESPACE=
while :; do
case $1 in
--help)
echo "Moves the specified Helm release from one namespace to another."
echo ""
echo "WARNING: This script does NOT move resources, only the release itself."
echo " It should only be used with charts that explicitly specify resource namespaces."
echo ""
echo "Usage: helm-move RELEASE-NAME FROM-NAMESPACE TO-NAMESPACE"
exit
;;
?*)
if [ -z "$RELEASE_NAME" ]; then
RELEASE_NAME="$1"
elif [ -z "$FROM_NAMESPACE" ]; then
FROM_NAMESPACE="$1"
elif [ -z "$TO_NAMESPACE" ]; then
TO_NAMESPACE="$1"
else
echo "Too many arguments" >&2
exit 1
fi
;;
*)
break
esac
shift
done
if [ -z "$RELEASE_NAME" ]; then
echo "RELEASE-NAME was not given" >&2
exit 1
elif [ -z "$FROM_NAMESPACE" ]; then
echo "FROM-NAMESPACE was not given" >&2
exit 1
elif [ -z "$TO_NAMESPACE" ]; then
echo "TO-NAMESPACE was not given" >&2
exit 1
fi
# Make sure that the target namespace exists
kubectl create ns "$TO_NAMESPACE" || true
# Move each secret that corresponds to a revision of the release to the new namespace
for secret in $(kubectl -n $FROM_NAMESPACE get secret -o name --field-selector "type=helm.sh/release.v1" -l "name=$RELEASE_NAME"); do
# We need to replace the namespace in the release data
release="$(
kubectl -n $FROM_NAMESPACE get $secret -o go-template='{{.data.release}}' |
base64 -d |
base64 -d |
gzip -d |
jq -c ".namespace=\"$TO_NAMESPACE\"" |
gzip |
base64 |
base64
)"
# Copy the secret to a new namespace, modifying it as it goes
kubectl -n $FROM_NAMESPACE get $secret -o json |
jq -c 'del(.metadata.creationTimestamp)' |
jq -c 'del(.metadata.resourceVersion)' |
jq -c 'del(.metadata.uid)' |
jq -c ".metadata.namespace=\"$TO_NAMESPACE\"" |
jq -c ".data.release=\"$release\"" |
kubectl create -f -
# Remove the old secret
kubectl -n $FROM_NAMESPACE delete $secret
done