|
| 1 | +#!/usr/bin/env bash |
| 2 | +# patch-cluster-secret.sh <karmada-kubeconfig> <cluster-name> <internal-kubeconfig> |
| 3 | +# |
| 4 | +# After "karmadactl join", Karmada stores the member cluster's kubeconfig in a |
| 5 | +# Secret referenced by the Cluster object's spec.secretRef, and sets |
| 6 | +# spec.apiEndpoint to the localhost address it resolved from the external |
| 7 | +# kubeconfig. The Karmada controller manager runs inside Docker and cannot use |
| 8 | +# localhost to reach POP cell API servers. |
| 9 | +# |
| 10 | +# This script: |
| 11 | +# 1. Replaces the kubeconfig in the Secret with the Docker-IP variant so that |
| 12 | +# the Karmada controller can make API calls to the member cluster. |
| 13 | +# 2. Patches spec.apiEndpoint on the Cluster object so that health checks also |
| 14 | +# use the Docker bridge IP instead of localhost. |
| 15 | +# |
| 16 | +# Usage: |
| 17 | +# hack/e2e/patch-cluster-secret.sh \ |
| 18 | +# tmp/e2e/kubeconfigs/karmada.yaml \ |
| 19 | +# compute-pop-dfw \ |
| 20 | +# tmp/e2e/kubeconfigs/pop-dfw-internal.yaml |
| 21 | + |
| 22 | +set -euo pipefail |
| 23 | + |
| 24 | +KARMADA_KUBECONFIG="${1:?usage: $0 <karmada-kubeconfig> <cluster-name> <internal-kubeconfig>}" |
| 25 | +CLUSTER_NAME="${2:?usage: $0 <karmada-kubeconfig> <cluster-name> <internal-kubeconfig>}" |
| 26 | +INTERNAL_KUBECONFIG="${3:?usage: $0 <karmada-kubeconfig> <cluster-name> <internal-kubeconfig>}" |
| 27 | + |
| 28 | +# ------------------------------------------------------------------ |
| 29 | +# Read the Cluster object's secretRef (name + namespace) |
| 30 | +# ------------------------------------------------------------------ |
| 31 | +SECRET_NAME=$(kubectl \ |
| 32 | + --kubeconfig="${KARMADA_KUBECONFIG}" \ |
| 33 | + get cluster "${CLUSTER_NAME}" \ |
| 34 | + -o jsonpath='{.spec.secretRef.name}' 2>/dev/null || true) |
| 35 | + |
| 36 | +if [ -z "${SECRET_NAME}" ]; then |
| 37 | + echo "ERROR: Could not find spec.secretRef.name on cluster '${CLUSTER_NAME}'." >&2 |
| 38 | + echo " Has karmadactl join completed successfully?" >&2 |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +SECRET_NAMESPACE=$(kubectl \ |
| 43 | + --kubeconfig="${KARMADA_KUBECONFIG}" \ |
| 44 | + get cluster "${CLUSTER_NAME}" \ |
| 45 | + -o jsonpath='{.spec.secretRef.namespace}' 2>/dev/null || true) |
| 46 | + |
| 47 | +SECRET_NAMESPACE="${SECRET_NAMESPACE:-karmada-system}" |
| 48 | + |
| 49 | +echo " Patching secret ${SECRET_NAMESPACE}/${SECRET_NAME} with Docker-IP kubeconfig..." |
| 50 | + |
| 51 | +# ------------------------------------------------------------------ |
| 52 | +# Replace the kubeconfig data in the secret |
| 53 | +# ------------------------------------------------------------------ |
| 54 | +kubectl \ |
| 55 | + --kubeconfig="${KARMADA_KUBECONFIG}" \ |
| 56 | + create secret generic "${SECRET_NAME}" \ |
| 57 | + --namespace="${SECRET_NAMESPACE}" \ |
| 58 | + --from-file=kubeconfig="${INTERNAL_KUBECONFIG}" \ |
| 59 | + --dry-run=client -o yaml \ |
| 60 | + | kubectl \ |
| 61 | + --kubeconfig="${KARMADA_KUBECONFIG}" \ |
| 62 | + apply -f - |
| 63 | + |
| 64 | +echo " Secret ${SECRET_NAMESPACE}/${SECRET_NAME} updated — Karmada controller will use Docker bridge IP" |
| 65 | + |
| 66 | +# ------------------------------------------------------------------ |
| 67 | +# Extract the Docker-IP server URL from the internal kubeconfig and |
| 68 | +# patch spec.apiEndpoint on the Cluster object so that Karmada's |
| 69 | +# cluster-status controller uses the same reachable address for health |
| 70 | +# checks. Without this patch the controller continues to probe the |
| 71 | +# localhost address stored by karmadactl join and the cluster never |
| 72 | +# transitions to Ready. |
| 73 | +# ------------------------------------------------------------------ |
| 74 | +DOCKER_SERVER=$(kubectl \ |
| 75 | + --kubeconfig="${INTERNAL_KUBECONFIG}" \ |
| 76 | + config view --minify -o jsonpath='{.clusters[0].cluster.server}') |
| 77 | + |
| 78 | +if [ -z "${DOCKER_SERVER}" ]; then |
| 79 | + echo "ERROR: Could not read server URL from ${INTERNAL_KUBECONFIG}" >&2 |
| 80 | + exit 1 |
| 81 | +fi |
| 82 | + |
| 83 | +echo " Patching spec.apiEndpoint on cluster '${CLUSTER_NAME}' → ${DOCKER_SERVER}..." |
| 84 | +kubectl \ |
| 85 | + --kubeconfig="${KARMADA_KUBECONFIG}" \ |
| 86 | + patch cluster "${CLUSTER_NAME}" \ |
| 87 | + --type=merge \ |
| 88 | + -p "{\"spec\":{\"apiEndpoint\":\"${DOCKER_SERVER}\"}}" |
| 89 | + |
| 90 | +echo " Cluster '${CLUSTER_NAME}' patched — health checks will now use Docker bridge IP" |
0 commit comments