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..ad81568 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,71 @@ 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 + 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 + # pairs are never regenerated: the master already trusts them. + new="" + for pod in $pods; do + if [ "$exists" = true ] && \ + printf '%s' "$existing_keys" | grep -Fq "\"$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 +117,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..5cf3430 100644 --- a/helm/salt-minion/tests/keygen_job_test.yaml +++ b/helm/salt-minion/tests/keygen_job_test.yaml @@ -57,3 +57,36 @@ 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 + + - 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\\['" 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 diff --git a/src/minion/kubernetes/Dockerfile b/src/minion/kubernetes/Dockerfile index a8485f5..b953d91 100644 --- a/src/minion/kubernetes/Dockerfile +++ b/src/minion/kubernetes/Dockerfile @@ -105,7 +105,17 @@ 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-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; \ + 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 +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; \ + 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 5bfc554..70fc695 100644 --- a/src/minion/vcf/Dockerfile +++ b/src/minion/vcf/Dockerfile @@ -88,7 +88,18 @@ 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-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; \ + \ + 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 +107,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 /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).