-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.sh
More file actions
333 lines (288 loc) · 18.2 KB
/
Copy pathbootstrap.sh
File metadata and controls
333 lines (288 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env bash
# platform-one bootstrap — runs automatically in GitHub Codespaces
set -euo pipefail
WORKDIR="/workspaces/platform-one"
BIN="/usr/local/bin"
log() { echo -e "\033[1;36m==> $*\033[0m"; }
ok() { echo -e "\033[1;32m✔ $*\033[0m"; }
warn() { echo -e "\033[1;33m⚠ $*\033[0m"; }
# ── Helper: latest GitHub release tag ───────────────────────────────────────
gh_latest() {
curl -sf "https://api.github.com/repos/$1/releases/latest" \
| grep '"tag_name"' | head -1 | cut -d'"' -f4
}
# ── k3d ─────────────────────────────────────────────────────────────────────
if ! command -v k3d &>/dev/null; then
log "Installing k3d"
curl -sf https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash
fi
ok "k3d $(k3d version | head -1)"
# ── kubectl (already in universal image, but ensure it) ─────────────────────
if ! command -v kubectl &>/dev/null; then
log "Installing kubectl"
K8S_VER=$(curl -sf https://dl.k8s.io/release/stable.txt)
curl -sfLo "$BIN/kubectl" "https://dl.k8s.io/release/${K8S_VER}/bin/linux/amd64/kubectl"
chmod +x "$BIN/kubectl"
fi
ok "kubectl $(kubectl version --client --short 2>/dev/null || kubectl version --client)"
# ── Helm ─────────────────────────────────────────────────────────────────────
if ! command -v helm &>/dev/null; then
log "Installing Helm"
curl -sf https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
fi
ok "helm $(helm version --short)"
# ── ArgoCD CLI ───────────────────────────────────────────────────────────────
if ! command -v argocd &>/dev/null; then
log "Installing ArgoCD CLI"
ARGOCD_VER=$(gh_latest argoproj/argo-cd)
curl -sfLo "$BIN/argocd" \
"https://github.com/argoproj/argo-cd/releases/download/${ARGOCD_VER}/argocd-linux-amd64"
chmod +x "$BIN/argocd"
fi
ok "argocd $(argocd version --client --short 2>/dev/null | head -1)"
# ── cosign ───────────────────────────────────────────────────────────────────
if ! command -v cosign &>/dev/null; then
log "Installing cosign"
COSIGN_VER=$(gh_latest sigstore/cosign)
curl -sfLo "$BIN/cosign" \
"https://github.com/sigstore/cosign/releases/download/${COSIGN_VER}/cosign-linux-amd64"
chmod +x "$BIN/cosign"
fi
ok "cosign $(cosign version 2>/dev/null | grep GitVersion | awk '{print $2}')"
# ── syft ─────────────────────────────────────────────────────────────────────
if ! command -v syft &>/dev/null; then
log "Installing syft"
curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh \
| sh -s -- -b "$BIN" 2>/dev/null
fi
ok "syft $(syft --version)"
# ── Trivy ────────────────────────────────────────────────────────────────────
if ! command -v trivy &>/dev/null; then
log "Installing Trivy"
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh \
| sh -s -- -b "$BIN" 2>/dev/null
fi
ok "trivy $(trivy --version | head -1)"
# ── Vault CLI ────────────────────────────────────────────────────────────────
if ! command -v vault &>/dev/null; then
log "Installing Vault CLI"
VAULT_VER="1.17.2"
curl -sSLo /tmp/vault.zip \
"https://releases.hashicorp.com/vault/${VAULT_VER}/vault_${VAULT_VER}_linux_amd64.zip"
unzip -qo /tmp/vault.zip -d "$BIN" && rm /tmp/vault.zip
fi
ok "vault $(vault version)"
# ── conftest ─────────────────────────────────────────────────────────────────
if ! command -v conftest &>/dev/null; then
log "Installing conftest"
CVER=$(gh_latest open-policy-agent/conftest | tr -d v)
curl -sSLo /tmp/conftest.tar.gz \
"https://github.com/open-policy-agent/conftest/releases/download/v${CVER}/conftest_${CVER}_Linux_x86_64.tar.gz"
tar -xzf /tmp/conftest.tar.gz -C "$BIN" conftest && rm /tmp/conftest.tar.gz
fi
ok "conftest $(conftest --version)"
# ── k9s ──────────────────────────────────────────────────────────────────────
if ! command -v k9s &>/dev/null; then
log "Installing k9s"
K9S_VER=$(gh_latest derailed/k9s)
curl -sSLo /tmp/k9s.tar.gz \
"https://github.com/derailed/k9s/releases/download/${K9S_VER}/k9s_Linux_amd64.tar.gz"
tar -xzf /tmp/k9s.tar.gz -C "$BIN" k9s && rm /tmp/k9s.tar.gz
fi
ok "k9s $(k9s version --short 2>/dev/null | head -1)"
# ── Node deps for portal ─────────────────────────────────────────────────────
if [ -f "$WORKDIR/portal/package.json" ]; then
log "Installing portal npm dependencies"
cd "$WORKDIR/portal" && npm install --silent
ok "Portal deps installed"
fi
# ── Create k3d cluster ───────────────────────────────────────────────────────
if ! k3d cluster list | grep -q "^platform-one"; then
log "Creating k3d cluster (platform-one)"
k3d cluster create platform-one \
--port "8080:80@loadbalancer" \
--port "8443:443@loadbalancer" \
--agents 2 \
--k3s-arg "--disable=traefik@server:0" \
--k3s-arg "--disable=servicelb@server:0" \
--wait
ok "k3d cluster created"
else
warn "k3d cluster 'platform-one' already exists — skipping"
fi
kubectl config use-context k3d-platform-one
# ── Add Helm repos ────────────────────────────────────────────────────────────
log "Adding Helm repos"
helm repo add argo https://argoproj.github.io/argo-helm 2>/dev/null || true
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx 2>/dev/null || true
helm repo add cert-manager https://charts.jetstack.io 2>/dev/null || true
helm repo add crossplane https://charts.crossplane.io/stable 2>/dev/null || true
helm repo add hashicorp https://helm.releases.hashicorp.com 2>/dev/null || true
helm repo add grafana https://grafana.github.io/helm-charts 2>/dev/null || true
helm repo add prometheus https://prometheus-community.github.io/helm-charts 2>/dev/null || true
helm repo add kyverno https://kyverno.github.io/kyverno/ 2>/dev/null || true
helm repo add external-secrets https://charts.external-secrets.io 2>/dev/null || true
helm repo add open-telemetry https://open-telemetry.github.io/opentelemetry-helm-charts 2>/dev/null || true
helm repo update
# ── Namespaces ───────────────────────────────────────────────────────────────
log "Creating namespaces"
for ns in argocd crossplane-system vault monitoring kyverno external-secrets cert-manager ingress-nginx platform-portal; do
kubectl create namespace "$ns" --dry-run=client -o yaml | kubectl apply -f -
done
# ── ingress-nginx ─────────────────────────────────────────────────────────────
log "Installing ingress-nginx"
helm upgrade --install ingress-nginx ingress-nginx/ingress-nginx \
--namespace ingress-nginx \
--set controller.service.type=LoadBalancer \
--set controller.hostPort.enabled=true \
--set controller.hostPort.ports.http=80 \
--set controller.hostPort.ports.https=443 \
--wait --timeout 120s
ok "ingress-nginx ready"
# ── cert-manager ──────────────────────────────────────────────────────────────
log "Installing cert-manager"
helm upgrade --install cert-manager cert-manager/cert-manager \
--namespace cert-manager \
--set installCRDs=true \
--wait --timeout 120s
ok "cert-manager ready"
# ── ArgoCD ───────────────────────────────────────────────────────────────────
log "Installing ArgoCD"
helm upgrade --install argocd argo/argo-cd \
--namespace argocd \
--values "$WORKDIR/infrastructure/argocd/helm-values.yaml" \
--wait --timeout 180s
ok "ArgoCD ready"
# ── Argo Rollouts ─────────────────────────────────────────────────────────────
log "Installing Argo Rollouts"
helm upgrade --install argo-rollouts argo/argo-rollouts \
--namespace argo-rollouts \
--create-namespace \
--set dashboard.enabled=true \
--wait --timeout 120s
ok "Argo Rollouts ready"
# ── Crossplane ────────────────────────────────────────────────────────────────
log "Installing Crossplane"
helm upgrade --install crossplane crossplane/crossplane \
--namespace crossplane-system \
--set args='{--enable-composition-revisions}' \
--wait --timeout 180s
ok "Crossplane ready"
# ── Crossplane Providers ──────────────────────────────────────────────────────
log "Installing Crossplane providers (kubernetes + AWS)"
# Kubernetes provider — needed for namespace/object management in local dev
kubectl apply -f - <<'PROVIDER'
apiVersion: pkg.crossplane.io/v1
kind: Provider
metadata:
name: provider-kubernetes
spec:
package: xpkg.upbound.io/crossplane-contrib/provider-kubernetes:v0.14.1
installRuntimeConfig:
spec:
serviceAccountName: provider-kubernetes
PROVIDER
# Wait for provider to be healthy (up to 3 min)
echo "Waiting for Crossplane Kubernetes provider..."
for i in $(seq 1 36); do
STATUS=$(kubectl get provider provider-kubernetes -o jsonpath='{.status.conditions[?(@.type=="Healthy")].status}' 2>/dev/null || echo "")
[ "$STATUS" = "True" ] && { ok "Crossplane Kubernetes provider ready"; break; }
sleep 5
done
# Grant provider cluster-admin for local dev
kubectl create clusterrolebinding crossplane-provider-kubernetes \
--clusterrole=cluster-admin \
--serviceaccount=crossplane-system:provider-kubernetes 2>/dev/null || true
ok "Crossplane providers ready"
# ── Kyverno ──────────────────────────────────────────────────────────────────
log "Installing Kyverno"
helm upgrade --install kyverno kyverno/kyverno \
--namespace kyverno \
--set admissionController.replicas=1 \
--wait --timeout 180s
ok "Kyverno ready"
# ── Apply Kyverno policies ────────────────────────────────────────────────────
log "Applying Kyverno policies"
kubectl apply -f "$WORKDIR/policies/kyverno/policy-bundle.yaml" || warn "Some policies need cluster to warm up first"
# ── External Secrets Operator ─────────────────────────────────────────────────
log "Installing External Secrets Operator"
helm upgrade --install external-secrets external-secrets/external-secrets \
--namespace external-secrets \
--wait --timeout 120s
ok "External Secrets Operator ready"
# ── Vault (dev mode for local) ────────────────────────────────────────────────
log "Installing Vault (dev mode)"
helm upgrade --install vault hashicorp/vault \
--namespace vault \
--values "$WORKDIR/infrastructure/vault/helm-values.yaml" \
--wait --timeout 120s
ok "Vault ready"
# ── kube-prometheus-stack ────────────────────────────────────────────────────
log "Installing kube-prometheus-stack"
helm upgrade --install kube-prometheus prometheus/kube-prometheus-stack \
--namespace monitoring \
--values "$WORKDIR/infrastructure/monitoring/prometheus-values.yaml" \
--wait --timeout 300s
ok "Prometheus + Grafana ready"
# ── Loki ─────────────────────────────────────────────────────────────────────
log "Installing Loki"
helm upgrade --install loki grafana/loki \
--namespace monitoring \
--values "$WORKDIR/infrastructure/monitoring/loki-values.yaml" \
--wait --timeout 120s
ok "Loki ready"
# ── Tempo ─────────────────────────────────────────────────────────────────────
log "Installing Tempo"
helm upgrade --install tempo grafana/tempo \
--namespace monitoring \
--set tempo.storage.trace.backend=local \
--wait --timeout 120s
ok "Tempo ready"
# ── OTel Operator ─────────────────────────────────────────────────────────────
log "Installing OpenTelemetry Operator"
helm upgrade --install opentelemetry-operator open-telemetry/opentelemetry-operator \
--namespace monitoring \
--set "manager.collectorImage.repository=otel/opentelemetry-collector-contrib" \
--wait --timeout 120s
kubectl apply -f "$WORKDIR/observability/otel/collector-config.yaml"
ok "OTel Operator ready"
# ── Apply Crossplane XRDs ─────────────────────────────────────────────────────
log "Applying Crossplane XRDs"
kubectl apply -f "$WORKDIR/infrastructure/crossplane/xrds/" || warn "XRDs queued"
kubectl apply -f "$WORKDIR/infrastructure/crossplane/compositions/" || warn "Compositions queued"
# ── Apply ArgoCD project and RBAC ────────────────────────────────────────────
log "Creating ArgoCD platform project"
kubectl apply -f "$WORKDIR/infrastructure/argocd/projects/platform-project.yaml"
ok "ArgoCD platform project created"
# ── Bootstrap ArgoCD root app ─────────────────────────────────────────────────
log "Bootstrapping ArgoCD root app"
kubectl apply -f "$WORKDIR/infrastructure/argocd/apps/root-app.yaml"
# ── Portal ConfigMaps (bundle source code into cluster) ───────────────────────
log "Creating portal source ConfigMaps"
kubectl create configmap platform-portal-src \
--from-file="$WORKDIR/portal/server.js" \
--from-file="$WORKDIR/portal/package.json" \
-n platform-portal --dry-run=client -o yaml | kubectl apply -f -
kubectl create configmap platform-portal-public \
--from-file="$WORKDIR/portal/public/index.html" \
-n platform-portal --dry-run=client -o yaml | kubectl apply -f -
ok "Portal ConfigMaps created"
# ── Deploy Platform Portal ────────────────────────────────────────────────────
log "Deploying Platform Portal"
kubectl apply -f "$WORKDIR/portal/k8s/"
ok "Platform Portal deploying"
# ── Print summary ─────────────────────────────────────────────────────────────
ARGOCD_PASS=$(kubectl -n argocd get secret argocd-initial-admin-secret \
-o jsonpath="{.data.password}" 2>/dev/null | base64 -d || echo "check-argocd-secret")
echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║ platform-one is ready 🚀 ║"
echo "╠══════════════════════════════════════════════════════════════╣"
echo "║ Portal → http://localhost:8080 ║"
echo "║ ArgoCD → http://localhost:8080/argocd ║"
echo "║ Grafana → http://localhost:3000 (admin/prom-operator) ║"
echo "║ Prometheus → http://localhost:9090 ║"
echo "║ Vault → http://localhost:8200 (token: root) ║"
echo "╠══════════════════════════════════════════════════════════════╣"
echo "║ ArgoCD admin password: $ARGOCD_PASS"
echo "╚══════════════════════════════════════════════════════════════╝"