Skip to content

salt-minion: StatefulSet with a unique ID and keypair per replica - #36

Merged
saifuddinrangwala merged 4 commits into
mainfrom
feat/minion-statefulset
Nov 25, 2026
Merged

saifuddinrangwala merged 4 commits into
mainfrom
feat/minion-statefulset

Conversation

@saifuddinrangwala

@saifuddinrangwala saifuddinrangwala commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

With agent.replicas > 1 the chart rendered a Deployment whose pods all got the same agent.minion.id and mounted the same key Secret. So every replica registered as one and the same minion with one keypair, and the master saw a single identity that several pods kept displacing. Without an explicit id the pod ids differed, but every pod still presented the same key.

Change

  • Deployment becomes a StatefulSet (templates/statefulset.yaml). New agent.minion.idPrefix names the StatefulSet, so pod N is minion <idPrefix>-N (e.g. external-minion-0, -1, ...). No image change is needed: the entrypoints already fall back to the pod name, and the chart now also sets POD_NAME.

  • With agent.replicas > 1 the chart rejects agent.minion.id and a single static privateKeyB64/publicKeyB64 pair, because both describe one identity.

  • With replicas > 1, keySecretName points at a Secret holding one pair per pod, as <pod>.pem and <pod>.pub. Each pod mounts only its own pair (items plus subPathExpr: $(POD_NAME)) and never sees the others'.

  • The keygen Job generates one keypair per replica. It is idempotent: it never regenerates existing pairs, and on scale-up it adds only the missing ones (via kubectl patch, so the Role gains patch). With replicas > 1 it is named <release>-keygen-<replicas> so each scale step gets a fresh Job, since Job specs are immutable.

  • replicas: 1 is unchanged: same Secret layout, and agent.minion.id still works.

  • Minion images (both kubernetes and vcf): keep the onedir Salt when installing extensions. saltext.kubernetes requires salt>=3006, and pip does not treat the onedir Salt as satisfying it. It downloaded a different salt from the index (3008.4, built from the sdist) and installed it over the onedir 3008.5, which removed the /opt/saltstack/salt/salt-minion launcher and left /usr/bin/salt-minion dangling, so containers failed with exec: salt-minion: not found. The extension step now constrains salt to the installed version (read with salt-pip list --format=freeze; pip freeze prints the onedir Salt as a file:// URL), so pip only adds the extensions and their other dependencies. It is included here because the chart cannot be run end to end without a working minion image.

Testing

  • Rebased onto main after Fix minion PKI preparation and security defaults #34 merged, and re-tested on the merged templates. helm lint passes for 1 and 3 replicas, and helm template renders what I expect (StatefulSet name, per-pod items, subPathExpr, Job name and pod list).

  • helm unittest: 44 of 44 pass, including the existing suites and new ones for the identity, per-pod mounts, guards and keygen naming.

  • Installed on a real cluster (3 replicas, idPrefix: ext-minion, allowSelfGeneratedKey):

    • the keygen Job created one Secret holding 3 pairs (ext-minion-N.pem and .pub);
    • the pods were named ext-minion-0 to -2, spread across different nodes, with the minion ID equal to the pod name;
    • the key-preparation init container completed in every pod, so the items plus subPathExpr mount works.
  • Scale-up test found a bug, now fixed (a940030). Scaling from 3 to 4 regenerated the keys of every pod, not just the new one, which would break trust for all existing minions. The keygen script looked keys up with jsonpath={.data['<pod>.pem']}, and kubectl needs the dots in a key name escaped, so the lookup silently returned nothing. It now reads the Secret's key map once as JSON. After the fix, scaling from 4 to 5 added only ext-minion-4 and left the 8 existing entries byte-identical. A regression test covers the script.

  • Images: both fixed images were built (amd64, Salt 3008.5). In each, the installed Salt stays 3008.5, salt-minion --version works, saltext.kubernetes and saltext.vault import, and salt-call --local test.ping returns True.

  • With the fixed image, all 5 minion pods run. Each pod's minion.pub and minion.pem match its own entries in the Secret, the 5 public keys are all different, and a pod's PKI directory contains only its own pair. A master shows ext-minion-0 to ext-minion-4 as 5 separate unaccepted keys.

Not tested:

  • Accepting the keys and dispatching jobs to the minions.
  • Scaling down.
  • Building the images against public PyPI; I used an internal mirror.

Notes

  • Upgrade: reinstall rather than helm upgrade, since a Deployment cannot be converted to a StatefulSet in place.
  • Scale down: it leaves the extra pairs in the Secret. Remove them, and the minion keys on the master, if you don't want them.
  • Pod access: any pod could still read the other pairs through the API if its ServiceAccount can read Secrets. The keygen Role grants get/list on Secrets. Only the container filesystem is isolated.
  • CI: nothing in CI runs helm unittest yet, so these tests don't run automatically (same gap as noted on Fix minion PKI preparation and security defaults #34).

🤖 Generated with Claude Code

…plica

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 <idPrefix>-N.
- replicas > 1 rejects agent.minion.id and the single static
  privateKeyB64/publicKeyB64 pair.
- keySecretName holds <pod>.pem/<pod>.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 <noreply@anthropic.com>
Saifuddin Rangwala and others added 3 commits November 25, 2026 19:29
The keygen Job looked up each pod's key with
jsonpath={.data['<pod>.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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
@saifuddinrangwala
saifuddinrangwala marked this pull request as ready for review November 25, 2026 15:19
@saifuddinrangwala
saifuddinrangwala merged commit 552b50d into main Nov 25, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant