chart: refuse to render without an explicit namespace

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.
This commit is contained in:
TapTap committed 2026-11-14 21:59:48 +00:00
1 parent 0cd0b9582d
commit 8e87fed93f
11 files changed
+120 -10

No files matched your search

+1 -3
View File
@@ -35,9 +35,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Lint and render the chart
run: |
helm lint charts/audiomanager
helm template audiomanager charts/audiomanager --set users.existingSecret=test > /dev/null
run: scripts/check-chart.sh
chart-push:
needs:
+9 -1
View File
@@ -259,6 +259,7 @@ Image: gitea.tap-tap.win/taptap/audiomanager:<tag>
Chart: https://gitea.tap-tap.win/TapTap/Charts/raw/branch/main
```
Kustomize example:
```yaml
@@ -271,6 +272,12 @@ helmCharts:
valuesFile: values.yaml
```
The chart renders every resource into the namespace passed to Helm
(`--namespace` or `helmCharts[].namespace`) and refuses to render when that
namespace is missing or falls back to `default`, so a forgotten namespace cannot
deploy into the wrong place. Set `allowDefaultNamespace=true` to opt in to
`default`. `scripts/check-chart.sh` lints the chart and enforces this in CI.
The chart expects:
- a `users.yaml` Secret (Vault-managed) referenced by `users.existingSecret`;
@@ -300,8 +307,9 @@ The shared module owns the API client, auth store, SQLDelight offline database,
## Testing
```bash
gradle test # unit + API integration tests
gradle test # unit + API integration tests
GRADLE=gradle scripts/smoke.sh # end-to-end server smoke test
scripts/check-chart.sh # lint the chart and enforce the namespace rule
```
The smoke test generates tagged MP3s, starts the server, and verifies login, sync, moods, radio, add-to-mood, streaming, transcoding, covers, scan and logout.
+1 -1
View File
@@ -2,5 +2,5 @@ apiVersion: v2
name: audiomanager
description: Self-hosted OpenSubsonic music server
type: application
version: 0.2.0
version: 0.2.1
appVersion: "0.1.0"
@@ -31,3 +31,22 @@ app.kubernetes.io/instance: {{ .Release.Name }}
{{- define "audiomanager.image" -}}
{{- printf "%s:%s" .Values.image.repository (default .Chart.AppVersion .Values.image.tag) -}}
{{- end -}}
{{/*
The namespace every resource is rendered into.
Helm falls back to "default" when --namespace is omitted, which would silently
put the release somewhere the operator did not ask for. Refuse to render in
that case unless the fallback is explicitly allowed, so a forgotten --namespace
(or a kustomization without namespace:) fails instead of deploying.
*/}}
{{- define "audiomanager.namespace" -}}
{{- $namespace := .Release.Namespace -}}
{{- if not $namespace -}}
{{- fail "a namespace is required: pass --namespace <namespace> (or set namespace: in the Kustomize helmCharts entry)" -}}
{{- end -}}
{{- if and (eq $namespace "default") (not .Values.allowDefaultNamespace) -}}
{{- fail "refusing to deploy into the implicit \"default\" namespace: pass --namespace <namespace>, or set allowDefaultNamespace=true to opt in" -}}
{{- end -}}
{{- $namespace -}}
{{- end -}}
@@ -2,7 +2,7 @@ apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "audiomanager.fullname" . }}-library
namespace: {{ .Release.Namespace }}
namespace: {{ include "audiomanager.namespace" . }}
labels:
{{- include "audiomanager.labels" . | nindent 4 }}
data:
@@ -2,7 +2,7 @@ apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "audiomanager.fullname" . }}
namespace: {{ .Release.Namespace }}
namespace: {{ include "audiomanager.namespace" . }}
labels:
{{- include "audiomanager.labels" . | nindent 4 }}
spec:
+1 -1
View File
@@ -3,7 +3,7 @@ apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ include "audiomanager.fullname" . }}
namespace: {{ .Release.Namespace }}
namespace: {{ include "audiomanager.namespace" . }}
labels:
{{- include "audiomanager.labels" . | nindent 4 }}
{{- with .Values.ingress.annotations }}
+1 -1
View File
@@ -3,7 +3,7 @@ apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ include "audiomanager.fullname" . }}-data
namespace: {{ .Release.Namespace }}
namespace: {{ include "audiomanager.namespace" . }}
labels:
{{- include "audiomanager.labels" . | nindent 4 }}
spec:
+1 -1
View File
@@ -2,7 +2,7 @@ apiVersion: v1
kind: Service
metadata:
name: {{ include "audiomanager.fullname" . }}
namespace: {{ .Release.Namespace }}
namespace: {{ include "audiomanager.namespace" . }}
labels:
{{- include "audiomanager.labels" . | nindent 4 }}
spec:
+5
View File
@@ -1,5 +1,10 @@
replicaCount: 1
# Every resource is rendered into the namespace passed with --namespace (or
# helmCharts[].namespace in Kustomize). Rendering fails when that namespace is
# missing or falls back to "default"; set this to true to allow "default".
allowDefaultNamespace: false
image:
repository: gitea.tap-tap.win/taptap/audiomanager
# CI publishes :latest and :sha-<commit> for every push to main.
+80
View File
@@ -0,0 +1,80 @@
#!/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"