Skip to content

Headlamp demo script#23

Open
ciaranjohnston wants to merge 2 commits intomainfrom
headlamp-demo
Open

Headlamp demo script#23
ciaranjohnston wants to merge 2 commits intomainfrom
headlamp-demo

Conversation

@ciaranjohnston
Copy link
Member

Create a script that runs as a demo, performing the following steps with a prompt request to continue after eaxh step:

step 1:

  • create a kind cluster with the name "kpt-demo"
    step 2:
  • create a directory called "headlamp" and cd to it
  • download the KRM file from the headlamp project: https://raw.githubusercontent.com/kubernetes-sigs/headlamp/main/kubernetes-headlamp.yaml
  • Create KRM for the service account and the cluster role binding needed to get the headlamp-admin reconciled
  • run "kpt pkg init" in the directory
    step 3:
  • create a pipeline in the Kptfile which mutates the kpt file using the "set-label" mutator. Change the k8s-app label to "headlamp-kpt-demo"
    step 4:
  • run "kpt fn render" to run the pipeline
    step 5:
  • run kpt live init, followed by kpt live apply to instantiate the app in the previously created kind cluster
  • configure the cluster to expose a port so I can access the UI - e.g. "kubectl port-forward -n kube-system service/headlamp 8080:80"
  • create a service account token:
    kubectl create token headlamp-admin -n kube-system
  • print the token out so I can load it into the browser
    step 6:
  • clean up - remove the cluster and the package

this script is intended for use to demonstrate in a simple way how kpt can be applied quickly and easily to automate and manage an existing application with a KRM YAML for deployment.

…d along with a mutated label

Signed-off-by: Ciaran Johnston <ciaran.johnston@ericsson.com>
Copilot AI review requested due to automatic review settings March 22, 2026 15:20
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an interactive Bash demo script to showcase using kpt to package, mutate (via a function pipeline), render, and kpt live apply the Headlamp Kubernetes UI onto a local kind cluster, then print an access token and clean up.

Changes:

  • Introduces headlamp-demo.sh with step-by-step prompts to create a kind cluster, fetch Headlamp manifests, and initialize a kpt package.
  • Adds a set-labels mutator pipeline to change the k8s-app label and runs kpt fn render.
  • Deploys with kpt live, starts a local kubectl port-forward, prints a service account token, and performs teardown.
Comments suppressed due to low confidence (1)

headlamp-demo.sh:147

  • The PR description says there should be a prompt to continue after each step, but Step 6 immediately exits (triggering the EXIT trap cleanup) without a final confirmation/pause. If the intent is to let the presenter keep Headlamp open after Step 5, consider moving cleanup out of the EXIT trap into an explicit Step 6 action gated by a final prompt (or add a pause in Step 6 before exiting).
# ─── Step 6 ───────────────────────────────────────────────────────────────────
banner "Step 6: Cleanup"

# The EXIT trap handles cleanup; just print a message and exit cleanly.
echo -e "${BOLD}Demo complete. Cluster deleted and package removed.${RESET}"


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +20 to +25
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}"
}
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rm -rf "${HEADLAMP_DIR}" runs on every script exit and will unconditionally delete a headlamp/ directory next to the script, even if it existed before the demo. This is destructive if the repo already contains user data at that path. Consider using a unique temp directory (e.g., via mktemp -d) or track whether the directory was created by this run and only remove it in that case (optionally prompting before deletion).

Copilot uses AI. Check for mistakes.
Comment on lines +72 to +86
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
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script installs a ClusterRoleBinding to cluster-admin for the headlamp-admin service account. Even for a demo, this grants full cluster control and is risky if someone runs the script against a non-throwaway kubeconfig context. Prefer using the minimal RBAC recommended by Headlamp (or at least add a prominent warning and a safety check to ensure the current context is the kind-kpt-demo cluster before applying).

Copilot uses AI. Check for mistakes.
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=$!
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kubectl port-forward ... & is started in the background, but set -e won’t catch failures from a backgrounded process (e.g., service not ready, port 8080 already in use). The script will still print the URL/token even if port-forward immediately exits. Consider waiting briefly and verifying the process is still alive (kill -0 $PORT_FORWARD_PID), or retrying until the service endpoints are ready, failing the script if port-forward can’t be established.

Suggested change
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

Copilot uses AI. Check for mistakes.
run mkdir -p "${HEADLAMP_DIR}"
cd "${HEADLAMP_DIR}"

run curl -sLO https://raw.githubusercontent.com/kubernetes-sigs/headlamp/main/kubernetes-headlamp.yaml
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The manifest is fetched from Headlamp’s main branch, which can change at any time and break the demo unexpectedly. For a repeatable demo, consider pinning the URL to a specific release tag or commit SHA (and optionally verifying a checksum).

Copilot uses AI. Check for mistakes.
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
Copy link

Copilot AI Mar 22, 2026

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. If kubectl port-forward exits 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).

Suggested change
kill "${PORT_FORWARD_PID}" 2>/dev/null || true
if ps -p "${PORT_FORWARD_PID}" -o command= 2>/dev/null | grep -qE 'kubectl(\.exe)?[[:space:]].*port-forward'; then
kill "${PORT_FORWARD_PID}" 2>/dev/null || true
fi

Copilot uses AI. Check for mistakes.
Signed-off-by: Ciaran Johnston <ciaran.johnston@ericsson.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants