Skip to content

Commit 2065254

Browse files
committed
Add imagePullSecrets support to JobConfig for backup and restore jobs
Users with private container registries can now specify imagePullSecrets in JobConfig, which is propagated to both backup CronJob and restore Job PodSpecs. This enables pulling backup/restore images from authenticated registries without manual intervention. Signed-off-by: shunki-fujita <shunki-fujita@cybozu.co.jp>
1 parent 154998b commit 2065254

9 files changed

Lines changed: 116 additions & 1 deletion

api/v1beta2/job_types.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ type JobConfig struct {
8686
//
8787
// +optional
8888
VolumeMounts []VolumeMountApplyConfiguration `json:"volumeMounts,omitempty"`
89+
90+
// ImagePullSecrets is a list of references to secrets in the same namespace to use for pulling any images.
91+
//
92+
// +optional
93+
ImagePullSecrets []LocalObjectReferenceApplyConfiguration `json:"imagePullSecrets,omitempty"`
8994
}
9095

9196
// VolumeSourceApplyConfiguration is the type defined to implement the DeepCopy method.
@@ -210,7 +215,7 @@ type BucketConfig struct {
210215
// AffinityApplyConfiguration is the type defined to implement the DeepCopy method.
211216
type AffinityApplyConfiguration corev1ac.AffinityApplyConfiguration
212217

213-
// DeepCopy is copying the receiver, creating a new EnvVarApplyConfiguration.
218+
// DeepCopy is copying the receiver, creating a new AffinityApplyConfiguration.
214219
func (in *AffinityApplyConfiguration) DeepCopy() *AffinityApplyConfiguration {
215220
out := new(AffinityApplyConfiguration)
216221
bytes, err := json.Marshal(in)
@@ -223,3 +228,20 @@ func (in *AffinityApplyConfiguration) DeepCopy() *AffinityApplyConfiguration {
223228
}
224229
return out
225230
}
231+
232+
// LocalObjectReferenceApplyConfiguration is the type defined to implement the DeepCopy method.
233+
type LocalObjectReferenceApplyConfiguration corev1ac.LocalObjectReferenceApplyConfiguration
234+
235+
// DeepCopy is copying the receiver, creating a new LocalObjectReferenceApplyConfiguration.
236+
func (in *LocalObjectReferenceApplyConfiguration) DeepCopy() *LocalObjectReferenceApplyConfiguration {
237+
out := new(LocalObjectReferenceApplyConfiguration)
238+
bytes, err := json.Marshal(in)
239+
if err != nil {
240+
panic("Failed to marshal")
241+
}
242+
err = json.Unmarshal(bytes, out)
243+
if err != nil {
244+
panic("Failed to unmarshal")
245+
}
246+
return out
247+
}

api/v1beta2/zz_generated.deepcopy.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

charts/moco/templates/generated/crds/moco_crds.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,15 @@ spec:
574574
type: object
575575
type: object
576576
type: array
577+
imagePullSecrets:
578+
description: ImagePullSecrets is a list of references to...
579+
items:
580+
description: LocalObjectReferenceApplyConfiguration is the...
581+
properties:
582+
name:
583+
type: string
584+
type: object
585+
type: array
577586
maxCpu:
578587
anyOf:
579588
- type: integer
@@ -6708,6 +6717,15 @@ spec:
67086717
type: object
67096718
type: object
67106719
type: array
6720+
imagePullSecrets:
6721+
description: ImagePullSecrets is a list of references to...
6722+
items:
6723+
description: LocalObjectReferenceApplyConfiguration is the...
6724+
properties:
6725+
name:
6726+
type: string
6727+
type: object
6728+
type: array
67116729
maxCpu:
67126730
anyOf:
67136731
- type: integer

config/crd/bases/moco.cybozu.com_backuppolicies.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,15 @@ spec:
601601
type: object
602602
type: object
603603
type: array
604+
imagePullSecrets:
605+
description: ImagePullSecrets is a list of references to...
606+
items:
607+
description: LocalObjectReferenceApplyConfiguration is the...
608+
properties:
609+
name:
610+
type: string
611+
type: object
612+
type: array
604613
maxCpu:
605614
anyOf:
606615
- type: integer

config/crd/bases/moco.cybozu.com_mysqlclusters.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4822,6 +4822,15 @@ spec:
48224822
type: object
48234823
type: object
48244824
type: array
4825+
imagePullSecrets:
4826+
description: ImagePullSecrets is a list of references to...
4827+
items:
4828+
description: LocalObjectReferenceApplyConfiguration is the...
4829+
properties:
4830+
name:
4831+
type: string
4832+
type: object
4833+
type: array
48254834
maxCpu:
48264835
anyOf:
48274836
- type: integer

config/crd/tests/apiextensions.k8s.io_v1_customresourcedefinition_backuppolicies.moco.cybozu.com.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,15 @@ spec:
600600
type: object
601601
type: object
602602
type: array
603+
imagePullSecrets:
604+
description: ImagePullSecrets is a list of references to...
605+
items:
606+
description: LocalObjectReferenceApplyConfiguration is the...
607+
properties:
608+
name:
609+
type: string
610+
type: object
611+
type: array
603612
maxCpu:
604613
anyOf:
605614
- type: integer

config/crd/tests/apiextensions.k8s.io_v1_customresourcedefinition_mysqlclusters.moco.cybozu.com.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4822,6 +4822,15 @@ spec:
48224822
type: object
48234823
type: object
48244824
type: array
4825+
imagePullSecrets:
4826+
description: ImagePullSecrets is a list of references to...
4827+
items:
4828+
description: LocalObjectReferenceApplyConfiguration is the...
4829+
properties:
4830+
name:
4831+
type: string
4832+
type: object
4833+
type: array
48254834
maxCpu:
48264835
anyOf:
48274836
- type: integer

controllers/mysqlcluster_controller.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,14 @@ func (r *MySQLClusterReconciler) reconcileV1BackupJob(ctx context.Context, req c
12791279
}
12801280
return volumes
12811281
}()...).
1282+
WithImagePullSecrets(func() []*corev1ac.LocalObjectReferenceApplyConfiguration {
1283+
imagePullSecrets := make([]*corev1ac.LocalObjectReferenceApplyConfiguration, 0, len(jc.ImagePullSecrets))
1284+
for _, s := range jc.ImagePullSecrets {
1285+
s := s
1286+
imagePullSecrets = append(imagePullSecrets, (*corev1ac.LocalObjectReferenceApplyConfiguration)(&s))
1287+
}
1288+
return imagePullSecrets
1289+
}()...).
12821290
WithContainers(container).
12831291
WithSecurityContext(corev1ac.PodSecurityContext().
12841292
WithFSGroup(constants.ContainerGID).
@@ -1585,6 +1593,14 @@ func (r *MySQLClusterReconciler) reconcileV1RestoreJob(ctx context.Context, req
15851593
}
15861594
return volumes
15871595
}()...).
1596+
WithImagePullSecrets(func() []*corev1ac.LocalObjectReferenceApplyConfiguration {
1597+
imagePullSecrets := make([]*corev1ac.LocalObjectReferenceApplyConfiguration, 0, len(jc.ImagePullSecrets))
1598+
for _, s := range jc.ImagePullSecrets {
1599+
s := s
1600+
imagePullSecrets = append(imagePullSecrets, (*corev1ac.LocalObjectReferenceApplyConfiguration)(&s))
1601+
}
1602+
return imagePullSecrets
1603+
}()...).
15881604
WithContainers(container).
15891605
WithSecurityContext(corev1ac.PodSecurityContext().
15901606
WithFSGroup(constants.ContainerGID).

controllers/mysqlcluster_controller_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ func testNewBackUpPolicy() *mocov1beta2.BackupPolicy {
110110
MountPath: ptr.To[string]("/path/to/dir"),
111111
},
112112
}
113+
jc.ImagePullSecrets = []mocov1beta2.LocalObjectReferenceApplyConfiguration{
114+
{Name: ptr.To[string]("my-registry-secret")},
115+
}
113116
jc.BucketConfig.BucketName = "mybucket"
114117
jc.BucketConfig.EndpointURL = "https://foo.bar.baz"
115118
jc.BucketConfig.Region = "us-east-1"
@@ -1422,6 +1425,8 @@ dummyKey: dummyValue
14221425
Expect(js.Template.Spec.Affinity).NotTo(BeNil())
14231426
Expect(js.Template.Spec.RestartPolicy).To(Equal(corev1.RestartPolicyNever))
14241427
Expect(js.Template.Spec.ServiceAccountName).To(Equal("foo"))
1428+
Expect(js.Template.Spec.ImagePullSecrets).To(HaveLen(1))
1429+
Expect(js.Template.Spec.ImagePullSecrets[0].Name).To(Equal("my-registry-secret"))
14251430
Expect(js.Template.Spec.Volumes).To(HaveLen(2))
14261431
Expect(js.Template.Spec.Volumes[0].EmptyDir).NotTo(BeNil())
14271432
Expect(js.Template.Spec.Volumes[1].EmptyDir).NotTo(BeNil())
@@ -1620,6 +1625,9 @@ dummyKey: dummyValue
16201625
MountPath: ptr.To[string]("/path/to/dir"),
16211626
},
16221627
}
1628+
jc.ImagePullSecrets = []mocov1beta2.LocalObjectReferenceApplyConfiguration{
1629+
{Name: ptr.To[string]("my-registry-secret")},
1630+
}
16231631
jc.BucketConfig.BucketName = "mybucket"
16241632
jc.BucketConfig.EndpointURL = "https://foo.bar.baz"
16251633
jc.BucketConfig.Region = "us-east-1"
@@ -1653,6 +1661,8 @@ dummyKey: dummyValue
16531661
Expect(js.Template.Labels).NotTo(BeEmpty())
16541662
Expect(js.Template.Spec.RestartPolicy).To(Equal(corev1.RestartPolicyNever))
16551663
Expect(js.Template.Spec.ServiceAccountName).To(Equal("foo"))
1664+
Expect(js.Template.Spec.ImagePullSecrets).To(HaveLen(1))
1665+
Expect(js.Template.Spec.ImagePullSecrets[0].Name).To(Equal("my-registry-secret"))
16561666
Expect(js.Template.Spec.Volumes).To(HaveLen(2))
16571667
Expect(js.Template.Spec.Volumes[0].EmptyDir).NotTo(BeNil())
16581668
Expect(js.Template.Spec.Volumes[1].EmptyDir).NotTo(BeNil())

0 commit comments

Comments
 (0)