Files

122 lines
4.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INVENTORY_FILE="$SCRIPT_DIR/tests/expected-workloads.txt"
VERIFY_SCRIPT="$SCRIPT_DIR/verify-render.sh"
ROLLOUT_TIMEOUT="${ROLLOUT_TIMEOUT:-300s}"
EXPECTED_IMAGES=(
"192.168.1.119:8888/argoproj/argocd:v3.4.4"
"192.168.1.119:8888/dexidp/dex:v2.45.0"
"192.168.1.119:8888/library/redis:8.2.3-alpine"
)
fail() {
echo "ERROR: $*" >&2
exit 1
}
[[ "${EUID:-$(id -u)}" -eq 0 ]] || fail "this script must run as root (use sudo)"
command -v k3s >/dev/null 2>&1 || fail "k3s is required"
[[ -x "$VERIFY_SCRIPT" ]] || fail "render verifier is missing or not executable: $VERIFY_SCRIPT"
[[ -s "$INVENTORY_FILE" ]] || fail "workload inventory is missing or empty: $INVENTORY_FILE"
temporary_dir="$(mktemp -d)"
trap 'rm -rf "$temporary_dir"' EXIT
rendered_manifest="$temporary_dir/argocd-install.yaml"
cached_images="$temporary_dir/cached-images.txt"
applications_before="$temporary_dir/applications-before.txt"
applications_after="$temporary_dir/applications-after.txt"
echo "Rendering pinned Argo CD offline manifest..."
k3s kubectl kustomize "$SCRIPT_DIR" > "$rendered_manifest" \
|| fail "kustomize render failed"
"$VERIFY_SCRIPT" "$rendered_manifest"
echo "Checking the local k3s containerd image cache..."
k3s ctr images list -q | sort -u > "$cached_images" \
|| fail "could not read the k3s containerd image cache"
missing_images=()
for expected_image in "${EXPECTED_IMAGES[@]}"; do
if ! grep -Fxq "$expected_image" "$cached_images"; then
missing_images+=("$expected_image")
fi
done
if (( ${#missing_images[@]} > 0 )); then
echo "ERROR: required images are missing from the local k3s cache:" >&2
printf ' %s\n' "${missing_images[@]}" >&2
fail "restore Harbor or import the exact images before applying"
fi
if k3s kubectl get crd applications.argoproj.io >/dev/null 2>&1; then
k3s kubectl -n argocd get applications.argoproj.io -o name 2>/dev/null \
| sort > "$applications_before"
else
: > "$applications_before"
fi
echo "Applying the verified manifest..."
k3s kubectl apply --server-side --force-conflicts -f "$rendered_manifest"
while read -r resource _containers; do
[[ -n "$resource" ]] || continue
resource_kind="${resource%%/*}"
resource_name="${resource#*/}"
resource_kind="$(printf '%s' "$resource_kind" | tr '[:upper:]' '[:lower:]')"
echo "Waiting for $resource_kind/$resource_name..."
k3s kubectl -n argocd rollout status "$resource_kind/$resource_name" \
--timeout="$ROLLOUT_TIMEOUT"
done < "$INVENTORY_FILE"
echo "Verifying live pull policies..."
while read -r resource containers; do
[[ -n "$resource" && -n "$containers" ]] || continue
resource_kind="${resource%%/*}"
resource_name="${resource#*/}"
resource_kind="$(printf '%s' "$resource_kind" | tr '[:upper:]' '[:lower:]')"
live_policies="$temporary_dir/${resource_kind}-${resource_name}-policies.txt"
k3s kubectl -n argocd get "$resource_kind/$resource_name" \
-o jsonpath='{range .spec.template.spec.initContainers[*]}{.name}={.imagePullPolicy}{"\n"}{end}{range .spec.template.spec.containers[*]}{.name}={.imagePullPolicy}{"\n"}{end}' \
| sort > "$live_policies"
expected_count=0
for container in $containers; do
expected_count=$((expected_count + 1))
grep -Fxq "$container=IfNotPresent" "$live_policies" \
|| fail "$resource does not have IfNotPresent for container $container"
done
actual_count="$(wc -l < "$live_policies" | tr -d '[:space:]')"
[[ "$actual_count" -eq "$expected_count" ]] \
|| fail "$resource container inventory changed: expected $expected_count, found $actual_count"
done < "$INVENTORY_FILE"
k3s kubectl -n argocd get applications.argoproj.io -o name 2>/dev/null \
| sort > "$applications_after"
missing_applications="$(comm -23 "$applications_before" "$applications_after")"
if [[ -n "$missing_applications" ]]; then
echo "ERROR: pre-existing Argo CD Applications are missing after apply:" >&2
printf '%s\n' "$missing_applications" >&2
exit 1
fi
pod_waiting_reasons="$(
k3s kubectl -n argocd get pods \
-o jsonpath='{range .items[*]}{.metadata.name}{" "}{range .status.initContainerStatuses[*]}{.state.waiting.reason}{" "}{end}{range .status.containerStatuses[*]}{.state.waiting.reason}{" "}{end}{"\n"}{end}'
)"
if printf '%s\n' "$pod_waiting_reasons" | grep -Eq 'ErrImagePull|ImagePullBackOff'; then
printf '%s\n' "$pod_waiting_reasons" >&2
fail "an Argo CD pod is still in an image-pull failure state"
fi
echo "Argo CD offline bootstrap completed successfully."
k3s kubectl -n argocd get pods -o wide
echo "Retained pre-existing Applications: $(wc -l < "$applications_before" | tr -d '[:space:]')"