Repository: knative/operator
Pull request #1554 (2023-09-07 14:14:41)

Fixed the image overriding issue for Jobs
Fixes: #1553



knative-prow[bot]: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/operator/pull/1554#" title="Author self-approved">houshengbo</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Foperator).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/knative/operator/blob/main/OWNERS)~~ [houshengbo]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
dprotaso: /lgtm
/approve
knative-prow[bot]: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/operator/pull/1554#issuecomment-1710565560" title="Approved">dprotaso</a>*, *<a href="https://github.com/knative/operator/pull/1554#" title="Author self-approved">houshengbo</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Foperator).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/knative/operator/blob/main/OWNERS)~~ [dprotaso,houshengbo]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/pkg/reconciler/common/images.go b/pkg/reconciler/common/images.go
index 0630388f7..12ebd53ba 100644
--- a/pkg/reconciler/common/images.go
+++ b/pkg/reconciler/common/images.go
@@ -94,7 +94,7 @@ func ImageTransform(registry *base.Registry, log *zap.SugaredLogger) mf.Transfor
 		}
 
 		objName := obj.GetName()
-		if objName == "" {
+		if obj.GetGenerateName() != "" {
 			objName = obj.GetGenerateName()
 		}
 		log.Debugw("Updating", "name", objName, "registry", registry)
diff --git a/pkg/reconciler/common/transformers.go b/pkg/reconciler/common/transformers.go
index 00ff77d59..8b8fcdc0d 100644
--- a/pkg/reconciler/common/transformers.go
+++ b/pkg/reconciler/common/transformers.go
@@ -30,9 +30,9 @@ func transformers(ctx context.Context, obj base.KComponent) []mf.Transformer {
 	return []mf.Transformer{
 		injectOwner(obj),
 		mf.InjectNamespace(obj.GetNamespace()),
-		JobTransform(obj),
 		HighAvailabilityTransform(obj, logger),
 		ImageTransform(obj.GetSpec().GetRegistry(), logger),
+		JobTransform(obj),
 		ConfigMapTransform(obj.GetSpec().GetConfig(), logger),
 		ResourceRequirementsTransform(obj, logger),
 		OverridesTransform(obj.GetSpec().GetWorkloadOverrides(), logger),


Test patch:
diff --git a/pkg/reconciler/common/images_test.go b/pkg/reconciler/common/images_test.go
index 7da38fa1b..080a18aa2 100644
--- a/pkg/reconciler/common/images_test.go
+++ b/pkg/reconciler/common/images_test.go
@@ -267,6 +267,14 @@ func TestResourceTransform(t *testing.T) {
 			util.AssertEqual(t, err, nil)
 			util.AssertDeepEqual(t, jobGenerated.Spec.Template.Spec.Containers, tt.expected)
 
+			// test for job with generated name and name
+			unstructuredJobGeneratedName := util.MakeUnstructured(t, makeJobNameGeneratedName(tt.name+"non-generated", tt.name, podSpec))
+			transform(&unstructuredJobGeneratedName)
+			jobGeneratedName := &batchv1.Job{}
+			err = scheme.Scheme.Convert(&unstructuredJobGeneratedName, jobGeneratedName, nil)
+			util.AssertEqual(t, err, nil)
+			util.AssertDeepEqual(t, jobGeneratedName.Spec.Template.Spec.Containers, tt.expected)
+
 			// test for statefulset
 			unstructuredStatefulSet := util.MakeUnstructured(t, makeStatefulSet(tt.name, podSpec))
 			transform(&unstructuredStatefulSet)
@@ -461,6 +469,23 @@ func makeStatefulSet(name string, podSpec corev1.PodSpec) *appsv1.StatefulSet {
 	}
 }
 
+func makeJobNameGeneratedName(name, genaredtedName string, podSpec corev1.PodSpec) *batchv1.Job {
+	return &batchv1.Job{
+		TypeMeta: metav1.TypeMeta{
+			Kind: "Job",
+		},
+		ObjectMeta: metav1.ObjectMeta{
+			Name:         name,
+			GenerateName: genaredtedName,
+		},
+		Spec: batchv1.JobSpec{
+			Template: corev1.PodTemplateSpec{
+				Spec: podSpec,
+			},
+		},
+	}
+}
+
 func makeJob(name string, podSpec corev1.PodSpec) *batchv1.Job {
 	return &batchv1.Job{
 		TypeMeta: metav1.TypeMeta{