Helm falls back to the "default" namespace when --namespace is omitted, so a forgotten flag (or a Kustomize helmCharts entry without namespace) silently deployed into the wrong place. Render the namespace through one helper that fails on an empty or implicit "default" namespace, with allowDefaultNamespace as an explicit opt-in. scripts/check-chart.sh lints the chart, asserts every namespaced resource carries the passed namespace, and checks both failure and opt-in paths. CI runs it instead of the plain template call. Bump the chart to 0.2.1.
81 lines
2.8 KiB
Bash
Executable File
81 lines
2.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# Render and verify the Helm chart. Every namespaced resource must be rendered
|
|
# into the namespace that was passed to Helm, and rendering must fail when no
|
|
# namespace is passed instead of silently falling back to "default".
|
|
#
|
|
# Requires helm on PATH (`nix-shell` provides it).
|
|
set -euo pipefail
|
|
|
|
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
CHART_DIR="${CHART_DIR:-$ROOT/charts/audiomanager}"
|
|
RELEASE="${RELEASE:-audiomanager}"
|
|
NAMESPACE="${NAMESPACE:-audiomanager}"
|
|
|
|
workdir="$(mktemp -d "${TMPDIR:-/tmp}/chart-check.XXXXXX")"
|
|
trap 'rm -rf "$workdir"' EXIT
|
|
|
|
# An empty kubeconfig makes the implicit namespace deterministic ("default"),
|
|
# independent of whatever context the developer or runner has configured.
|
|
(umask 077 && : > "$workdir/kubeconfig")
|
|
|
|
echo "helm lint ($NAMESPACE)"
|
|
helm lint "$CHART_DIR" --namespace "$NAMESPACE" >/dev/null
|
|
|
|
echo "render ($NAMESPACE)"
|
|
helm template "$RELEASE" "$CHART_DIR" \
|
|
--namespace "$NAMESPACE" \
|
|
--set users.existingSecret=test \
|
|
> "$workdir/rendered.yaml"
|
|
|
|
# A namespace per document: fail if a namespaced kind has no metadata.namespace
|
|
# or carries a different one. Cluster-scoped kinds are listed explicitly so a
|
|
# new template cannot quietly skip the namespace.
|
|
NAMESPACE="$NAMESPACE" awk '
|
|
function cluster_scoped(kind) {
|
|
return kind == "Namespace" \
|
|
|| kind ~ /^Cluster/ \
|
|
|| kind ~ /^(PersistentVolume|StorageClass|IngressClass|Node|PriorityClass)$/ \
|
|
|| kind ~ /^(CustomResourceDefinition|APIService|RuntimeClass|CSIDriver|CSINode|VolumeAttachment)$/ \
|
|
|| kind ~ /(WebhookConfiguration)$/
|
|
}
|
|
|
|
function check_document() {
|
|
if (kind == "") return
|
|
if (cluster_scoped(kind)) return
|
|
if (namespace == "") {
|
|
printf "FAIL: %s has no metadata.namespace\n", kind
|
|
failed = 1
|
|
} else if (namespace != ENVIRON["NAMESPACE"]) {
|
|
printf "FAIL: %s is in namespace %s, want %s\n", kind, namespace, ENVIRON["NAMESPACE"]
|
|
failed = 1
|
|
}
|
|
kind = ""
|
|
namespace = ""
|
|
}
|
|
|
|
/^---[[:space:]]*$/ { check_document(); next }
|
|
/^kind:[[:space:]]/ { kind = $2 }
|
|
/^metadata:[[:space:]]*$/ { in_metadata = 1; next }
|
|
/^[^[:space:]]/ { in_metadata = 0 }
|
|
in_metadata && /^ namespace:[[:space:]]/ { namespace = $2; in_metadata = 0 }
|
|
END { check_document(); exit failed }
|
|
' "$workdir/rendered.yaml"
|
|
|
|
echo "render without a namespace must fail"
|
|
if helm template "$RELEASE" "$CHART_DIR" \
|
|
--kubeconfig "$workdir/kubeconfig" \
|
|
--set users.existingSecret=test \
|
|
> "$workdir/without-namespace.yaml" 2>/dev/null; then
|
|
echo "FAIL: the chart rendered without --namespace" >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo "render with allowDefaultNamespace=true"
|
|
helm template "$RELEASE" "$CHART_DIR" \
|
|
--kubeconfig "$workdir/kubeconfig" \
|
|
--set users.existingSecret=test \
|
|
--set allowDefaultNamespace=true >/dev/null
|
|
|
|
echo "chart check passed"
|