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:
1 parent
0cd0b9582d
commit
8e87fed93f
11 files changed
+119
-9
No files matched your search
@@ -35,9 +35,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Lint and render the chart
|
- name: Lint and render the chart
|
||||||
run: |
|
run: scripts/check-chart.sh
|
||||||
helm lint charts/audiomanager
|
|
||||||
helm template audiomanager charts/audiomanager --set users.existingSecret=test > /dev/null
|
|
||||||
|
|
||||||
chart-push:
|
chart-push:
|
||||||
needs:
|
needs:
|
||||||
|
|||||||
@@ -259,6 +259,7 @@ Image: gitea.tap-tap.win/taptap/audiomanager:<tag>
|
|||||||
Chart: https://gitea.tap-tap.win/TapTap/Charts/raw/branch/main
|
Chart: https://gitea.tap-tap.win/TapTap/Charts/raw/branch/main
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Kustomize example:
|
Kustomize example:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
@@ -271,6 +272,12 @@ helmCharts:
|
|||||||
valuesFile: values.yaml
|
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:
|
The chart expects:
|
||||||
|
|
||||||
- a `users.yaml` Secret (Vault-managed) referenced by `users.existingSecret`;
|
- a `users.yaml` Secret (Vault-managed) referenced by `users.existingSecret`;
|
||||||
@@ -302,6 +309,7 @@ The shared module owns the API client, auth store, SQLDelight offline database,
|
|||||||
```bash
|
```bash
|
||||||
gradle test # unit + API integration tests
|
gradle test # unit + API integration tests
|
||||||
GRADLE=gradle scripts/smoke.sh # end-to-end server smoke test
|
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.
|
The smoke test generates tagged MP3s, starts the server, and verifies login, sync, moods, radio, add-to-mood, streaming, transcoding, covers, scan and logout.
|
||||||
|
|||||||
@@ -2,5 +2,5 @@ apiVersion: v2
|
|||||||
name: audiomanager
|
name: audiomanager
|
||||||
description: Self-hosted OpenSubsonic music server
|
description: Self-hosted OpenSubsonic music server
|
||||||
type: application
|
type: application
|
||||||
version: 0.2.0
|
version: 0.2.1
|
||||||
appVersion: "0.1.0"
|
appVersion: "0.1.0"
|
||||||
@@ -31,3 +31,22 @@ app.kubernetes.io/instance: {{ .Release.Name }}
|
|||||||
{{- define "audiomanager.image" -}}
|
{{- define "audiomanager.image" -}}
|
||||||
{{- printf "%s:%s" .Values.image.repository (default .Chart.AppVersion .Values.image.tag) -}}
|
{{- printf "%s:%s" .Values.image.repository (default .Chart.AppVersion .Values.image.tag) -}}
|
||||||
{{- end -}}
|
{{- 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
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: {{ include "audiomanager.fullname" . }}-library
|
name: {{ include "audiomanager.fullname" . }}-library
|
||||||
namespace: {{ .Release.Namespace }}
|
namespace: {{ include "audiomanager.namespace" . }}
|
||||||
labels:
|
labels:
|
||||||
{{- include "audiomanager.labels" . | nindent 4 }}
|
{{- include "audiomanager.labels" . | nindent 4 }}
|
||||||
data:
|
data:
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ apiVersion: apps/v1
|
|||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: {{ include "audiomanager.fullname" . }}
|
name: {{ include "audiomanager.fullname" . }}
|
||||||
namespace: {{ .Release.Namespace }}
|
namespace: {{ include "audiomanager.namespace" . }}
|
||||||
labels:
|
labels:
|
||||||
{{- include "audiomanager.labels" . | nindent 4 }}
|
{{- include "audiomanager.labels" . | nindent 4 }}
|
||||||
spec:
|
spec:
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ apiVersion: networking.k8s.io/v1
|
|||||||
kind: Ingress
|
kind: Ingress
|
||||||
metadata:
|
metadata:
|
||||||
name: {{ include "audiomanager.fullname" . }}
|
name: {{ include "audiomanager.fullname" . }}
|
||||||
namespace: {{ .Release.Namespace }}
|
namespace: {{ include "audiomanager.namespace" . }}
|
||||||
labels:
|
labels:
|
||||||
{{- include "audiomanager.labels" . | nindent 4 }}
|
{{- include "audiomanager.labels" . | nindent 4 }}
|
||||||
{{- with .Values.ingress.annotations }}
|
{{- with .Values.ingress.annotations }}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ apiVersion: v1
|
|||||||
kind: PersistentVolumeClaim
|
kind: PersistentVolumeClaim
|
||||||
metadata:
|
metadata:
|
||||||
name: {{ include "audiomanager.fullname" . }}-data
|
name: {{ include "audiomanager.fullname" . }}-data
|
||||||
namespace: {{ .Release.Namespace }}
|
namespace: {{ include "audiomanager.namespace" . }}
|
||||||
labels:
|
labels:
|
||||||
{{- include "audiomanager.labels" . | nindent 4 }}
|
{{- include "audiomanager.labels" . | nindent 4 }}
|
||||||
spec:
|
spec:
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ apiVersion: v1
|
|||||||
kind: Service
|
kind: Service
|
||||||
metadata:
|
metadata:
|
||||||
name: {{ include "audiomanager.fullname" . }}
|
name: {{ include "audiomanager.fullname" . }}
|
||||||
namespace: {{ .Release.Namespace }}
|
namespace: {{ include "audiomanager.namespace" . }}
|
||||||
labels:
|
labels:
|
||||||
{{- include "audiomanager.labels" . | nindent 4 }}
|
{{- include "audiomanager.labels" . | nindent 4 }}
|
||||||
spec:
|
spec:
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
replicaCount: 1
|
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:
|
image:
|
||||||
repository: gitea.tap-tap.win/taptap/audiomanager
|
repository: gitea.tap-tap.win/taptap/audiomanager
|
||||||
# CI publishes :latest and :sha-<commit> for every push to main.
|
# CI publishes :latest and :sha-<commit> for every push to main.
|
||||||
|
|||||||
Executable
+80
@@ -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"
|
||||||
Reference in new issue
Block a user