Repository: tektoncd/operator
Pull request #349 (2021-07-05 12:42:48)

Add namespace transformer for ClusterInterceptor
# Changes

As of now operator is ignoring namespace transformation
on new Resource [clusterInterceptor] and thus it was giving an error.

Add a `injectNamespaceCRDClusterInterceptorClientConfig` function which
will add transformation for ClusterInterceptor CRD.

Signed-off-by: vinamra28 <vinjain@redhat.com>
# Submitter Checklist

These are the criteria that every PR should meet, please check them off as you
review them:

- [ ] Includes [tests](https://github.com/tektoncd/community/blob/master/standards.md#principles) (if functionality changed/added)
- [ ] Includes [docs](https://github.com/tektoncd/community/blob/master/standards.md#principles) (if user facing)
- [X] Commit messages follow [commit message best practices](https://github.com/tektoncd/community/blob/master/standards.md#commit-messages)

_See [the contribution guide](https://github.com/tektoncd/operator/blob/master/CONTRIBUTING.md) for more details._

# Release Notes
```release-note
NONE
```

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 **nikhil-thomas**
You can assign the PR to them by writing `/assign @nikhil-thomas` in a comment when ready.

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

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

- **[OWNERS](https://github.com/tektoncd/operator/blob/main/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":["nikhil-thomas"]} -->
tekton-robot: The following is the coverage report on the affected files.
Say `/test pull-tekton-operator-go-coverage` to re-run this coverage report

File | Old Coverage | New Coverage | Delta
---- |:------------:|:------------:|:-----:
[pkg/reconciler/common/transformers.go](https://storage.cloud.google.com/tekton-prow/pr-logs/pull/tektoncd_operator/349/pull-tekton-operator-go-coverage/1412029261820203008/artifacts/line-cov.html#file0) | 67.6% | 66.0% | -1.6

tekton-robot: The following is the coverage report on the affected files.
Say `/test pull-tekton-operator-go-coverage` to re-run this coverage report

File | Old Coverage | New Coverage | Delta
---- |:------------:|:------------:|:-----:
[pkg/reconciler/common/transformers.go](https://storage.cloud.google.com/tekton-prow/pr-logs/pull/tektoncd_operator/349/pull-tekton-operator-go-coverage/1412058328674078721/artifacts/line-cov.html#file0) | 67.6% | 68.8% | 1.2

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

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

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?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/main/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/common/transformers.go b/pkg/reconciler/common/transformers.go
index aad4a556a..1697cad19 100644
--- a/pkg/reconciler/common/transformers.go
+++ b/pkg/reconciler/common/transformers.go
@@ -53,6 +53,7 @@ func transformers(ctx context.Context, obj v1alpha1.TektonComponent) []mf.Transf
 		mf.InjectOwner(obj),
 		injectNamespaceConditional(AnnotationPreserveNS, obj.GetSpec().GetTargetNamespace()),
 		injectNamespaceCRDWebhookClientConfig(obj.GetSpec().GetTargetNamespace()),
+		injectNamespaceCRClusterInterceptorClientConfig(obj.GetSpec().GetTargetNamespace()),
 	}
 }
 
@@ -123,6 +124,24 @@ func injectNamespaceCRDWebhookClientConfig(targetNamespace string) mf.Transforme
 	}
 }
 
+func injectNamespaceCRClusterInterceptorClientConfig(targetNamespace string) mf.Transformer {
+	return func(u *unstructured.Unstructured) error {
+		kind := strings.ToLower(u.GetKind())
+		if kind != "clusterinterceptor" {
+			return nil
+		}
+		service, found, err := unstructured.NestedFieldNoCopy(u.Object, "spec", "clientConfig", "service")
+		if !found || err != nil {
+			return err
+		}
+		m := service.(map[string]interface{})
+		if _, ok := m["namespace"]; ok {
+			m["namespace"] = targetNamespace
+		}
+		return nil
+	}
+}
+
 // ImagesFromEnv will provide map of key value.
 func ImagesFromEnv(prefix string) map[string]string {
 	images := map[string]string{}


Test patch:
diff --git a/pkg/reconciler/common/testdata/test-replace-namespace-in-cluster-interceptor.yaml b/pkg/reconciler/common/testdata/test-replace-namespace-in-cluster-interceptor.yaml
new file mode 100644
index 000000000..64f11caf7
--- /dev/null
+++ b/pkg/reconciler/common/testdata/test-replace-namespace-in-cluster-interceptor.yaml
@@ -0,0 +1,11 @@
+---
+apiVersion: triggers.tekton.dev/v1alpha1
+kind: ClusterInterceptor
+metadata:
+  name: cel
+spec:
+  clientConfig:
+    service:
+      name: tekton-triggers-core-interceptors
+      path: "cel"
+      namespace: tekton-pipelines
diff --git a/pkg/reconciler/common/transformers_test.go b/pkg/reconciler/common/transformers_test.go
index 0606cab98..b79b818a7 100644
--- a/pkg/reconciler/common/transformers_test.go
+++ b/pkg/reconciler/common/transformers_test.go
@@ -18,7 +18,6 @@ package common
 
 import (
 	"context"
-	"gotest.tools/assert"
 	"os"
 	"path"
 	"reflect"
@@ -27,6 +26,7 @@ import (
 	"github.com/google/go-cmp/cmp"
 	mf "github.com/manifestival/manifestival"
 	"github.com/tektoncd/operator/pkg/apis/operator/v1alpha1"
+	"gotest.tools/v3/assert"
 	appsv1 "k8s.io/api/apps/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -366,3 +366,18 @@ func TestReplaceNamespaceInDeploymentArgs(t *testing.T) {
 	assert.Equal(t, args[0], "-api_addr")
 	assert.Equal(t, args[1], "tekton-results-api-service.openshift-pipelines.svc.cluster.local:50051")
 }
+
+func TestReplaceNamespaceInClusterInterceptor(t *testing.T) {
+	testData := path.Join("testdata", "test-replace-namespace-in-cluster-interceptor.yaml")
+	manifest, err := mf.ManifestFrom(mf.Recursive(testData))
+	assertNoEror(t, err)
+
+	manifest, err = manifest.Transform(injectNamespaceCRClusterInterceptorClientConfig("foobar"))
+	assertNoEror(t, err)
+
+	clusterInterceptor := manifest.Resources()[0].Object
+	service, _, err := unstructured.NestedFieldNoCopy(clusterInterceptor, "spec", "clientConfig", "service")
+	m := service.(map[string]interface{})
+	assertNoEror(t, err)
+	assert.Equal(t, "foobar", m["namespace"])
+}