-
Notifications
You must be signed in to change notification settings - Fork 1
Headlamp demo script #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,146 @@ | ||||||||||||||||||||
| #!/usr/bin/env bash | ||||||||||||||||||||
| set -e | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # Colors | ||||||||||||||||||||
| BOLD='\033[1m' | ||||||||||||||||||||
| CYAN='\033[0;36m' | ||||||||||||||||||||
| GREEN='\033[0;32m' | ||||||||||||||||||||
| RESET='\033[0m' | ||||||||||||||||||||
|
|
||||||||||||||||||||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||||||||||||||||||||
| HEADLAMP_DIR="${SCRIPT_DIR}/headlamp" | ||||||||||||||||||||
| PORT_FORWARD_PID="" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| cleanup() { | ||||||||||||||||||||
| local exit_code=$? | ||||||||||||||||||||
| if [[ $exit_code -ne 0 ]]; then | ||||||||||||||||||||
| echo "" | ||||||||||||||||||||
| echo -e "\033[0;31m${BOLD}An error occurred. Running cleanup...${RESET}" | ||||||||||||||||||||
| fi | ||||||||||||||||||||
| if [[ -n "${PORT_FORWARD_PID}" ]]; then | ||||||||||||||||||||
| kill "${PORT_FORWARD_PID}" 2>/dev/null || true | ||||||||||||||||||||
| fi | ||||||||||||||||||||
| kind delete cluster --name kpt-demo 2>/dev/null || true | ||||||||||||||||||||
| rm -rf "${HEADLAMP_DIR}" | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+20
to
+25
|
||||||||||||||||||||
| trap cleanup EXIT | ||||||||||||||||||||
|
|
||||||||||||||||||||
| banner() { | ||||||||||||||||||||
| echo "" | ||||||||||||||||||||
| echo -e "${CYAN}${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}" | ||||||||||||||||||||
| echo -e "${CYAN}${BOLD} $1${RESET}" | ||||||||||||||||||||
| echo -e "${CYAN}${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}" | ||||||||||||||||||||
| echo "" | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| run() { | ||||||||||||||||||||
| echo -e "${GREEN}> $*${RESET}" | ||||||||||||||||||||
| "$@" | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| pause() { | ||||||||||||||||||||
| echo "" | ||||||||||||||||||||
| read -rp "Press Enter to continue to the next step..." | ||||||||||||||||||||
| echo "" | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # ─── Step 1 ─────────────────────────────────────────────────────────────────── | ||||||||||||||||||||
| banner "Step 1: Create kind cluster 'kpt-demo'" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| run kind create cluster --name kpt-demo | ||||||||||||||||||||
|
|
||||||||||||||||||||
| pause | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # ─── Step 2 ─────────────────────────────────────────────────────────────────── | ||||||||||||||||||||
| banner "Step 2: Download headlamp KRM file and initialize kpt package" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| run mkdir -p "${HEADLAMP_DIR}" | ||||||||||||||||||||
| cd "${HEADLAMP_DIR}" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| run curl -sLO https://raw.githubusercontent.com/kubernetes-sigs/headlamp/main/kubernetes-headlamp.yaml | ||||||||||||||||||||
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| echo -e "${BOLD}# Generating security configuration${RESET}" | ||||||||||||||||||||
| echo -e "${GREEN}> Generating headlamp-admin-sa.yaml${RESET}" | ||||||||||||||||||||
| cat > headlamp-admin-sa.yaml <<'EOF' | ||||||||||||||||||||
| apiVersion: v1 | ||||||||||||||||||||
| kind: ServiceAccount | ||||||||||||||||||||
| metadata: | ||||||||||||||||||||
| name: headlamp-admin | ||||||||||||||||||||
| namespace: kube-system | ||||||||||||||||||||
| EOF | ||||||||||||||||||||
|
|
||||||||||||||||||||
| echo -e "${GREEN}> Generating headlamp-admin-crb.yaml${RESET}" | ||||||||||||||||||||
| cat > headlamp-admin-crb.yaml <<'EOF' | ||||||||||||||||||||
| apiVersion: rbac.authorization.k8s.io/v1 | ||||||||||||||||||||
| kind: ClusterRoleBinding | ||||||||||||||||||||
| metadata: | ||||||||||||||||||||
| name: headlamp-admin | ||||||||||||||||||||
| roleRef: | ||||||||||||||||||||
| apiGroup: rbac.authorization.k8s.io | ||||||||||||||||||||
| kind: ClusterRole | ||||||||||||||||||||
| name: cluster-admin | ||||||||||||||||||||
| subjects: | ||||||||||||||||||||
| - kind: ServiceAccount | ||||||||||||||||||||
| name: headlamp-admin | ||||||||||||||||||||
| namespace: kube-system | ||||||||||||||||||||
| EOF | ||||||||||||||||||||
|
Comment on lines
+72
to
+86
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| run kpt pkg init | ||||||||||||||||||||
|
|
||||||||||||||||||||
| pause | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # ─── Step 3 ─────────────────────────────────────────────────────────────────── | ||||||||||||||||||||
| banner "Step 3: Add set-label mutator pipeline to Kptfile" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| echo -e "${GREEN}> Appending pipeline block to Kptfile${RESET}" | ||||||||||||||||||||
| cat >> Kptfile <<'EOF' | ||||||||||||||||||||
| pipeline: | ||||||||||||||||||||
| mutators: | ||||||||||||||||||||
| - image: ghcr.io/kptdev/krm-functions-catalog/set-labels:v0.1.5 | ||||||||||||||||||||
| configMap: | ||||||||||||||||||||
| k8s-app: headlamp-kpt-demo | ||||||||||||||||||||
| EOF | ||||||||||||||||||||
|
|
||||||||||||||||||||
| echo "Kptfile now contains:" | ||||||||||||||||||||
| cat Kptfile | ||||||||||||||||||||
|
|
||||||||||||||||||||
| pause | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # ─── Step 4 ─────────────────────────────────────────────────────────────────── | ||||||||||||||||||||
| banner "Step 4: Run kpt fn render" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| run kpt fn render | ||||||||||||||||||||
|
|
||||||||||||||||||||
| pause | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # ─── Step 5 ─────────────────────────────────────────────────────────────────── | ||||||||||||||||||||
| banner "Step 5: Deploy to cluster, expose port, and create service account token" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| run kpt live init | ||||||||||||||||||||
| run kpt live apply --output=table --reconcile-timeout=5m | ||||||||||||||||||||
|
|
||||||||||||||||||||
| echo "" | ||||||||||||||||||||
| echo -e "${GREEN}> kubectl port-forward -n kube-system service/headlamp 8080:80 &${RESET}" | ||||||||||||||||||||
| kubectl port-forward -n kube-system service/headlamp 8080:80 & | ||||||||||||||||||||
| PORT_FORWARD_PID=$! | ||||||||||||||||||||
|
||||||||||||||||||||
| PORT_FORWARD_PID=$! | |
| PORT_FORWARD_PID=$! | |
| # Give port-forward a moment to start and then verify it is still running. | |
| sleep 2 | |
| if ! kill -0 "${PORT_FORWARD_PID}" 2>/dev/null; then | |
| echo "Error: kubectl port-forward failed to start or exited early. See output above for details." >&2 | |
| exit 1 | |
| fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cleanup does
kill "${PORT_FORWARD_PID}"without confirming the PID is still the port-forward process. Ifkubectl port-forwardexits early, that PID could theoretically be reused and the cleanup might kill an unrelated process. Consider checking the process is still running and matches the expected command before killing (or run port-forward in the foreground and terminate it explicitly).