Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions helm/salt-minion/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (`<pod name>` 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 `<pod>.pem`/`<pod>.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 `<release>-keygen-<replicas>` 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
Expand Down
37 changes: 35 additions & 2 deletions helm/salt-minion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<idPrefix>-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` |
Expand Down Expand Up @@ -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
(`<pod>.pem` / `<pod>.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:
Expand Down
42 changes: 42 additions & 0 deletions helm/salt-minion/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>-<ordinal> 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 <prefix>-0, <prefix>-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 -}}
70 changes: 69 additions & 1 deletion helm/salt-minion/templates/keygen-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ({"<pod>.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 <pod>.pem / <pod>.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"
Expand All @@ -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 }}
2 changes: 1 addition & 1 deletion helm/salt-minion/templates/minion-keygen-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ metadata:
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["create", "get", "list"]
verbs: ["create", "get", "list", "patch"]
{{- end }}
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
{{- 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
app.kubernetes.io/name: salt-minion
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
Expand Down Expand Up @@ -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
Expand All @@ -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 <pod-name>/ directory of the
# shared Secret, so it never sees the other replicas' keys.
subPathExpr: $(POD_NAME)
{{- end }}
readOnly: true
- name: minion-pki
mountPath: /pki
Expand All @@ -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 }}
Expand Down Expand Up @@ -173,6 +194,17 @@ spec:
secret:
secretName: {{ $keySecret }}
defaultMode: 0440
{{- if include "salt-minion.perPodKeys" . }}
# Secret keys are <pod-name>.pem / <pod-name>.pub; map each pair
# to <pod-name>/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: {}
Expand Down
82 changes: 0 additions & 82 deletions helm/salt-minion/tests/deployment_test.yaml

This file was deleted.

Loading
Loading