imagePullSecrets in every namespace with secretGenerator
Published: 2026-02-15
A private container registry needs an imagePullSecret for every Pod in every namespace. With 15 namespaces and a new one every month, copying registry credentials by hand quickly becomes a chore. I use Kustomize secretGenerator with FluxCD for this. It needs no extra operator.
The problem
A Pod needs to pull registry.example.com/myapp:1.0. The registry requires auth. The imagePullSecret must be in the same namespace as the Pod. You can't use a secret from another namespace.
Options:
- Copy the secret to every namespace by hand. Hard to maintain: when the token rotates, you update 15 places.
- Use a Secret replication operator (for example, Reflector or Kubernetes Replicator). It is one more component to run, and if the operator has a bug, it fails silently.
- Use
secretGenerator. It is built into Kustomize, needs no extra dependencies and fits GitOps well.
The secretGenerator approach
In a FluxCD GitOps repo, create a Kustomization that generates the Secret in every namespace:
yaml# fluxcd/custom/secrets/docker-registries/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- name: registry-example-com
namespace: app
type: kubernetes.io/dockerconfigjson
files:
- .dockerconfigjson=registry-credentials.dockerconfigjson
options:
disableNameSuffixHash: true
- name: registry-example-com
namespace: monitoring
type: kubernetes.io/dockerconfigjson
files:
- .dockerconfigjson=registry-credentials.dockerconfigjson
options:
disableNameSuffixHash: true
- name: registry-example-com
namespace: db
type: kubernetes.io/dockerconfigjson
files:
- .dockerconfigjson=registry-credentials.dockerconfigjson
options:
disableNameSuffixHash: true
disableNameSuffixHash: true keeps the secret name stable: registry-example-com instead of registry-example-com-abc123. This is important because HelmRelease values use the secret name. Without this flag, Kustomize adds a content hash to the name. After every credential rotation the name changes, and all references break.
Why secretGenerator and not copied manifests
secretGenerator is idempotent. If the content changes, kubectl apply on the same Kustomization updates the secret in each namespace. For a new namespace, you add one block and commit. Flux does the rest. There is no "apply for each namespace" loop to maintain.
The .dockerconfigjson file
Generate it once from your credentials:
bashkubectl create secret docker-registry registry-example-com \
--docker-server=registry.example.com \
--docker-username=robot-ci \
--docker-password=TOKEN \
--dry-run=client -o jsonpath='{.data.\.dockerconfigjson}' \
| base64 -d > registry-credentials.dockerconfigjson
The file should look like this:
json{
"auths": {
"registry.example.com": {
"username": "robot-ci",
"password": "TOKEN",
"auth": "BASE64_USER_PASS"
}
}
}
Do not commit this file in plain text. Options:
- Encrypt it with SOPS or SealedSecrets. With SealedSecrets you need one SealedSecret per namespace, so there is more boilerplate.
- Store it in Vault and use ESO. ESO creates the K8s Secret from Vault and updates it on rotation.
- Add the file to
.gitignoreand inject it in CI when you generate manifests.
In production we use ESO: a single ExternalSecret syncs the dockerconfigjson from Vault into each namespace, with creationPolicy: Merge on the existing secret. For dev/test, the file is in .gitignore and CI injects it during bootstrap.
Rotating registry credentials
When the robot token rotates:
- If you use ESO, update the token in Vault. ESO refreshes the Secret on the next sync cycle.
- If you use the file: generate a new
registry-credentials.dockerconfigjson, commit and push. Flux reconciles and updates the Secret in all namespaces at the same time.
No manual kubectl apply loop over namespaces.
Adding a new namespace
For a new namespace, add one block to kustomization.yaml:
yaml - name: registry-example-com
namespace: newservice
type: kubernetes.io/dockerconfigjson
files:
- .dockerconfigjson=registry-credentials.dockerconfigjson
options:
disableNameSuffixHash: true
Commit, push, Flux reconciles, done. The new namespace gets the secret in less than a minute.
Generating the namespace list
If you have many namespaces and don't want to keep the list by hand, a small script generates the Kustomization from the namespaces in the live cluster:
bashkubectl get namespaces -o jsonpath='{.items[*].metadata.name}' \
| tr ' ' '\n' \
| grep -v 'kube-\|flux-\|kube$' \
| while read ns; do
cat <<EOF
- name: registry-example-com
namespace: $ns
type: kubernetes.io/dockerconfigjson
files:
- .dockerconfigjson=registry-credentials.dockerconfigjson
options:
disableNameSuffixHash: true
EOF
done
Run it when you add several namespaces at once, then review the output and commit it.
Using the secret in HelmReleases
Add it to the HelmRelease values:
yamlvalues:
imagePullSecrets:
- name: registry-example-com
image:
repository: registry.example.com/myapp
Or set it for all Pods in the chart through the Pod spec:
yaml# In your Helm chart's deployment.yaml
spec:
imagePullSecrets:
{{- toYaml .Values.imagePullSecrets | nindent 4 }}
Patching the default ServiceAccount
You can also patch the default ServiceAccount in each namespace and add an imagePullSecrets entry. Then every Pod in the namespace gets the pull secret automatically, even if the Helm chart doesn't set it:
yamlapiVersion: v1
kind: ServiceAccount
metadata:
name: default
namespace: app
imagePullSecrets:
- name: registry-example-com
Apply it as a Kustomize resource next to the secretGenerator. This helps with third-party charts that don't have imagePullSecrets in their values.
The Kubernetes admission controller copies imagePullSecrets from the ServiceAccount into each new Pod in the namespace. You don't need to change the Pod spec or the Helm chart.
Default SA patches for all namespaces
To add the ServiceAccount patch to all namespaces:
yamlapiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- sa-app.yaml
- sa-monitoring.yaml
- sa-db.yaml
Each sa-*.yaml file is a ServiceAccount with the imagePullSecrets entry. It is less DRY than secretGenerator, but it is explicit and easy to read.
Debugging image pull errors
When a Pod can't pull an image:
bashkubectl describe pod <pod> -n <namespace> | grep -A5 "Events:"
Common events:
ImagePullBackOff: the pull failed, Kubernetes retries with exponential backoff.ErrImagePull: the first pull failed.403 Forbidden/401 Unauthorized: credentials are wrong or missing.
Check that the secret exists and has the right content:
bashkubectl get secret registry-example-com -n <namespace> -o jsonpath='{.data.\.dockerconfigjson}' | base64 -d | jq .
If the secret is missing, Flux may not have reconciled yet. Check:
bashflux get kustomization secrets -n flux-system