Repository: tektoncd/operator
Pull request #1240 (2023-01-04 08:09:48)

[release-v0.63.x] PAC: fixes service monitor target namespace
This is an automated cherry-pick of #1239

/assign vdemeester

```release-note
Your release note here
```

tekton-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by:
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **piyush-garg** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @piyush-garg` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://prow.tekton.dev/command-help?repo=tektoncd%2Foperator).

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

- **[OWNERS](https://github.com/tektoncd/operator/blob/release-v0.63.x/OWNERS)**

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":["piyush-garg"]} -->
tekton-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/tektoncd/operator/pull/1240#pullrequestreview-1235480424" title="LGTM">vdemeester</a>*

The full list of commands accepted by this bot can be found [here](https://prow.tekton.dev/command-help?repo=tektoncd%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/tektoncd/operator/blob/release-v0.63.x/OWNERS)~~ [vdemeester]

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/openshift/common/transformer.go b/pkg/reconciler/openshift/common/transformer.go
index 8a7a7b833..609d30b8f 100644
--- a/pkg/reconciler/openshift/common/transformer.go
+++ b/pkg/reconciler/openshift/common/transformer.go
@@ -170,3 +170,17 @@ func RemoveFsGroupForJob() mf.Transformer {
 		return nil
 	}
 }
+
+func UpdateServiceMonitorTargetNamespace(targetNamespace string) mf.Transformer {
+	return func(u *unstructured.Unstructured) error {
+		if u.GetKind() != "ServiceMonitor" {
+			return nil
+		}
+		nsSelector, found, err := unstructured.NestedFieldNoCopy(u.Object, "spec", "namespaceSelector")
+		if !found || err != nil {
+			return err
+		}
+		nsSelector.(map[string]interface{})["matchNames"].([]interface{})[0] = targetNamespace
+		return nil
+	}
+}
diff --git a/pkg/reconciler/openshift/openshiftpipelinesascode/transform.go b/pkg/reconciler/openshift/openshiftpipelinesascode/transform.go
index ef66285af..031af6e9f 100644
--- a/pkg/reconciler/openshift/openshiftpipelinesascode/transform.go
+++ b/pkg/reconciler/openshift/openshiftpipelinesascode/transform.go
@@ -46,6 +46,7 @@ func filterAndTransform(extension common.Extension) client.FilterAndTransform {
 			common.ApplyProxySettings,
 			occommon.ApplyCABundles,
 			common.CopyConfigMap(pipelinesAsCodeCM, pac.Spec.Settings),
+			occommon.UpdateServiceMonitorTargetNamespace(pac.Spec.TargetNamespace),
 		}
 
 		allTfs := append(tfs, extension.Transformers(pac)...)


Test patch:
diff --git a/pkg/reconciler/openshift/common/testdata/test-inject-ns-in-servicemonitor-expected.yaml b/pkg/reconciler/openshift/common/testdata/test-inject-ns-in-servicemonitor-expected.yaml
new file mode 100644
index 000000000..3be5696e6
--- /dev/null
+++ b/pkg/reconciler/openshift/common/testdata/test-inject-ns-in-servicemonitor-expected.yaml
@@ -0,0 +1,21 @@
+apiVersion: monitoring.coreos.com/v1
+kind: ServiceMonitor
+metadata:
+  name: pipelines-as-code-monitor
+  namespace: pipelines-as-code
+  labels:
+    app.kubernetes.io/version: "v0.15.0"
+    app.kubernetes.io/part-of: pipelines-as-code
+  annotations:
+    networkoperator.openshift.io/ignore-errors: ""
+spec:
+  endpoints:
+    - interval: 10s
+      port: http-metrics
+  jobLabel: app
+  namespaceSelector:
+    matchNames:
+      - its-me-ns
+  selector:
+    matchLabels:
+      app: pipelines-as-code-watcher
diff --git a/pkg/reconciler/openshift/common/testdata/test-inject-ns-in-servicemonitor.yaml b/pkg/reconciler/openshift/common/testdata/test-inject-ns-in-servicemonitor.yaml
new file mode 100644
index 000000000..eeaf7af5a
--- /dev/null
+++ b/pkg/reconciler/openshift/common/testdata/test-inject-ns-in-servicemonitor.yaml
@@ -0,0 +1,21 @@
+apiVersion: monitoring.coreos.com/v1
+kind: ServiceMonitor
+metadata:
+  name: pipelines-as-code-monitor
+  namespace: pipelines-as-code
+  labels:
+    app.kubernetes.io/version: "v0.15.0"
+    app.kubernetes.io/part-of: pipelines-as-code
+  annotations:
+    networkoperator.openshift.io/ignore-errors: ""
+spec:
+  endpoints:
+    - interval: 10s
+      port: http-metrics
+  jobLabel: app
+  namespaceSelector:
+    matchNames:
+      - pipelines-as-code
+  selector:
+    matchLabels:
+      app: pipelines-as-code-watcher
diff --git a/pkg/reconciler/openshift/common/transformer_test.go b/pkg/reconciler/openshift/common/transformer_test.go
index 4f6e1a8e1..e66ffae16 100644
--- a/pkg/reconciler/openshift/common/transformer_test.go
+++ b/pkg/reconciler/openshift/common/transformer_test.go
@@ -147,3 +147,21 @@ func TestRemoveFsGroup(t *testing.T) {
 		t.Errorf("failed to update deployment %s", diff.PrintWantGot(d))
 	}
 }
+
+func TestUpdateServiceMonitorTargetNamespace(t *testing.T) {
+	targetNs := "its-me-ns"
+	testData := path.Join("testdata", "test-inject-ns-in-servicemonitor.yaml")
+	manifest, err := mf.ManifestFrom(mf.Recursive(testData))
+	assert.NilError(t, err)
+
+	updatedManifest, err := manifest.Transform(UpdateServiceMonitorTargetNamespace(targetNs))
+	assert.NilError(t, err)
+
+	testData = path.Join("testdata", "test-inject-ns-in-servicemonitor-expected.yaml")
+	expectedManifest, err := mf.ManifestFrom(mf.Recursive(testData))
+	assert.NilError(t, err)
+
+	if d := cmp.Diff(updatedManifest.Resources()[0], expectedManifest.Resources()[0]); d != "" {
+		t.Errorf("failed to update deployment %s", diff.PrintWantGot(d))
+	}
+}