Skip to content

Commit e3f029b

Browse files
committed
fix: use custom struct for PodProfile template metadata
PodTemplateSpec does not support certain metadata fields required for PodProfile. Defined PodProfileTemplateMetadata as a dedicated type to overcome this limitation.
1 parent 32665cf commit e3f029b

File tree

12 files changed

+191
-10
lines changed

12 files changed

+191
-10
lines changed

api/v1/podprofile_types.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,25 @@ type PodProfileSpec struct {
3131

3232
// Template describes the pods that will be created.
3333
// +required
34-
Template corev1.PodTemplateSpec `json:"template"`
34+
Template PodProfileTemplate `json:"template"`
35+
}
36+
37+
type PodProfileTemplate struct {
38+
// +optional
39+
PodProfileTemplateMetadata `json:"metadata,omitempty"`
40+
// Specification of the desired behavior of the pod.
41+
// More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
42+
// +optional
43+
Spec corev1.PodSpec `json:"spec,omitempty"`
44+
}
45+
46+
type PodProfileTemplateMetadata struct {
47+
// Additional labels for generated pod.
48+
// +optional
49+
Labels map[string]string `json:"labels,omitempty"`
50+
// Additional annotations for generated pod.
51+
// +optional
52+
Annotations map[string]string `json:"annotations,omitempty"`
3553
}
3654

3755
// PodProfileStatus defines the observed state of PodProfile.

api/v1/zz_generated.deepcopy.go

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

config/crd/bases/pixiv.net_cronjobs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6-
controller-gen.kubebuilder.io/version: v0.17.2
6+
controller-gen.kubebuilder.io/version: v0.20.0
77
name: cronjobs.pixiv.net
88
spec:
99
group: pixiv.net

config/crd/bases/pixiv.net_jobs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6-
controller-gen.kubebuilder.io/version: v0.17.2
6+
controller-gen.kubebuilder.io/version: v0.20.0
77
name: jobs.pixiv.net
88
spec:
99
group: pixiv.net

config/crd/bases/pixiv.net_podprofiles.yaml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6-
controller-gen.kubebuilder.io/version: v0.17.2
6+
controller-gen.kubebuilder.io/version: v0.20.0
77
name: podprofiles.pixiv.net
88
spec:
99
group: pixiv.net
@@ -43,9 +43,17 @@ spec:
4343
description: Template describes the pods that will be created.
4444
properties:
4545
metadata:
46-
description: |-
47-
Standard object's metadata.
48-
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
46+
properties:
47+
annotations:
48+
additionalProperties:
49+
type: string
50+
description: Additional annotations for generated pod.
51+
type: object
52+
labels:
53+
additionalProperties:
54+
type: string
55+
description: Additional labels for generated pod.
56+
type: object
4957
type: object
5058
spec:
5159
description: |-

internal/construct/job.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ func BatchJobSpec(ctx context.Context, jobProfileSpec *pixivnetv1.JobProfileSpec
156156
spec.Suspend = params.Suspend
157157
spec.PodReplacementPolicy = params.PodReplacementPolicy
158158
spec.ManagedBy = params.ManagedBy
159-
spec.Template = podProfile.Spec.Template // Set the podprofile directly as the target for patching.
159+
// Set the podprofile directly as the target for patching.
160+
spec.Template.Labels = podProfile.Spec.Template.Labels
161+
spec.Template.Annotations = podProfile.Spec.Template.Annotations
162+
spec.Template.Spec = podProfile.Spec.Template.Spec
160163

161164
if len(jobProfileSpec.Patches) == 0 { // If no patch is provided, no action is necessary.
162165
return &batchJob.Spec, nil

internal/controller/common_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func newPodProfile(resourceName string) *pixivnetv1.PodProfile {
4646
Namespace: testNamespace,
4747
},
4848
Spec: pixivnetv1.PodProfileSpec{
49-
Template: corev1.PodTemplateSpec{
49+
Template: pixivnetv1.PodProfileTemplate{
5050
Spec: corev1.PodSpec{
5151
Containers: []corev1.Container{
5252
{

internal/controller/podprofile_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var _ = Describe("PodProfile Controller", func() {
5555
Namespace: testNamespace,
5656
},
5757
Spec: pixivnetv1.PodProfileSpec{
58-
Template: corev1.PodTemplateSpec{
58+
Template: pixivnetv1.PodProfileTemplate{
5959
Spec: corev1.PodSpec{
6060
Containers: []corev1.Container{
6161
{

test/e2e/e2e_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,13 +509,82 @@ var _ = Describe("Manager", Ordered, func() {
509509
}
510510
return names[0], true
511511
}
512+
// Get a list of Pod names spwned from a batch Job.
513+
listBatchJobPods := func(batchJobName string) ([]string, error) {
514+
output, err := utils.Run(
515+
utils.KubectlCmd(
516+
"-n", namespace, "get", "pod",
517+
"-o", "jsonpath={.items[*].metadata.name}",
518+
"-l", "job-name="+batchJobName,
519+
),
520+
)
521+
if err != nil {
522+
return nil, err
523+
}
524+
return strings.Split(output, " "), nil
525+
}
526+
// Verify that the batch Job created only one Pod.
527+
// If so, return the name the Pod.
528+
ensureOnlyOneBatchJobManagedPodCreated := func(batchJobName string) (string, bool) {
529+
By(fmt.Sprintf("making ensure the batch Job %s created only one Pod", batchJobName))
530+
names, err := listBatchJobPods(batchJobName)
531+
if !Expect(err).To(Succeed()) {
532+
return "", false
533+
}
534+
if !Expect(names).Should(HaveLen(1)) {
535+
return "", false
536+
}
537+
return names[0], true
538+
}
512539

513540
It("should reconcile resources successfully", func() {
514541
type testCase struct {
515542
manifest string // directory in test/manifests/.
516543
additionalAssertion func(*testCase) // additional assertion; does nothing if nil.
517544
}
518545
testCases := []testCase{
546+
{
547+
manifest: "pod-metadata",
548+
additionalAssertion: func(tc *testCase) {
549+
name, ok := ensureOnlyOneBatchJobCreated("job-" + tc.manifest)
550+
if !ok {
551+
return
552+
}
553+
output, err := utils.Run(utils.KubectlCmd("-n", namespace,
554+
"get", "job", name, "-o", "jsonpath={.spec.template.spec.containers[0].command}",
555+
))
556+
if !Expect(err).To(Succeed()) {
557+
return
558+
}
559+
Expect(output).To(Equal(`["perl","-Mbignum=bpi","-wle","print bpi(100)"]`))
560+
561+
pod, ok := ensureOnlyOneBatchJobManagedPodCreated(name)
562+
if !ok {
563+
return
564+
}
565+
for _, check := range []struct {
566+
path string
567+
want string
568+
}{
569+
{
570+
path: `{.metadata.labels.app}`,
571+
want: "pod-metadata",
572+
},
573+
{
574+
path: `{.metadata.annotations.desc}`,
575+
want: "add pod-metadata",
576+
},
577+
} {
578+
output, err := utils.Run(utils.KubectlCmd("-n", namespace,
579+
"get", "pod", pod, "-o", "jsonpath="+check.path,
580+
))
581+
if !Expect(err).To(Succeed()) {
582+
return
583+
}
584+
Expect(output).To(Equal(check.want))
585+
}
586+
},
587+
},
519588
{
520589
manifest: "sample",
521590
additionalAssertion: func(tc *testCase) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resources:
2+
- v1_podprofile.yaml
3+
- v1_job.yaml

0 commit comments

Comments
 (0)