From 97f7fcc3035e25fea92b685ace508e91a27f31b1 Mon Sep 17 00:00:00 2001 From: Dibyo Mukherjee Date: Mon, 25 Jan 2021 16:45:57 -0500 Subject: [PATCH] Send Tekton installation namespace to EL The EventListener did not have knowledge of which namespace Triggers was installed in. Instead it always assumed it was `tekton-pipelines` leading to the bug described in #923. This commit fixes this by having the Reconciler send the installation namespace as a environment variable set on the EventListener's pod. NOTE: This fix is temporary and should not be necessary once #868 is implemented since then we will resolve the Interceptor's address using information from its CRD. Fixes #923 Signed-off-by: Dibyo Mukherjee --- pkg/interceptors/interceptors.go | 5 ++--- pkg/interceptors/interceptors_test.go | 16 ++++++++++++++++ .../v1alpha1/eventlistener/eventlistener.go | 4 ++++ .../v1alpha1/eventlistener/eventlistener_test.go | 6 ++++++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/pkg/interceptors/interceptors.go b/pkg/interceptors/interceptors.go index d49fbd6a2..7b4fce2d8 100644 --- a/pkg/interceptors/interceptors.go +++ b/pkg/interceptors/interceptors.go @@ -24,10 +24,9 @@ import ( "io/ioutil" "net/http" "net/url" + "os" "path" - "github.com/tektoncd/triggers/pkg/system" - "google.golang.org/grpc/codes" triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" @@ -188,7 +187,7 @@ func ResolveURL(i *triggersv1.TriggerInterceptor) *url.URL { } return &url.URL{ Scheme: "http", - Host: fmt.Sprintf("%s.%s.svc", CoreInterceptorsHost, system.DefaultNamespace), + Host: fmt.Sprintf("%s.%s.svc", CoreInterceptorsHost, os.Getenv("TEKTON_INSTALL_NAMESPACE")), Path: path, } } diff --git a/pkg/interceptors/interceptors_test.go b/pkg/interceptors/interceptors_test.go index ec4679f72..d8870a86d 100644 --- a/pkg/interceptors/interceptors_test.go +++ b/pkg/interceptors/interceptors_test.go @@ -22,6 +22,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "os" "strings" "testing" @@ -327,12 +328,27 @@ func TestResolvePath(t *testing.T) { want: "http://tekton-triggers-core-interceptors.tekton-pipelines.svc", }} { t.Run(tc.want, func(t *testing.T) { + os.Setenv("TEKTON_INSTALL_NAMESPACE", "tekton-pipelines") + t.Cleanup(func() { os.Unsetenv("TEKTON_INSTALL_NAMESPACE") }) got := interceptors.ResolveURL(&tc.in) if tc.want != got.String() { t.Fatalf("ResolveURL() want: %s; got: %s", tc.want, got) } }) } + + t.Run("different namespaces", func(t *testing.T) { + os.Setenv("TEKTON_INSTALL_NAMESPACE", "another-namespace") + t.Cleanup(func() { os.Unsetenv("TEKTON_INSTALL_NAMESPACE") }) + in := &triggersv1.EventInterceptor{ + Bitbucket: &triggersv1.BitbucketInterceptor{}, + } + got := interceptors.ResolveURL(in) + want := "http://tekton-triggers-core-interceptors.another-namespace.svc/bitbucket" + if want != got.String() { + t.Fatalf("ResolveURL() want: %s; got: %s", want, got) + } + }) } // testServer creates a httptest server with the passed in handler and returns a http.Client that diff --git a/pkg/reconciler/v1alpha1/eventlistener/eventlistener.go b/pkg/reconciler/v1alpha1/eventlistener/eventlistener.go index 451b5b271..daa25a4e7 100644 --- a/pkg/reconciler/v1alpha1/eventlistener/eventlistener.go +++ b/pkg/reconciler/v1alpha1/eventlistener/eventlistener.go @@ -26,6 +26,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" listers "github.com/tektoncd/triggers/pkg/client/listers/triggers/v1alpha1" + "github.com/tektoncd/triggers/pkg/system" "go.uber.org/zap" "golang.org/x/xerrors" appsv1 "k8s.io/api/apps/v1" @@ -462,6 +463,9 @@ func getContainer(el *v1alpha1.EventListener, c Config) corev1.Container { FieldPath: "metadata.namespace", }, }, + }, { + Name: "TEKTON_INSTALL_NAMESPACE", + Value: system.GetNamespace(), }} certEnv := map[string]*corev1.EnvVarSource{} diff --git a/pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go b/pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go index 56e1e4c59..3d2863b8d 100644 --- a/pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go +++ b/pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go @@ -248,6 +248,9 @@ func makeDeployment(ops ...func(d *appsv1.Deployment)) *appsv1.Deployment { FieldPath: "metadata.namespace", }, }, + }, { + Name: "TEKTON_INSTALL_NAMESPACE", + Value: "tekton-pipelines", }}, }}, Volumes: []corev1.Volume{{ @@ -333,6 +336,9 @@ var withTLSConfig = func(d *appsv1.Deployment) { FieldPath: "metadata.namespace", }, }, + }, { + Name: "TEKTON_INSTALL_NAMESPACE", + Value: "tekton-pipelines", }, { Name: "TLS_CERT", ValueFrom: &corev1.EnvVarSource{