From fa89a7360662372f891bb0b682ad624cd380e84d Mon Sep 17 00:00:00 2001 From: Saifuddin Rangwala Date: Wed, 25 Nov 2026 19:02:45 +0530 Subject: [PATCH 1/4] salt-minion: run as a StatefulSet with a unique ID and keypair per replica With replicas > 1 the Deployment made every pod register as the same minion (same agent.minion.id) using the same keypair. - Deployment -> StatefulSet. agent.minion.idPrefix names the StatefulSet, so pod N is minion -N. - replicas > 1 rejects agent.minion.id and the single static privateKeyB64/publicKeyB64 pair. - keySecretName holds .pem/.pub pairs when replicas > 1; each pod mounts only its own pair (items + subPathExpr). - The keygen Job generates one pair per replica, only adds missing pairs on scale-up, and its Role gains patch. - Tests and docs updated; replicas: 1 behaves as before. Co-Authored-By: Claude Sonnet 5.5 --- helm/salt-minion/CHANGELOG.md | 15 ++ helm/salt-minion/README.md | 37 +++- helm/salt-minion/templates/_helpers.tpl | 42 +++++ helm/salt-minion/templates/keygen-job.yaml | 64 ++++++- .../templates/minion-keygen-role.yaml | 2 +- .../{deployment.yaml => statefulset.yaml} | 36 +++- helm/salt-minion/tests/deployment_test.yaml | 82 --------- helm/salt-minion/tests/images_test.yaml | 18 +- helm/salt-minion/tests/keygen_job_test.yaml | 21 +++ .../tests/minion_keygen_role_test.yaml | 11 +- helm/salt-minion/tests/statefulset_test.yaml | 159 ++++++++++++++++++ helm/salt-minion/values.yaml | 14 +- 12 files changed, 402 insertions(+), 99 deletions(-) rename helm/salt-minion/templates/{deployment.yaml => statefulset.yaml} (80%) delete mode 100644 helm/salt-minion/tests/deployment_test.yaml create mode 100644 helm/salt-minion/tests/statefulset_test.yaml diff --git a/helm/salt-minion/CHANGELOG.md b/helm/salt-minion/CHANGELOG.md index 2b65d85..0831a85 100644 --- a/helm/salt-minion/CHANGELOG.md +++ b/helm/salt-minion/CHANGELOG.md @@ -26,6 +26,21 @@ versions it carries — check here. ## Chart releases (`salt-minion-chart/vX.Y.Z`) +### Unreleased +- `Deployment` -> `StatefulSet` (`templates/deployment.yaml` is now `templates/statefulset.yaml`), + so every replica gets a stable, unique identity. Reinstall rather than `helm upgrade`: a + Deployment cannot be converted in place. +- Previously `agent.replicas > 1` made every pod register as one and the same minion, all + sharing one keypair. Now each pod is its own minion (`` as ID) with its own + keypair. New `agent.minion.idPrefix` names the StatefulSet and the pods. +- With `agent.replicas > 1` the chart rejects `agent.minion.id` and + `agent.minion.privateKeyB64`/`publicKeyB64` (single-identity settings). `keySecretName` + then points at a Secret holding `.pem`/`.pub` pairs; each pod mounts only its own. +- The keygen Job generates one keypair per replica, only adds missing pairs when scaling up, + and is named `-keygen-` when `replicas > 1`. Its Role gains `patch`. +- With `agent.replicas: 1` the Secret layout (`minion.pem`/`minion.pub`) and `agent.minion.id` + behave as before. + ### salt-minion-chart/v1.0.0 **Breaking - defaults change on `helm upgrade` from any prior release:** - `agent.minion.keySecretName` is now **required** - the chart fails fast diff --git a/helm/salt-minion/README.md b/helm/salt-minion/README.md index 068d4a6..e9e3845 100644 --- a/helm/salt-minion/README.md +++ b/helm/salt-minion/README.md @@ -192,9 +192,10 @@ See [values.yaml](values.yaml) for the complete list of configuration options. | `agent.saltMasterHost` | Salt master hostname or comma-separated list (REQUIRED) | `""` | | `agent.saltMasterPort` | Master request/return port | `4506` | | `agent.saltPublishPort` | Master publish port | `4505` | -| `agent.replicas` | Number of minion pod replicas | `1` | +| `agent.replicas` | Number of minion pods. Each pod is a separate minion with its own ID and keypair (see "Multiple replicas") | `1` | | `agent.logLevel` | Minion log level | `info` | -| `agent.minion.id` | Minion ID (uses pod hostname if empty) | `""` | +| `agent.minion.id` | Minion ID (uses pod hostname if empty). Only with `agent.replicas: 1` | `""` | +| `agent.minion.idPrefix` | Names the StatefulSet; pod `N` becomes minion `-N`. Default StatefulSet name `salt-minion` | `""` | | `agent.vault.addr` | Vault server address (enables saltext.vault) | `""` | | `rbac.create` | Create RBAC resources | `true` | | `rbac.fullAccess` | Grant full cluster admin rights | `true` | @@ -256,6 +257,38 @@ helm install salt-minion ./helm/salt-minion \ --set agent.minion.allowSelfGeneratedKey=true ``` +### Multiple replicas + +The minion runs as a StatefulSet, and every replica is a **separate minion** with +its own ID (the pod name) and its own keypair. Set `agent.minion.idPrefix` to choose +the names: + +```bash +helm install salt-minion ./helm/salt-minion \ + --set agent.saltMasterHost=salt-master.example.com \ + --set agent.replicas=3 \ + --set agent.minion.idPrefix=external-minion \ + --set agent.minion.allowSelfGeneratedKey=true # minions: external-minion-0, -1, -2 +``` + +- `agent.minion.id` and `agent.minion.privateKeyB64`/`publicKeyB64` describe a single + identity, so the chart refuses them when `agent.replicas > 1`. +- With `allowSelfGeneratedKey`, the keygen Job creates one Secret holding a pair per pod + (`.pem` / `.pub`). Scaling up only adds the missing pairs; existing keys are + never regenerated. Each pod mounts only its own pair. +- With your own keys, create that Secret yourself (same key names) and set + `agent.minion.keySecretName`: + + ```bash + kubectl create secret generic my-minion-keys -n salt \ + --from-file=external-minion-0.pem=./0.pem --from-file=external-minion-0.pub=./0.pub \ + --from-file=external-minion-1.pem=./1.pem --from-file=external-minion-1.pub=./1.pub + ``` +- Every pod's public key must be accepted by the master, for example one `SaltMinionKey` + per pod. +- Scaling down leaves the extra pairs in the Secret. Remove them, and the minion keys on + the master, if you don't want them. + ### Health Checks The minion includes both liveness and readiness probes: diff --git a/helm/salt-minion/templates/_helpers.tpl b/helm/salt-minion/templates/_helpers.tpl index f337480..e1784ca 100644 --- a/helm/salt-minion/templates/_helpers.tpl +++ b/helm/salt-minion/templates/_helpers.tpl @@ -49,15 +49,57 @@ true {{- define "salt-minion.keygenJobEnabled" -}} {{- if include "salt-minion.keygenEnabled" . -}} +{{- if include "salt-minion.perPodKeys" . -}} +true +{{- else -}} {{- $secretName := include "salt-minion.keySecretName" . -}} {{- if not (lookup "v1" "Secret" .Values.namespace $secretName) -}} true {{- end -}} {{- end -}} {{- end -}} +{{- end -}} {{- define "salt-minion.keygenRbacEnabled" -}} {{- if and .Values.rbac.create (include "salt-minion.keygenEnabled" .) -}} true {{- end -}} {{- end -}} + +{{/* +StatefulSet name. Pods are named - and the entrypoint falls +back to the pod hostname for the minion ID, so this doubles as the ID prefix. +*/}} +{{- define "salt-minion.workloadName" -}} +{{- .Values.agent.minion.idPrefix | default "salt-minion" -}} +{{- end -}} + +{{/* Space-separated pod names (== minion IDs) of every replica. */}} +{{- define "salt-minion.podNames" -}} +{{- $name := include "salt-minion.workloadName" . -}} +{{- $names := list -}} +{{- range $i := until (int .Values.agent.replicas) -}} +{{- $names = append $names (printf "%s-%d" $name $i) -}} +{{- end -}} +{{- join " " $names -}} +{{- end -}} + +{{/* "true" when each replica needs its own keypair (replicas > 1). */}} +{{- define "salt-minion.perPodKeys" -}} +{{- if gt (int .Values.agent.replicas) 1 -}} +true +{{- end -}} +{{- end -}} + +{{/* Every replica is a separate minion, so it needs its own ID and keypair. */}} +{{- define "salt-minion.validateReplicas" -}} +{{- $minion := .Values.agent.minion -}} +{{- if include "salt-minion.perPodKeys" . -}} +{{- if $minion.id -}} +{{- fail "agent.minion.id cannot be used with agent.replicas > 1 (every pod would register as the same minion); use agent.minion.idPrefix to get -0, -1, ..." -}} +{{- end -}} +{{- if $minion.privateKeyB64 -}} +{{- fail "agent.minion.privateKeyB64/publicKeyB64 hold a single keypair and cannot be used with agent.replicas > 1; set agent.minion.keySecretName to a Secret with per-pod keys, or enable agent.minion.allowSelfGeneratedKey" -}} +{{- end -}} +{{- end -}} +{{- end -}} diff --git a/helm/salt-minion/templates/keygen-job.yaml b/helm/salt-minion/templates/keygen-job.yaml index 9117235..08e738f 100644 --- a/helm/salt-minion/templates/keygen-job.yaml +++ b/helm/salt-minion/templates/keygen-job.yaml @@ -3,7 +3,9 @@ apiVersion: batch/v1 kind: Job metadata: - name: {{ .Release.Name }}-keygen + # Per-pod keys: the Job is idempotent and re-created per replica count, so + # scaling up generates only the missing keys (Job specs are immutable). + name: {{ .Release.Name }}-keygen{{ if include "salt-minion.perPodKeys" . }}-{{ .Values.agent.replicas }}{{ end }} namespace: {{ .Values.namespace }} spec: backoffLimit: 3 @@ -27,6 +29,65 @@ spec: secret={{ $secretName | quote }} namespace={{ .Values.namespace | quote }} + {{- if include "salt-minion.perPodKeys" . }} + pods={{ include "salt-minion.podNames" . | quote }} + + temporary_directory="$(mktemp -d)" + trap 'rm -rf "$temporary_directory"' EXIT + + exists=false + if kubectl get secret "$secret" -n "$namespace" >/dev/null 2>&1; then + exists=true + fi + + # One keypair per pod, stored as .pem / .pub. Existing + # pairs are never regenerated: the master already trusts them. + new="" + for pod in $pods; do + if [ "$exists" = true ] && \ + [ -n "$(kubectl get secret "$secret" -n "$namespace" \ + -o "jsonpath={.data['$pod.pem']}")" ]; then + echo "Keys for $pod already exist; skipping" + continue + fi + + openssl genpkey \ + -algorithm RSA \ + -pkeyopt rsa_keygen_bits:2048 \ + -out "$temporary_directory/$pod.pem" + + openssl rsa \ + -pubout \ + -in "$temporary_directory/$pod.pem" \ + -out "$temporary_directory/$pod.pub" + + new="$new $pod" + done + + if [ -z "$new" ]; then + echo "All keys already exist; nothing to do" + exit 0 + fi + + if [ "$exists" = false ]; then + set -- + for pod in $new; do + set -- "$@" \ + --from-file="$pod.pem=$temporary_directory/$pod.pem" \ + --from-file="$pod.pub=$temporary_directory/$pod.pub" + done + kubectl create secret generic "$secret" \ + --namespace "$namespace" "$@" + else + for pod in $new; do + pem="$(base64 < "$temporary_directory/$pod.pem" | tr -d '\n')" + pub="$(base64 < "$temporary_directory/$pod.pub" | tr -d '\n')" + kubectl patch secret "$secret" --namespace "$namespace" \ + --type merge \ + -p "{\"data\":{\"$pod.pem\":\"$pem\",\"$pod.pub\":\"$pub\"}}" + done + fi + {{- else }} if kubectl get secret "$secret" -n "$namespace" >/dev/null 2>&1; then echo "Secret $secret already exists; skipping generation" @@ -50,4 +111,5 @@ spec: --namespace "$namespace" \ --from-file=minion.pem="$temporary_directory/minion.pem" \ --from-file=minion.pub="$temporary_directory/minion.pub" + {{- end }} {{- end }} diff --git a/helm/salt-minion/templates/minion-keygen-role.yaml b/helm/salt-minion/templates/minion-keygen-role.yaml index 09e2d66..86b421a 100644 --- a/helm/salt-minion/templates/minion-keygen-role.yaml +++ b/helm/salt-minion/templates/minion-keygen-role.yaml @@ -7,5 +7,5 @@ metadata: rules: - apiGroups: [""] resources: ["secrets"] - verbs: ["create", "get", "list"] + verbs: ["create", "get", "list", "patch"] {{- end }} diff --git a/helm/salt-minion/templates/deployment.yaml b/helm/salt-minion/templates/statefulset.yaml similarity index 80% rename from helm/salt-minion/templates/deployment.yaml rename to helm/salt-minion/templates/statefulset.yaml index 0c310a9..d9a5d93 100644 --- a/helm/salt-minion/templates/deployment.yaml +++ b/helm/salt-minion/templates/statefulset.yaml @@ -1,10 +1,11 @@ {{- if eq .Values.agent.authMode "in_cluster" }} {{- include "salt-minion.validateKeySource" . -}} +{{- include "salt-minion.validateReplicas" . -}} {{- $keySecret := include "salt-minion.keySecretName" . }} apiVersion: apps/v1 -kind: Deployment +kind: StatefulSet metadata: - name: salt-minion + name: {{ include "salt-minion.workloadName" . }} namespace: {{ .Values.namespace }} labels: app: salt-minion @@ -12,7 +13,12 @@ metadata: app.kubernetes.io/managed-by: {{ .Release.Service }} helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }} spec: + # No Service is needed: a StatefulSet only uses serviceName for pod DNS, + # and minions connect outbound to the master. + serviceName: {{ include "salt-minion.workloadName" . }} replicas: {{ .Values.agent.replicas }} + # Minions are independent of each other, so start/replace them in parallel. + podManagementPolicy: Parallel selector: matchLabels: app: salt-minion @@ -40,6 +46,11 @@ spec: - name: prepare-minion-keys image: {{ include "salt-minion.agentImage" . | quote }} imagePullPolicy: {{ .Values.agent.image.pullPolicy }} + env: + - name: POD_NAME + valueFrom: + fieldRef: + fieldPath: metadata.name command: - /bin/sh - -c @@ -62,6 +73,11 @@ spec: volumeMounts: - name: minion-keys-source mountPath: /var/run/salt-minion-keys + {{- if include "salt-minion.perPodKeys" . }} + # Each pod mounts only its own / directory of the + # shared Secret, so it never sees the other replicas' keys. + subPathExpr: $(POD_NAME) + {{- end }} readOnly: true - name: minion-pki mountPath: /pki @@ -73,6 +89,11 @@ spec: image: {{ include "salt-minion.agentImage" . | quote }} imagePullPolicy: {{ .Values.agent.image.pullPolicy }} env: + - name: POD_NAME + valueFrom: + fieldRef: + fieldPath: metadata.name + - name: SALT_MASTER {{- if kindIs "slice" .Values.agent.saltMasterHost }} value: {{ join "," .Values.agent.saltMasterHost | quote }} @@ -173,6 +194,17 @@ spec: secret: secretName: {{ $keySecret }} defaultMode: 0440 + {{- if include "salt-minion.perPodKeys" . }} + # Secret keys are .pem / .pub; map each pair + # to /minion.pem|pub so a pod can mount just its own. + items: + {{- range $pod := splitList " " (include "salt-minion.podNames" .) }} + - key: {{ $pod }}.pem + path: {{ $pod }}/minion.pem + - key: {{ $pod }}.pub + path: {{ $pod }}/minion.pub + {{- end }} + {{- end }} - name: minion-pki emptyDir: {} diff --git a/helm/salt-minion/tests/deployment_test.yaml b/helm/salt-minion/tests/deployment_test.yaml deleted file mode 100644 index 53cbe3e..0000000 --- a/helm/salt-minion/tests/deployment_test.yaml +++ /dev/null @@ -1,82 +0,0 @@ -suite: minion deployment -templates: - - templates/deployment.yaml -tests: - - it: prepares keys with a group-readable source - set: - agent.minion.allowSelfGeneratedKey: true - asserts: - - equal: - path: spec.template.spec.securityContext.fsGroup - value: 10001 - - equal: - path: spec.template.spec.volumes[?(@.name == "minion-keys-source")].secret.defaultMode - value: 288 # 0440: owner and group read access - - equal: - path: spec.template.spec.volumes[?(@.name == "minion-keys-source")].secret.secretName - value: RELEASE-NAME-keys - - equal: - path: spec.template.spec.initContainers[?(@.name == "prepare-minion-keys")].volumeMounts[?(@.name == "minion-pki")].mountPath - value: /pki - - equal: - path: spec.template.spec.containers[?(@.name == "salt-minion")].volumeMounts[?(@.name == "minion-pki")] - value: - name: minion-pki - mountPath: /etc/salt/pki/minion - subPath: minion - - - it: shares the configured security context with key preparation - set: - agent.minion.keySecretName: existing-keys - agent.podSecurityContext.fsGroup: 2000 - agent.containerSecurityContext.runAsUser: 2000 - agent.containerSecurityContext.runAsGroup: 2000 - asserts: - - equal: - path: spec.template.spec.securityContext.fsGroup - value: 2000 - - equal: - path: spec.template.spec.initContainers[?(@.name == "prepare-minion-keys")].securityContext - value: - runAsUser: 2000 - runAsGroup: 2000 - runAsNonRoot: true - allowPrivilegeEscalation: false - capabilities: - drop: [ALL] - - equal: - path: spec.template.spec.containers[?(@.name == "salt-minion")].securityContext - value: - runAsUser: 2000 - runAsGroup: 2000 - runAsNonRoot: true - allowPrivilegeEscalation: false - capabilities: - drop: [ALL] - - - it: uses the requested existing Secret - set: - agent.minion.keySecretName: existing-keys - asserts: - - equal: - path: spec.template.spec.volumes[?(@.name == "minion-keys-source")].secret.secretName - value: existing-keys - - - it: renders external mode without requiring keys - set: - agent.authMode: external - asserts: - - hasDocuments: - count: 0 - - - it: rejects missing keys when generation is disabled - set: - agent.minion.allowSelfGeneratedKey: false - asserts: - - failedTemplate: - errorMessage: set agent.minion.keySecretName or both agent.minion.privateKeyB64 and agent.minion.publicKeyB64, or enable agent.minion.allowSelfGeneratedKey - - - it: requires a key source by default - asserts: - - failedTemplate: - errorMessage: set agent.minion.keySecretName or both agent.minion.privateKeyB64 and agent.minion.publicKeyB64, or enable agent.minion.allowSelfGeneratedKey diff --git a/helm/salt-minion/tests/images_test.yaml b/helm/salt-minion/tests/images_test.yaml index b0092e7..9d32b4a 100644 --- a/helm/salt-minion/tests/images_test.yaml +++ b/helm/salt-minion/tests/images_test.yaml @@ -1,17 +1,17 @@ suite: workload images templates: - - templates/deployment.yaml + - templates/statefulset.yaml - templates/keygen-job.yaml set: agent.minion.allowSelfGeneratedKey: true tests: - it: renders the default images asserts: - - template: templates/deployment.yaml + - template: templates/statefulset.yaml equal: path: spec.template.spec.containers[?(@.name == "salt-minion")].image value: ghcr.io/saltstack/salt-kubernetes/salt-minion:0.1.0 - - template: templates/deployment.yaml + - template: templates/statefulset.yaml equal: path: spec.template.spec.initContainers[?(@.name == "prepare-minion-keys")].image value: ghcr.io/saltstack/salt-kubernetes/salt-minion:0.1.0 @@ -25,7 +25,7 @@ tests: agent.image.tag: '1.10' keygen.image.tag: '1.10' asserts: - - template: templates/deployment.yaml + - template: templates/statefulset.yaml equal: path: spec.template.spec.containers[?(@.name == "salt-minion")].image value: ghcr.io/saltstack/salt-kubernetes/salt-minion:1.10 @@ -39,7 +39,7 @@ tests: agent.image.tag: 123 keygen.image.tag: 123 asserts: - - template: templates/deployment.yaml + - template: templates/statefulset.yaml equal: path: spec.template.spec.containers[?(@.name == "salt-minion")].image value: ghcr.io/saltstack/salt-kubernetes/salt-minion:123 @@ -53,11 +53,11 @@ tests: agent.image.repository: mirror.example:5000/team/minion keygen.image.repository: other.example/team/kubectl asserts: - - template: templates/deployment.yaml + - template: templates/statefulset.yaml equal: path: spec.template.spec.containers[?(@.name == "salt-minion")].image value: mirror.example:5000/team/minion:0.1.0 - - template: templates/deployment.yaml + - template: templates/statefulset.yaml equal: path: spec.template.spec.initContainers[?(@.name == "prepare-minion-keys")].image value: mirror.example:5000/team/minion:0.1.0 @@ -73,7 +73,7 @@ tests: keygen.image.imagePullSecrets: - name: keygen-secret asserts: - - template: templates/deployment.yaml + - template: templates/statefulset.yaml equal: path: spec.template.spec.imagePullSecrets value: @@ -98,6 +98,6 @@ tests: keygen.image.imagePullSecrets: - name: keygen-secret asserts: - - template: templates/deployment.yaml + - template: templates/statefulset.yaml notExists: path: spec.template.spec.imagePullSecrets diff --git a/helm/salt-minion/tests/keygen_job_test.yaml b/helm/salt-minion/tests/keygen_job_test.yaml index b1e7a20..d4d22b4 100644 --- a/helm/salt-minion/tests/keygen_job_test.yaml +++ b/helm/salt-minion/tests/keygen_job_test.yaml @@ -57,3 +57,24 @@ tests: asserts: - hasDocuments: count: 0 + + - it: names the Job after the replica count and lists every pod when replicas > 1 + set: + agent.replicas: 3 + agent.minion.idPrefix: ext + agent.minion.allowSelfGeneratedKey: true + asserts: + - equal: + path: metadata.name + value: custom-release-keygen-3 + - matchRegex: + path: spec.template.spec.containers[0].args[0] + pattern: 'pods="ext-0 ext-1 ext-2"' + + - it: keeps the single-keypair Job name with one replica + set: + agent.minion.allowSelfGeneratedKey: true + asserts: + - equal: + path: metadata.name + value: custom-release-keygen diff --git a/helm/salt-minion/tests/minion_keygen_role_test.yaml b/helm/salt-minion/tests/minion_keygen_role_test.yaml index f242593..0025f27 100644 --- a/helm/salt-minion/tests/minion_keygen_role_test.yaml +++ b/helm/salt-minion/tests/minion_keygen_role_test.yaml @@ -27,7 +27,7 @@ tests: value: - apiGroups: [""] resources: [secrets] - verbs: [create, get, list] + verbs: [create, get, list, patch] - it: does not create the Role when a Secret name is supplied set: @@ -68,3 +68,12 @@ tests: asserts: - hasDocuments: count: 0 + + - it: lets the Role patch the Secret to add keys when scaling up + set: + agent.replicas: 2 + agent.minion.allowSelfGeneratedKey: true + asserts: + - contains: + path: rules[0].verbs + content: patch diff --git a/helm/salt-minion/tests/statefulset_test.yaml b/helm/salt-minion/tests/statefulset_test.yaml new file mode 100644 index 0000000..4f2e3e2 --- /dev/null +++ b/helm/salt-minion/tests/statefulset_test.yaml @@ -0,0 +1,159 @@ +suite: minion statefulset +templates: + - templates/statefulset.yaml +tests: + - it: prepares keys with a group-readable source + set: + agent.minion.allowSelfGeneratedKey: true + asserts: + - equal: + path: spec.template.spec.securityContext.fsGroup + value: 10001 + - equal: + path: spec.template.spec.volumes[?(@.name == "minion-keys-source")].secret.defaultMode + value: 288 # 0440: owner and group read access + - equal: + path: spec.template.spec.volumes[?(@.name == "minion-keys-source")].secret.secretName + value: RELEASE-NAME-keys + - equal: + path: spec.template.spec.initContainers[?(@.name == "prepare-minion-keys")].volumeMounts[?(@.name == "minion-pki")].mountPath + value: /pki + - equal: + path: spec.template.spec.containers[?(@.name == "salt-minion")].volumeMounts[?(@.name == "minion-pki")] + value: + name: minion-pki + mountPath: /etc/salt/pki/minion + subPath: minion + + - it: shares the configured security context with key preparation + set: + agent.minion.keySecretName: existing-keys + agent.podSecurityContext.fsGroup: 2000 + agent.containerSecurityContext.runAsUser: 2000 + agent.containerSecurityContext.runAsGroup: 2000 + asserts: + - equal: + path: spec.template.spec.securityContext.fsGroup + value: 2000 + - equal: + path: spec.template.spec.initContainers[?(@.name == "prepare-minion-keys")].securityContext + value: + runAsUser: 2000 + runAsGroup: 2000 + runAsNonRoot: true + allowPrivilegeEscalation: false + capabilities: + drop: [ALL] + - equal: + path: spec.template.spec.containers[?(@.name == "salt-minion")].securityContext + value: + runAsUser: 2000 + runAsGroup: 2000 + runAsNonRoot: true + allowPrivilegeEscalation: false + capabilities: + drop: [ALL] + + - it: uses the requested existing Secret + set: + agent.minion.keySecretName: existing-keys + asserts: + - equal: + path: spec.template.spec.volumes[?(@.name == "minion-keys-source")].secret.secretName + value: existing-keys + + - it: renders external mode without requiring keys + set: + agent.authMode: external + asserts: + - hasDocuments: + count: 0 + + - it: rejects missing keys when generation is disabled + set: + agent.minion.allowSelfGeneratedKey: false + asserts: + - failedTemplate: + errorMessage: set agent.minion.keySecretName or both agent.minion.privateKeyB64 and agent.minion.publicKeyB64, or enable agent.minion.allowSelfGeneratedKey + + - it: requires a key source by default + asserts: + - failedTemplate: + errorMessage: set agent.minion.keySecretName or both agent.minion.privateKeyB64 and agent.minion.publicKeyB64, or enable agent.minion.allowSelfGeneratedKey + + - it: renders a StatefulSet named after the ID prefix with per-pod identities + set: + agent.replicas: 3 + agent.minion.idPrefix: external-minion + agent.minion.allowSelfGeneratedKey: true + asserts: + - isKind: + of: StatefulSet + - equal: + path: metadata.name + value: external-minion + - equal: + path: spec.serviceName + value: external-minion + - equal: + path: spec.replicas + value: 3 + - notExists: + path: spec.template.spec.containers[?(@.name == "salt-minion")].env[?(@.name == "SALT_MINION_ID")] + + - it: mounts only the pod's own keys when replicas > 1 + set: + agent.replicas: 2 + agent.minion.idPrefix: ext + agent.minion.allowSelfGeneratedKey: true + asserts: + - equal: + path: spec.template.spec.initContainers[?(@.name == "prepare-minion-keys")].volumeMounts[?(@.name == "minion-keys-source")].subPathExpr + value: $(POD_NAME) + - equal: + path: spec.template.spec.volumes[?(@.name == "minion-keys-source")].secret.items + value: + - key: ext-0.pem + path: ext-0/minion.pem + - key: ext-0.pub + path: ext-0/minion.pub + - key: ext-1.pem + path: ext-1/minion.pem + - key: ext-1.pub + path: ext-1/minion.pub + + - it: keeps the single shared Secret layout with one replica + set: + agent.minion.keySecretName: existing-keys + asserts: + - notExists: + path: spec.template.spec.initContainers[?(@.name == "prepare-minion-keys")].volumeMounts[?(@.name == "minion-keys-source")].subPathExpr + - notExists: + path: spec.template.spec.volumes[?(@.name == "minion-keys-source")].secret.items + + - it: allows an explicit minion ID with one replica + set: + agent.minion.id: my-minion + agent.minion.keySecretName: existing-keys + asserts: + - equal: + path: spec.template.spec.containers[?(@.name == "salt-minion")].env[?(@.name == "SALT_MINION_ID")].value + value: my-minion + + - it: rejects an explicit minion ID with several replicas + set: + agent.replicas: 2 + agent.minion.id: my-minion + agent.minion.allowSelfGeneratedKey: true + asserts: + - failedTemplate: + errorMessage: "agent.minion.id cannot be used with agent.replicas > 1 (every pod would register as the same minion); use agent.minion.idPrefix to get -0, -1, ..." + + - it: rejects a single static keypair with several replicas + set: + agent.replicas: 2 + agent.minion.privateKeyB64: cHJpdmF0ZQ== + agent.minion.publicKeyB64: cHVibGlj + asserts: + - failedTemplate: + errorMessage: "agent.minion.privateKeyB64/publicKeyB64 hold a single keypair and cannot be used with agent.replicas > 1; set agent.minion.keySecretName to a Secret with per-pod keys, or enable agent.minion.allowSelfGeneratedKey" diff --git a/helm/salt-minion/values.yaml b/helm/salt-minion/values.yaml index e388022..ba9fe14 100644 --- a/helm/salt-minion/values.yaml +++ b/helm/salt-minion/values.yaml @@ -80,8 +80,14 @@ agent: reconRandomize: true minion: - # Salt minion ID. Leave empty to use the pod hostname. + # Salt minion ID. Leave empty to use the pod hostname. Only valid with + # agent.replicas: 1 - with more replicas every pod would register as the + # same minion, so the chart refuses; use idPrefix instead. id: "" + # Names the StatefulSet (default "salt-minion"). Pods are then named + # -0, -1, ... and each pod's minion ID is its pod + # name, e.g. idPrefix: external-minion -> external-minion-0, -1, ... + idPrefix: "" masterId: "" masterPubKeyB64: "" # Additional grains injected via the minion config ConfigMap. @@ -98,6 +104,12 @@ agent: # ../../salt-key-operator) *before* the minion starts, so it must be # handed that exact keypair rather than generating its own, which the # trust store would never have been told about. + # + # With agent.replicas > 1 every pod needs its OWN keypair. The Secret then + # holds one pair per pod, as '.pem' and '.pub' (e.g. + # external-minion-0.pem), and each pod mounts only its own pair. The + # keygen Job creates this layout (and adds missing pairs when you scale + # up); privateKeyB64/publicKeyB64 hold one pair and are rejected. keySecretName: "" # Explicit opt-out of the keySecretName requirement above when From a94003096b64837a7bbb903403ae75d39057c8b3 Mon Sep 17 00:00:00 2001 From: Saifuddin Rangwala Date: Wed, 25 Nov 2026 19:29:46 +0530 Subject: [PATCH 2/4] salt-minion: do not regenerate existing keys when scaling up The keygen Job looked up each pod's key with jsonpath={.data['.pem']}. kubectl needs the dots in a key name escaped, so the lookup silently returned nothing, every pair looked missing, and a scale-up regenerated the keys of every existing pod. That breaks the master's trust in all existing minions. Read the Secret's key map once as JSON and check each key by name. Found by scaling 3 -> 4 on a real cluster; a fake kubectl in the earlier test had hidden it. Add a regression test on the script. Co-Authored-By: Claude Sonnet 5.5 --- helm/salt-minion/templates/keygen-job.yaml | 10 ++++++++-- helm/salt-minion/tests/keygen_job_test.yaml | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/helm/salt-minion/templates/keygen-job.yaml b/helm/salt-minion/templates/keygen-job.yaml index 08e738f..ad81568 100644 --- a/helm/salt-minion/templates/keygen-job.yaml +++ b/helm/salt-minion/templates/keygen-job.yaml @@ -36,8 +36,15 @@ spec: trap 'rm -rf "$temporary_directory"' EXIT exists=false + existing_keys="" if kubectl get secret "$secret" -n "$namespace" >/dev/null 2>&1; then exists=true + # The whole key map as JSON ({".pem":"...",...}). Looking a + # key up with jsonpath does not work: kubectl needs the dots in + # the key escaped, an unescaped lookup silently returns nothing, + # and every pair would then look missing and be regenerated. + existing_keys="$(kubectl get secret "$secret" -n "$namespace" \ + -o 'jsonpath={.data}')" fi # One keypair per pod, stored as .pem / .pub. Existing @@ -45,8 +52,7 @@ spec: new="" for pod in $pods; do if [ "$exists" = true ] && \ - [ -n "$(kubectl get secret "$secret" -n "$namespace" \ - -o "jsonpath={.data['$pod.pem']}")" ]; then + printf '%s' "$existing_keys" | grep -Fq "\"$pod.pem\":"; then echo "Keys for $pod already exist; skipping" continue fi diff --git a/helm/salt-minion/tests/keygen_job_test.yaml b/helm/salt-minion/tests/keygen_job_test.yaml index d4d22b4..5cf3430 100644 --- a/helm/salt-minion/tests/keygen_job_test.yaml +++ b/helm/salt-minion/tests/keygen_job_test.yaml @@ -78,3 +78,15 @@ tests: - equal: path: metadata.name value: custom-release-keygen + + - it: checks existing keys against the whole key map, not a per-key jsonpath + set: + agent.replicas: 2 + agent.minion.allowSelfGeneratedKey: true + asserts: + - matchRegex: + path: spec.template.spec.containers[0].args[0] + pattern: "jsonpath=\\{\\.data\\}" + - notMatchRegex: + path: spec.template.spec.containers[0].args[0] + pattern: "\\.data\\['" From 326fb5dce397fa615b6e1525c6ca622790cb32bf Mon Sep 17 00:00:00 2001 From: Saifuddin Rangwala Date: Wed, 25 Nov 2026 20:03:59 +0530 Subject: [PATCH 3/4] minion images: keep the onedir Salt when installing extensions saltext.kubernetes requires salt>=3006. pip does not treat the onedir Salt as satisfying that, so it downloaded a different salt from the index (3008.4 built from the sdist) and installed it over the onedir 3008.5. That removed the /opt/saltstack/salt/salt-minion launcher and left /usr/bin/salt-minion as a dangling symlink, so the container failed with "exec: salt-minion: not found". Constrain salt to the installed version for the extension installs, so pip only adds the extensions and their other dependencies. The version is read from `salt-pip list --format=freeze`; `pip freeze` prints the onedir Salt as a file:// URL. Applies to both the kubernetes and vcf images. Verified for both: salt stays at 3008.5, salt-minion runs, the extensions import, salt-call works. Co-Authored-By: Claude Sonnet 5.5 --- src/minion/kubernetes/Dockerfile | 13 +++++++++++-- src/minion/vcf/Dockerfile | 14 ++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/minion/kubernetes/Dockerfile b/src/minion/kubernetes/Dockerfile index a8485f5..530378e 100644 --- a/src/minion/kubernetes/Dockerfile +++ b/src/minion/kubernetes/Dockerfile @@ -105,7 +105,16 @@ RUN --mount=type=secret,id=build_ca,target=/tmp/build-ca.crt,required=false \ if [ -n "${PIP_ONLY_BINARY}" ]; then \ set -- "$@" --only-binary "${PIP_ONLY_BINARY}"; \ fi; \ - echo "Installing extensions from salt-extensions.txt using salt-pip..."; \ + # Extensions depend on "salt", and pip does not treat the onedir Salt as + # satisfying it: it would download a different Salt from the index and + # install it over the onedir one, removing the salt-minion launcher. + # Constrain salt to the installed version so pip only adds the + # extensions and their other dependencies. + salt_installed="$(salt-pip list --format=freeze | grep -i '^salt==')"; \ + test -n "${salt_installed}"; \ + echo "${salt_installed}" > /tmp/salt-constraint.txt; \ + set -- "$@" --constraint /tmp/salt-constraint.txt; \ + echo "Installing extensions from salt-extensions.txt using salt-pip (${salt_installed})..."; \ salt-pip install --no-cache-dir "$@" \ -r /tmp/salt-extensions.txt; \ if [ -n "${EXTRA_SALT_EXTENSIONS}" ]; then \ @@ -113,7 +122,7 @@ RUN --mount=type=secret,id=build_ca,target=/tmp/build-ca.crt,required=false \ salt-pip install --no-cache-dir "$@" \ ${EXTRA_SALT_EXTENSIONS}; \ fi; \ - rm -f /tmp/salt-extensions.txt; \ + rm -f /tmp/salt-extensions.txt /tmp/salt-constraint.txt; \ if [ -s /tmp/build-ca.crt ]; then \ rm -f /usr/local/share/ca-certificates/build-ca.crt; \ update-ca-certificates >/dev/null; \ diff --git a/src/minion/vcf/Dockerfile b/src/minion/vcf/Dockerfile index 5bfc554..1b0544e 100644 --- a/src/minion/vcf/Dockerfile +++ b/src/minion/vcf/Dockerfile @@ -88,7 +88,17 @@ RUN set -eu; \ if [ -n "${PIP_TRUSTED_HOST}" ]; then set -- "$@" --trusted-host "${PIP_TRUSTED_HOST}"; fi; \ if [ -n "${PIP_ONLY_BINARY}" ]; then set -- "$@" --only-binary "${PIP_ONLY_BINARY}"; fi; \ \ - echo "Installing extensions from salt-extensions.txt using salt-pip..."; \ + # Extensions depend on "salt", and pip does not treat the onedir Salt as + # satisfying it: it would download a different Salt from the index and + # install it over the onedir one, removing the salt-minion launcher. + # Constrain salt to the installed version so pip only adds the + # extensions and their other dependencies. + salt_installed="$(salt-pip list --format=freeze | grep -i '^salt==')"; \ + test -n "${salt_installed}"; \ + echo "${salt_installed}" > /tmp/salt-constraint.txt; \ + set -- "$@" --constraint /tmp/salt-constraint.txt; \ + \ + echo "Installing extensions from salt-extensions.txt using salt-pip (${salt_installed})..."; \ salt-pip install --no-cache-dir "$@" -r /tmp/salt-extensions.txt; \ \ if [ -n "${EXTRA_SALT_EXTENSIONS}" ]; then \ @@ -96,7 +106,7 @@ RUN set -eu; \ # hadolint ignore=SC2086 salt-pip install --no-cache-dir "$@" ${EXTRA_SALT_EXTENSIONS}; \ fi; \ - rm -f /tmp/salt-extensions.txt + rm -f /tmp/salt-extensions.txt /tmp/salt-constraint.txt # Bundled so this image needs no separate init container to reach the # Kubernetes API - see salt-minion-kubernetes/values.yaml (agent.kubectl.bundled). From 03b3fe386d1d32266ed9ad8581183ef227a9119a Mon Sep 17 00:00:00 2001 From: Saifuddin Rangwala Date: Wed, 25 Nov 2026 20:22:35 +0530 Subject: [PATCH 4/4] minion images: avoid a pipe when reading the installed Salt version Hadolint DL4006 flags a pipe in a RUN without pipefail. Write `salt-pip list --format=freeze` to a file and grep the file instead, which also makes a salt-pip failure stop the build rather than being masked by the pipe. Both images rebuilt and re-verified (salt stays 3008.5, salt-minion runs, extensions import). Co-Authored-By: Claude Sonnet 5.5 --- src/minion/kubernetes/Dockerfile | 5 +++-- src/minion/vcf/Dockerfile | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/minion/kubernetes/Dockerfile b/src/minion/kubernetes/Dockerfile index 530378e..b953d91 100644 --- a/src/minion/kubernetes/Dockerfile +++ b/src/minion/kubernetes/Dockerfile @@ -110,7 +110,8 @@ RUN --mount=type=secret,id=build_ca,target=/tmp/build-ca.crt,required=false \ # install it over the onedir one, removing the salt-minion launcher. # Constrain salt to the installed version so pip only adds the # extensions and their other dependencies. - salt_installed="$(salt-pip list --format=freeze | grep -i '^salt==')"; \ + salt-pip list --format=freeze > /tmp/salt-packages.txt; \ + salt_installed="$(grep -i '^salt==' /tmp/salt-packages.txt)"; \ test -n "${salt_installed}"; \ echo "${salt_installed}" > /tmp/salt-constraint.txt; \ set -- "$@" --constraint /tmp/salt-constraint.txt; \ @@ -122,7 +123,7 @@ RUN --mount=type=secret,id=build_ca,target=/tmp/build-ca.crt,required=false \ salt-pip install --no-cache-dir "$@" \ ${EXTRA_SALT_EXTENSIONS}; \ fi; \ - rm -f /tmp/salt-extensions.txt /tmp/salt-constraint.txt; \ + rm -f /tmp/salt-extensions.txt /tmp/salt-constraint.txt /tmp/salt-packages.txt; \ if [ -s /tmp/build-ca.crt ]; then \ rm -f /usr/local/share/ca-certificates/build-ca.crt; \ update-ca-certificates >/dev/null; \ diff --git a/src/minion/vcf/Dockerfile b/src/minion/vcf/Dockerfile index 1b0544e..70fc695 100644 --- a/src/minion/vcf/Dockerfile +++ b/src/minion/vcf/Dockerfile @@ -93,7 +93,8 @@ RUN set -eu; \ # install it over the onedir one, removing the salt-minion launcher. # Constrain salt to the installed version so pip only adds the # extensions and their other dependencies. - salt_installed="$(salt-pip list --format=freeze | grep -i '^salt==')"; \ + salt-pip list --format=freeze > /tmp/salt-packages.txt; \ + salt_installed="$(grep -i '^salt==' /tmp/salt-packages.txt)"; \ test -n "${salt_installed}"; \ echo "${salt_installed}" > /tmp/salt-constraint.txt; \ set -- "$@" --constraint /tmp/salt-constraint.txt; \ @@ -106,7 +107,7 @@ RUN set -eu; \ # hadolint ignore=SC2086 salt-pip install --no-cache-dir "$@" ${EXTRA_SALT_EXTENSIONS}; \ fi; \ - rm -f /tmp/salt-extensions.txt /tmp/salt-constraint.txt + rm -f /tmp/salt-extensions.txt /tmp/salt-constraint.txt /tmp/salt-packages.txt # Bundled so this image needs no separate init container to reach the # Kubernetes API - see salt-minion-kubernetes/values.yaml (agent.kubectl.bundled).