Files
harmony/fleet/scripts/run_server_install.sh
Sylvain Tremblay 286dc2b055 chore(fleet): auto-source env.sh from run_server_install.sh
run_server_install.sh now unconditionally sources
examples/fleet_server_install/env.sh after computing REPO_ROOT, so
the example's env knobs (KUBECONFIG, RUST_LOG, NO_ZITADEL,
ZITADEL_HOST, …) are picked up without the user having to source
manually before invoking the script. The script's `${VAR:-default}`
block only fills in values env.sh leaves unset.

env.sh keeps a (commented-out) KUBECONFIG hint and the new optional
Zitadel knobs documented post-source.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 08:53:32 -04:00

136 lines
6.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Test-deploy fleet_server_install against a local k3d cluster. Creates
# the cluster if it doesn't exist, builds the operator image, sideloads
# it, and runs the install Score. Leaves the cluster running at the
# end so subsequent runs (and kubectl poking) skip the bootstrap.
#
# Runnable from any cwd — paths are resolved relative to the script.
#
# Flags:
# --score-only Skip cluster create + image build + sideload; just
# refresh the kubeconfig and run the Score. The cluster
# must already exist (script errors out otherwise).
# Use when you're iterating on Score code and the
# operator binary in the cluster is still current.
#
# Env knobs (read from examples/fleet_server_install/env.sh, which is
# sourced automatically below — set them there, not in your shell):
# NO_ZITADEL=1 Skip the Zitadel install. By default Zitadel +
# a CNPG PostgreSQL cluster are installed alongside
# the operator.
# ZITADEL_HOST Hostname Zitadel ingress should answer on
# (default: `zitadel.localhost`). `.localhost` /
# `.test` hostnames default to HTTP ingress.
# ZITADEL_VERSION Override the Zitadel chart version (default
# matches the example's clap default).
#
# Known limitation — re-running after a rebuild won't redeploy:
# FleetOperatorScore delegates to HelmChartScore, which short-circuits
# when a release at the same chart_version already exists. The chart
# version is harmony's package version and doesn't change between two
# `cargo run`s, so a rebuilt operator image never reaches the cluster
# on a second run unless the helm release is removed first. The exact
# `helm uninstall` command is printed at the end of every run — copy
# it, run it, then re-invoke this script. The CRDs survive uninstall
# (helm.sh/resource-policy: keep), so any existing Deployment/Device
# CRs persist across the redeploy.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
# Always pick up the example crate's env.sh so this script's behavior
# matches what `cargo run -p example_fleet_server_install` would see
# directly. Lets the user keep KUBECONFIG, RUST_LOG, NO_ZITADEL, etc.
# in one place. The `${VAR:-default}` lines below only fill in what
# env.sh leaves unset.
# shellcheck disable=SC1091
source "$REPO_ROOT/examples/fleet_server_install/env.sh"
K3D_BIN="${K3D_BIN:-$HOME/.local/share/harmony/k3d/k3d}"
CLUSTER_NAME="${CLUSTER_NAME:-fleet-server-test}"
NATS_NODE_PORT="${NATS_NODE_PORT:-4222}"
KUBECONFIG_FILE="${KUBECONFIG_FILE:-/tmp/fleet-server-test-kubeconfig}"
OPERATOR_IMAGE_TAG="${OPERATOR_IMAGE_TAG:-dev}"
OPERATOR_IMAGE="hub.nationtech.io/harmony/harmony-fleet-operator:${OPERATOR_IMAGE_TAG}"
NO_ZITADEL="${NO_ZITADEL:-0}"
ZITADEL_HOST="${ZITADEL_HOST:-zitadel.localhost}"
ZITADEL_VERSION="${ZITADEL_VERSION:-}"
SCORE_ONLY=0
for arg in "$@"; do
case "$arg" in
--score-only) SCORE_ONLY=1 ;;
*) echo "unknown argument: $arg" >&2; exit 1 ;;
esac
done
# `cargo run -p ...` and the build script both expect to operate
# inside the workspace; the script can be invoked from any cwd.
cd "$REPO_ROOT"
# 1. Ensure k3d cluster exists with NATS port mapped to the host.
if "$K3D_BIN" cluster list 2>/dev/null | awk 'NR>1 {print $1}' | grep -qx "$CLUSTER_NAME"; then
echo "k3d cluster '$CLUSTER_NAME' already exists; reusing"
elif [[ "$SCORE_ONLY" == "1" ]]; then
echo "ERROR: --score-only set but cluster '$CLUSTER_NAME' missing — drop --score-only to bootstrap" >&2
exit 1
else
echo "creating k3d cluster '$CLUSTER_NAME' (host port $NATS_NODE_PORT -> loadbalancer)"
"$K3D_BIN" cluster create "$CLUSTER_NAME" \
--wait --timeout 90s \
-p "${NATS_NODE_PORT}:${NATS_NODE_PORT}@loadbalancer"
fi
if [[ "$SCORE_ONLY" != "1" ]]; then
# 2. Build the operator image (PUSH=0 keeps it local — no Harbor push).
echo "building operator image $OPERATOR_IMAGE"
DOCKER_TAG="$OPERATOR_IMAGE_TAG" PUSH=0 \
bash fleet/harmony-fleet-operator/build_docker.sh
# 3. Sideload the image into the k3d cluster. `k3d image import` reads
# from the local docker daemon — same place build_docker.sh wrote it.
echo "sideloading $OPERATOR_IMAGE into k3d cluster '$CLUSTER_NAME'"
"$K3D_BIN" image import "$OPERATOR_IMAGE" -c "$CLUSTER_NAME"
else
echo "--score-only: skipping operator build + sideload (assuming '$OPERATOR_IMAGE' is already in the cluster)"
fi
# 4. Hand kubectl/helm a kubeconfig pointing at the k3d cluster.
"$K3D_BIN" kubeconfig get "$CLUSTER_NAME" > "$KUBECONFIG_FILE"
export KUBECONFIG="$KUBECONFIG_FILE"
# 5. Build Zitadel flags. Zitadel is on by default; set NO_ZITADEL=1 to
# skip it (the example treats absence of --zitadel-host as the
# skip signal).
ZITADEL_ARGS=()
if [[ "$NO_ZITADEL" != "1" ]]; then
ZITADEL_ARGS+=(--zitadel-host "$ZITADEL_HOST")
if [[ -n "$ZITADEL_VERSION" ]]; then
ZITADEL_ARGS+=(--zitadel-version "$ZITADEL_VERSION")
fi
fi
# 6. Run the FleetServerScore via the example binary.
cargo run -p example_fleet_server_install -- \
--nats-namespace fleet-system \
--nats-name fleet-nats \
--nats-expose load-balancer \
--operator-namespace fleet-system \
--operator-release harmony-fleet-operator \
--operator-image "$OPERATOR_IMAGE" \
--operator-image-pull-policy IfNotPresent \
--log-level "info,kube_runtime=warn" \
"${ZITADEL_ARGS[@]}"
echo
echo "k3d cluster '$CLUSTER_NAME' left running."
echo " KUBECONFIG=$KUBECONFIG_FILE"
echo " tear down cluster: $K3D_BIN cluster delete $CLUSTER_NAME"
echo " redeploy operator: helm uninstall harmony-fleet-operator -n fleet-system --kubeconfig $KUBECONFIG_FILE && bash $0"
echo " (CRDs survive uninstall via helm.sh/resource-policy: keep — Deployment/Device CRs persist)"
if [[ "$NO_ZITADEL" != "1" ]]; then
echo " redeploy Zitadel: helm uninstall zitadel -n zitadel --kubeconfig $KUBECONFIG_FILE && bash $0"
fi