Skip to content

Commit 6992451

Browse files
authored
Support image pull secrets for Fleet controllers and agents (#5201)
* Support image pull secrets for Fleet controllers and jobs Fleet controller images can now be sourced from private registries requiring authentication. This can be used as follows: 1. Create a secret pointing to a private registry containing a Fleet controller image: ``` kubectl create secret docker-registry registry-creds \ -n cattle-fleet-system \ --docker-username=fleet-ci \ --docker-password=foo \ --docker-server=[my-server]:[my-port] ``` 2. Install Fleet with: ``` --set image.repository=[my-server]:[my-port]/fleet --set-json global.cattle.imagePullSecrets='[{"name": "registry-creds"}]' ``` All Fleet controllers (`fleet-controller`, `gitjob`, `helmops` and created jobs (migrations, cleanups) will now source the controller image from that registry. * Propagate image pull secrets to agents Agent deployments now support image pull secrets, which are copied into the agent's namespace before the deployment is created. The existing feature for automated copy of resources to downstream clusters is not usable in this case, since it requires an already running agent to copy the resource, in this case an image pull secret, to the downstream cluster. * Propagate controller image pull secrets to git job This ensures that git jobs are able to run from images fetched from private registries if needed. Image pull secrets are copied into the git job's namespace, and owned by the GitRepo.
1 parent fd0ead6 commit 6992451

14 files changed

Lines changed: 109 additions & 0 deletions

File tree

charts/fleet/templates/configmap.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ data:
1919
{{ if .Values.garbageCollectionInterval }}
2020
"garbageCollectionInterval": "{{.Values.garbageCollectionInterval}}",
2121
{{ end }}
22+
{{ if .Values.global.cattle.imagePullSecrets }}
23+
"imagePullSecrets": {{toJson .Values.global.cattle.imagePullSecrets}},
24+
{{ end }}
2225
"ignoreClusterRegistrationLabels": {{.Values.ignoreClusterRegistrationLabels}},
2326
"bootstrap": {
2427
"paths": "{{.Values.bootstrap.paths}}",

charts/fleet/templates/deployment.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ spec:
3535
{{ toYaml $.Values.extraAnnotations.fleetController | indent 8 }}
3636
{{- end }}
3737
spec:
38+
{{- if $.Values.global.cattle.imagePullSecrets }}
39+
imagePullSecrets:
40+
{{ toYaml $.Values.global.cattle.imagePullSecrets | indent 8 }}
41+
{{- end }}
3842
containers:
3943
- env:
4044
- name: NAMESPACE

charts/fleet/templates/deployment_gitjob.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ spec:
3737
{{- end }}
3838
spec:
3939
serviceAccountName: gitjob
40+
{{- if $.Values.global.cattle.imagePullSecrets }}
41+
imagePullSecrets:
42+
{{ toYaml $.Values.global.cattle.imagePullSecrets | indent 8 }}
43+
{{- end }}
4044
containers:
4145
- image: "{{ template "system_default_registry" $ }}{{ $.Values.image.repository }}:{{ $.Values.image.tag }}"
4246
name: gitjob

charts/fleet/templates/deployment_helmops.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ spec:
3737
{{- end }}
3838
spec:
3939
serviceAccountName: helmops
40+
{{- if $.Values.global.cattle.imagePullSecrets }}
41+
imagePullSecrets:
42+
{{ toYaml $.Values.global.cattle.imagePullSecrets | indent 8 }}
43+
{{- end }}
4044
containers:
4145
- image: "{{ template "system_default_registry" $ }}{{ $.Values.image.repository }}:{{ $.Values.image.tag }}"
4246
name: helmops

charts/fleet/templates/job_cleanup_clusterregistrations.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ spec:
1313
labels:
1414
app: fleet-job
1515
spec:
16+
{{- if .Values.global.cattle.imagePullSecrets }}
17+
imagePullSecrets:
18+
{{ toYaml .Values.global.cattle.imagePullSecrets | indent 8 }}
19+
{{- end }}
1620
serviceAccountName: fleet-controller
1721
restartPolicy: Never
1822
securityContext:

charts/fleet/templates/job_cleanup_gitrepojobs.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ spec:
1616
labels:
1717
app: fleet-job
1818
spec:
19+
{{- if .Values.global.cattle.imagePullSecrets }}
20+
imagePullSecrets:
21+
{{ toYaml .Values.global.cattle.imagePullSecrets | indent 12 }}
22+
{{- end }}
1923
serviceAccountName: gitjob
2024
restartPolicy: Never
2125
securityContext:

charts/fleet/templates/job_migrate_gitrepo_helmrepourl.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ spec:
1313
labels:
1414
app: fleet-job
1515
spec:
16+
{{- if .Values.global.cattle.imagePullSecrets }}
17+
imagePullSecrets:
18+
{{ toYaml .Values.global.cattle.imagePullSecrets | indent 8 }}
19+
{{- end }}
1620
serviceAccountName: fleet-controller
1721
restartPolicy: Never
1822
securityContext:

charts/fleet/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ bootstrap:
7272

7373
global:
7474
cattle:
75+
imagePullSecrets: []
7576
systemDefaultRegistry: ""
7677

7778
## Node labels for pod assignment

internal/cmd/controller/agentmanagement/agent/agent.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ func AgentWithConfig(ctx context.Context, agentNamespace, controllerNamespace, a
5757

5858
objs = append(objs, secret)
5959

60+
// Copy image pull secrets, if any, to the agent namespace.
61+
// This ensures that those secrets are available at agent deployment time.
62+
for _, ips := range opts.ImagePullSecrets {
63+
source, err := client.Core.Secret().Get(controllerNamespace, ips.Name, metav1.GetOptions{})
64+
if err != nil {
65+
return objs, fmt.Errorf("failed to get image pull secret %q from controller namespace: %w", ips.Name, err)
66+
}
67+
68+
dest := &v1.Secret{
69+
ObjectMeta: metav1.ObjectMeta{
70+
Name: ips.Name,
71+
Namespace: agentNamespace,
72+
},
73+
Type: source.Type,
74+
Data: source.Data,
75+
}
76+
77+
objs = append(objs, dest)
78+
}
79+
6080
agentConfig, err := agentConfig(ctx, agentNamespace, controllerNamespace, cg, &opts.ConfigOptions)
6181
if err != nil {
6282
return objs, err
@@ -73,6 +93,7 @@ func AgentWithConfig(ctx context.Context, agentNamespace, controllerNamespace, a
7393
// keep in sync with manageagent.go
7494
mo := opts.ManifestOptions
7595
mo.AgentImage = cfg.AgentImage
96+
mo.ImagePullSecrets = cfg.ImagePullSecrets
7697
mo.AgentImagePullPolicy = cfg.AgentImagePullPolicy
7798
mo.CheckinInterval = cfg.AgentCheckinInterval.Duration.String()
7899
mo.SystemDefaultRegistry = cfg.SystemDefaultRegistry

internal/cmd/controller/agentmanagement/agent/manifest.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type ManifestOptions struct {
4646
DriftWorkers string
4747
cmd.LeaderElectionOptions
4848
PriorityClassName string
49+
ImagePullSecrets []corev1.LocalObjectReference
4950
}
5051

5152
// Manifest builds and returns a deployment manifest for the fleet-agent with a
@@ -167,6 +168,7 @@ func agentApp(namespace string, agentScope string, opts ManifestOptions) *appsv1
167168
},
168169
},
169170
Spec: corev1.PodSpec{
171+
ImagePullSecrets: opts.ImagePullSecrets,
170172
PriorityClassName: opts.PriorityClassName,
171173
ServiceAccountName: serviceAccount,
172174
Containers: []corev1.Container{

0 commit comments

Comments
 (0)