|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 5 | +PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)" |
| 6 | + |
| 7 | +# Cluster names |
| 8 | +MGMT_CLUSTER="mgmt" |
| 9 | +GLOBAL_CLUSTER="global" |
| 10 | +SITE_CLUSTER="site" |
| 11 | + |
| 12 | +cleanup() { |
| 13 | + echo "Cleaning up clusters..." |
| 14 | + kind delete cluster --name "${SITE_CLUSTER}" || true |
| 15 | + kind delete cluster --name "${GLOBAL_CLUSTER}" || true |
| 16 | + kind delete cluster --name "${MGMT_CLUSTER}" || true |
| 17 | +} |
| 18 | + |
| 19 | +create_clusters() { |
| 20 | + echo "Creating management cluster..." |
| 21 | + kind create cluster --name "${MGMT_CLUSTER}" |
| 22 | + |
| 23 | + echo "Creating global cluster..." |
| 24 | + kind create cluster --name "${GLOBAL_CLUSTER}" |
| 25 | + |
| 26 | + echo "Creating site cluster..." |
| 27 | + kind create cluster --name "${SITE_CLUSTER}" |
| 28 | +} |
| 29 | + |
| 30 | +install_argocd() { |
| 31 | + echo "Installing ArgoCD..." |
| 32 | + kubectl --context "kind-${MGMT_CLUSTER}" create namespace argocd |
| 33 | + kubectl --context "kind-${MGMT_CLUSTER}" apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml |
| 34 | + |
| 35 | + # Wait for ArgoCD to be ready |
| 36 | + kubectl --context "kind-${MGMT_CLUSTER}" wait --for=condition=available --timeout=300s deployment/argocd-server -n argocd |
| 37 | +} |
| 38 | + |
| 39 | +setup_cluster_access() { |
| 40 | + echo "Setting up cluster access..." |
| 41 | + |
| 42 | + # Register global cluster |
| 43 | + register_cluster "${GLOBAL_CLUSTER}" "global" |
| 44 | + |
| 45 | + # Register site cluster |
| 46 | + register_cluster "${SITE_CLUSTER}" "site" |
| 47 | + |
| 48 | + # Verify clusters are registered |
| 49 | + verify_clusters |
| 50 | +} |
| 51 | + |
| 52 | +verify_clusters() { |
| 53 | + echo "Verifying cluster registration..." |
| 54 | + |
| 55 | + local max_attempts=30 |
| 56 | + local attempt=0 |
| 57 | + |
| 58 | + while [ $attempt -lt $max_attempts ]; do |
| 59 | + local registered_clusters=$(kubectl --context "kind-${MGMT_CLUSTER}" get secrets -n argocd -l argocd.argoproj.io/secret-type=cluster -o name | wc -l) |
| 60 | + |
| 61 | + if [ "$registered_clusters" -ge 2 ]; then |
| 62 | + echo "✓ All clusters registered successfully" |
| 63 | + kubectl --context "kind-${MGMT_CLUSTER}" get secrets -n argocd -l argocd.argoproj.io/secret-type=cluster -o custom-columns=NAME:.metadata.name,CLUSTER:.stringData.name |
| 64 | + return 0 |
| 65 | + fi |
| 66 | + |
| 67 | + echo "Waiting for clusters to register... ($((attempt + 1))/$max_attempts)" |
| 68 | + sleep 2 |
| 69 | + ((attempt++)) |
| 70 | + done |
| 71 | + |
| 72 | + echo "✗ Cluster registration verification failed" |
| 73 | + return 1 |
| 74 | +} |
| 75 | + |
| 76 | +register_cluster() { |
| 77 | + local cluster_name="$1" |
| 78 | + local cluster_role="$2" |
| 79 | + echo "Registering ${cluster_name} cluster with ArgoCD..." |
| 80 | + |
| 81 | + # Get cluster config |
| 82 | + TARGET_SERVER=$(kubectl --context "kind-${cluster_name}" config view --minify -o jsonpath='{.clusters[0].cluster.server}') |
| 83 | + TARGET_CA=$(kubectl --context "kind-${cluster_name}" config view --raw --minify --flatten -o jsonpath='{.clusters[0].cluster.certificate-authority-data}') |
| 84 | + |
| 85 | + # Create service account in target cluster |
| 86 | + kubectl --context "kind-${cluster_name}" apply -f - <<EOF |
| 87 | +apiVersion: v1 |
| 88 | +kind: ServiceAccount |
| 89 | +metadata: |
| 90 | + name: argocd-manager |
| 91 | + namespace: kube-system |
| 92 | +--- |
| 93 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 94 | +kind: ClusterRoleBinding |
| 95 | +metadata: |
| 96 | + name: argocd-manager |
| 97 | +roleRef: |
| 98 | + apiGroup: rbac.authorization.k8s.io |
| 99 | + kind: ClusterRole |
| 100 | + name: cluster-admin |
| 101 | +subjects: |
| 102 | +- kind: ServiceAccount |
| 103 | + name: argocd-manager |
| 104 | + namespace: kube-system |
| 105 | +EOF |
| 106 | + |
| 107 | + # Get token |
| 108 | + TOKEN=$(kubectl --context "kind-${cluster_name}" create token argocd-manager -n kube-system) |
| 109 | + |
| 110 | + # Add cluster to ArgoCD |
| 111 | + kubectl --context "kind-${MGMT_CLUSTER}" apply -f - <<EOF |
| 112 | +apiVersion: v1 |
| 113 | +kind: Secret |
| 114 | +metadata: |
| 115 | + name: ${cluster_name}-secret |
| 116 | + namespace: argocd |
| 117 | + labels: |
| 118 | + argocd.argoproj.io/secret-type: cluster |
| 119 | + annotations: |
| 120 | + understack.rackspace.com/env: test |
| 121 | + understack.rackspace.com/partition: test |
| 122 | + understack.racksapce.com/role: ${cluster_role} |
| 123 | +type: Opaque |
| 124 | +stringData: |
| 125 | + name: ${cluster_name} |
| 126 | + server: ${TARGET_SERVER} |
| 127 | + config: | |
| 128 | + { |
| 129 | + "bearerToken": "${TOKEN}", |
| 130 | + "tlsClientConfig": { |
| 131 | + "caData": "${TARGET_CA}" |
| 132 | + } |
| 133 | + } |
| 134 | +EOF |
| 135 | +} |
| 136 | + |
| 137 | +main() { |
| 138 | + if [ $# -eq 0 ]; then |
| 139 | + echo "Usage: $0 [setup|cleanup]" |
| 140 | + echo "" |
| 141 | + echo "Commands:" |
| 142 | + echo " setup - Create clusters, install ArgoCD, and deploy UnderStack" |
| 143 | + echo " cleanup - Delete all test clusters" |
| 144 | + exit 1 |
| 145 | + fi |
| 146 | + |
| 147 | + case "${1}" in |
| 148 | + "cleanup") |
| 149 | + cleanup |
| 150 | + ;; |
| 151 | + "setup") |
| 152 | + create_clusters |
| 153 | + install_argocd |
| 154 | + setup_cluster_access |
| 155 | + echo "Setup of three clusters is complete!" |
| 156 | + echo "ArgoCD UI: kubectl --context kind-mgmt port-forward svc/argocd-server -n argocd 8080:443" |
| 157 | + ;; |
| 158 | + *) |
| 159 | + echo "Usage: $0 [setup|cleanup]" |
| 160 | + exit 1 |
| 161 | + ;; |
| 162 | + esac |
| 163 | +} |
| 164 | + |
| 165 | +main "$@" |
0 commit comments