Created
February 25, 2025 10:52
-
-
Save rverchere/1b202c35a99c7f4319928b9631813757 to your computer and use it in GitHub Desktop.
Script to migrate downstream rancher cluster to a new rancher URL.
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
#!/usr/bin/env bash | |
set -eu | |
# This script change the rancher manager URL (same rancher server) for downstream clusters. | |
# You need to access to the downstream cluster outside of rancher manager server, as you need to stop the cattle-agent deployment. | |
RANCHER_URL=https://rancher.mycompany.com | |
context=$(kubectl config current-context | cut -d'@' -f 2) # My context name is user@cluster | |
mkdir -p $context | |
secretName=$(kubectl get deploy cattle-cluster-agent -n cattle-system -o jsonpath="{.spec.template.spec.volumes[0].secret.secretName}") | |
secretUrlValue=$(kubectl get secret $secretName -n cattle-system -o jsonpath="{.data.url}" | base64 -d) | |
echo "> Context: $context, Secret Name: $secretName, Old URL: $secretUrlValue" | |
echo "> Saving secret..." | |
kubectl get secret $secretName -n cattle-system -o yaml > "$context/$secretName.orig.yaml"; | |
# As the deployment is automatically restarted (webhook?), we need to scale to 0 twice | |
read -p "> Secret for $context saved. Press enter to scale deployment to 0." | |
kubectl scale deployment cattle-cluster-agent -n cattle-system --replicas 0 | |
sleep 10 | |
kubectl scale deployment cattle-cluster-agent -n cattle-system --replicas 0 | |
read -p "> Deployment scaled to 0. Press enter to patch secret." | |
kubectl patch secret $secretName -n cattle-system --patch="{\"stringData\": { \"url\": \"${RANCHER_URL}\" }}" | |
kubectl get secret $secretName -n cattle-system -o yaml > "$context/$secretName.new.yaml"; | |
read -p "> Secret $secretName patched. Press enter to set CATTLE_SERVER environment variable." | |
kubectl set env deployment/cattle-cluster-agent -n cattle-system "CATTLE_SERVER=${RANCHER_URL}" | |
read -p "> CATTLE_SERVER environment variable set. Press enter to scale deployment to 2." | |
kubectl scale deployment cattle-cluster-agent -n cattle-system --replicas 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment