From 4c238e0feebff94f87c1deb32ed767b619666376 Mon Sep 17 00:00:00 2001 From: David BENQUE Date: Wed, 12 Dec 2018 21:37:07 +0100 Subject: [PATCH 1/6] k8sutil: ExposeMetricsPort works for ClusterScoped ExposeMetricsPort was failing to generate the kubernetes service to scrape metrics if the operator was generated with --cluster-scoped flag Fixes #835 --- pkg/k8sutil/constants.go | 8 +++++++- pkg/k8sutil/k8sutil.go | 12 ++++++++++++ pkg/scaffold/operator.go | 6 ++++++ pkg/scaffold/operator_test.go | 4 ++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/pkg/k8sutil/constants.go b/pkg/k8sutil/constants.go index 0ac85c02cc2..3f3aa9447ef 100644 --- a/pkg/k8sutil/constants.go +++ b/pkg/k8sutil/constants.go @@ -20,9 +20,15 @@ const ( KubeConfigEnvVar = "KUBECONFIG" // WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE - // which is the namespace that the pod is currently running in. + // which is the namespace where the watch activity happens and where the pod is currently running in. + // this value is empty if the operator is running with clusterScope. WatchNamespaceEnvVar = "WATCH_NAMESPACE" + // NamespaceEnvVar is the constant for env variable NAMESPACE + // which is the namespace that the pod is currently running in. + // this value is set only if the operator is running with clusterScope + NamespaceEnvVar = "NAMESPACE" + // OperatorNameEnvVar is the constant for env variable OPERATOR_NAME // wich is the name of the current operator OperatorNameEnvVar = "OPERATOR_NAME" diff --git a/pkg/k8sutil/k8sutil.go b/pkg/k8sutil/k8sutil.go index b0d0555151e..713b90b36a1 100644 --- a/pkg/k8sutil/k8sutil.go +++ b/pkg/k8sutil/k8sutil.go @@ -33,6 +33,15 @@ func GetWatchNamespace() (string, error) { return ns, nil } +// GetNamespace returns the namespace the operator should be running in. +func GetNamespace() (string, error) { + ns, found := os.LookupEnv(NamespaceEnvVar) + if !found { + return "", fmt.Errorf("%s must be set", NamespaceEnvVar) + } + return ns, nil +} + // GetOperatorName return the operator name func GetOperatorName() (string, error) { operatorName, found := os.LookupEnv(OperatorNameEnvVar) @@ -55,6 +64,9 @@ func InitOperatorService() (*v1.Service, error) { if err != nil { return nil, err } + if namespace == "" { // This will happen for clusterScoped operators + namespace, _ = GetNamespace() + } service := &v1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: operatorName, diff --git a/pkg/scaffold/operator.go b/pkg/scaffold/operator.go index df3cf2e6349..518ec4fc71d 100644 --- a/pkg/scaffold/operator.go +++ b/pkg/scaffold/operator.go @@ -70,6 +70,12 @@ spec: periodSeconds: 10 failureThreshold: 1 env: + {{- if .IsClusterScoped }} + - name: NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + {{- end}} - name: WATCH_NAMESPACE {{- if .IsClusterScoped }} value: "" diff --git a/pkg/scaffold/operator_test.go b/pkg/scaffold/operator_test.go index 0638d95762b..049874cc73b 100644 --- a/pkg/scaffold/operator_test.go +++ b/pkg/scaffold/operator_test.go @@ -126,6 +126,10 @@ spec: periodSeconds: 10 failureThreshold: 1 env: + - name: NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace - name: WATCH_NAMESPACE value: "" - name: POD_NAME From 650b9ee79915d8bec438c527a3ab20eec9a541f2 Mon Sep 17 00:00:00 2001 From: David BENQUE Date: Thu, 13 Dec 2018 06:02:21 +0100 Subject: [PATCH 2/6] k8sutil: increase error and test coverage Fixes #835 --- pkg/k8sutil/k8sutil.go | 4 +- pkg/k8sutil/k8sutil_test.go | 102 ++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) diff --git a/pkg/k8sutil/k8sutil.go b/pkg/k8sutil/k8sutil.go index 713b90b36a1..91befaa6f1f 100644 --- a/pkg/k8sutil/k8sutil.go +++ b/pkg/k8sutil/k8sutil.go @@ -65,7 +65,9 @@ func InitOperatorService() (*v1.Service, error) { return nil, err } if namespace == "" { // This will happen for clusterScoped operators - namespace, _ = GetNamespace() + if namespace, err = GetNamespace(); err != nil || namespace == "" { + return nil, fmt.Errorf("one of the env var %s or %s must not be empty", WatchNamespaceEnvVar, NamespaceEnvVar) + } } service := &v1.Service{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/k8sutil/k8sutil_test.go b/pkg/k8sutil/k8sutil_test.go index 5c5b71aed96..4a21252f925 100644 --- a/pkg/k8sutil/k8sutil_test.go +++ b/pkg/k8sutil/k8sutil_test.go @@ -19,6 +19,10 @@ import ( "os" "reflect" "testing" + + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + intstr "k8s.io/apimachinery/pkg/util/intstr" ) func TestGetOperatorName(t *testing.T) { @@ -73,3 +77,101 @@ func TestGetOperatorName(t *testing.T) { _ = os.Unsetenv(test.envVarKey) } } + +func TestInitOperatorService(t *testing.T) { + operatorName := "myTestOperator" + namespace := "myTestNamespace" + + serviceExp := &v1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: operatorName, + Namespace: namespace, + Labels: map[string]string{"name": operatorName}, + }, + TypeMeta: metav1.TypeMeta{ + Kind: "Service", + APIVersion: "v1", + }, + Spec: v1.ServiceSpec{ + Ports: []v1.ServicePort{ + { + Port: PrometheusMetricsPort, + Protocol: v1.ProtocolTCP, + TargetPort: intstr.IntOrString{ + Type: intstr.String, + StrVal: PrometheusMetricsPortName, + }, + Name: PrometheusMetricsPortName, + }, + }, + Selector: map[string]string{"name": operatorName}, + }, + } + + type Output struct { + service *v1.Service + err error + } + + type Scenario struct { + name string + envVars map[string]string + expectedOutput Output + } + tests := []Scenario{ + { + name: "WatchNamespace Case", + envVars: map[string]string{OperatorNameEnvVar: operatorName, WatchNamespaceEnvVar: namespace}, + expectedOutput: Output{ + service: serviceExp, + err: nil, + }, + }, + { + name: "ClusterScope Case", + envVars: map[string]string{OperatorNameEnvVar: operatorName, NamespaceEnvVar: namespace, WatchNamespaceEnvVar: ""}, + expectedOutput: Output{ + service: serviceExp, + err: nil, + }, + }, + { + name: "Error no namespace and empty watchnamespace", + envVars: map[string]string{OperatorNameEnvVar: operatorName, WatchNamespaceEnvVar: ""}, + expectedOutput: Output{ + service: nil, + err: fmt.Errorf("one of the env var %s or %s must not be empty", WatchNamespaceEnvVar, NamespaceEnvVar), + }, + }, + { + name: "Error no namespace no watchnamespace", + envVars: map[string]string{OperatorNameEnvVar: operatorName}, + expectedOutput: Output{ + service: nil, + err: fmt.Errorf("%s must be set", WatchNamespaceEnvVar), + }, + }, + { + name: "Error no namespace and watchnamespace are empty", + envVars: map[string]string{OperatorNameEnvVar: operatorName, NamespaceEnvVar: "", WatchNamespaceEnvVar: ""}, + expectedOutput: Output{ + service: nil, + err: fmt.Errorf("one of the env var %s or %s must not be empty", WatchNamespaceEnvVar, NamespaceEnvVar), + }, + }, + } + + for _, test := range tests { + for k, v := range test.envVars { + _ = os.Setenv(k, v) + } + service, err := InitOperatorService() + if !(reflect.DeepEqual(err, test.expectedOutput.err) && reflect.DeepEqual(service, test.expectedOutput.service)) { + t.Errorf("test %s failed, expected ouput: %s,%v; got: %s,%v", test.name, test.expectedOutput.service, test.expectedOutput.err, service, err) + } + + for k := range test.envVars { + _ = os.Unsetenv(k) + } + } +} From b9916cb6a2b2c4e9bc6a976687262db493a8ded0 Mon Sep 17 00:00:00 2001 From: David BENQUE Date: Thu, 13 Dec 2018 15:16:12 +0100 Subject: [PATCH 3/6] k8sutil: renaming+ansible scaffold Fixes #835 --- pkg/k8sutil/constants.go | 4 ++-- pkg/k8sutil/k8sutil.go | 12 ++++++------ pkg/k8sutil/k8sutil_test.go | 8 ++++---- pkg/scaffold/ansible/operator.go | 6 ++++++ 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/pkg/k8sutil/constants.go b/pkg/k8sutil/constants.go index 3f3aa9447ef..51941dedc8b 100644 --- a/pkg/k8sutil/constants.go +++ b/pkg/k8sutil/constants.go @@ -24,10 +24,10 @@ const ( // this value is empty if the operator is running with clusterScope. WatchNamespaceEnvVar = "WATCH_NAMESPACE" - // NamespaceEnvVar is the constant for env variable NAMESPACE + // OperatorNamespaceEnvVar is the constant for env variable NAMESPACE // which is the namespace that the pod is currently running in. // this value is set only if the operator is running with clusterScope - NamespaceEnvVar = "NAMESPACE" + OperatorNamespaceEnvVar = "NAMESPACE" // OperatorNameEnvVar is the constant for env variable OPERATOR_NAME // wich is the name of the current operator diff --git a/pkg/k8sutil/k8sutil.go b/pkg/k8sutil/k8sutil.go index 91befaa6f1f..ea2a2a2caa7 100644 --- a/pkg/k8sutil/k8sutil.go +++ b/pkg/k8sutil/k8sutil.go @@ -33,11 +33,11 @@ func GetWatchNamespace() (string, error) { return ns, nil } -// GetNamespace returns the namespace the operator should be running in. -func GetNamespace() (string, error) { - ns, found := os.LookupEnv(NamespaceEnvVar) +// getOperatorNamespace returns the namespace the operator should be running in. +func getOperatorNamespace() (string, error) { + ns, found := os.LookupEnv(OperatorNamespaceEnvVar) if !found { - return "", fmt.Errorf("%s must be set", NamespaceEnvVar) + return "", fmt.Errorf("%s must be set", OperatorNamespaceEnvVar) } return ns, nil } @@ -65,8 +65,8 @@ func InitOperatorService() (*v1.Service, error) { return nil, err } if namespace == "" { // This will happen for clusterScoped operators - if namespace, err = GetNamespace(); err != nil || namespace == "" { - return nil, fmt.Errorf("one of the env var %s or %s must not be empty", WatchNamespaceEnvVar, NamespaceEnvVar) + if namespace, err = getOperatorNamespace(); err != nil || namespace == "" { + return nil, fmt.Errorf("one of the env var %s or %s must not be empty", WatchNamespaceEnvVar, OperatorNamespaceEnvVar) } } service := &v1.Service{ diff --git a/pkg/k8sutil/k8sutil_test.go b/pkg/k8sutil/k8sutil_test.go index 4a21252f925..c2b98d289c6 100644 --- a/pkg/k8sutil/k8sutil_test.go +++ b/pkg/k8sutil/k8sutil_test.go @@ -129,7 +129,7 @@ func TestInitOperatorService(t *testing.T) { }, { name: "ClusterScope Case", - envVars: map[string]string{OperatorNameEnvVar: operatorName, NamespaceEnvVar: namespace, WatchNamespaceEnvVar: ""}, + envVars: map[string]string{OperatorNameEnvVar: operatorName, OperatorNamespaceEnvVar: namespace, WatchNamespaceEnvVar: ""}, expectedOutput: Output{ service: serviceExp, err: nil, @@ -140,7 +140,7 @@ func TestInitOperatorService(t *testing.T) { envVars: map[string]string{OperatorNameEnvVar: operatorName, WatchNamespaceEnvVar: ""}, expectedOutput: Output{ service: nil, - err: fmt.Errorf("one of the env var %s or %s must not be empty", WatchNamespaceEnvVar, NamespaceEnvVar), + err: fmt.Errorf("one of the env var %s or %s must not be empty", WatchNamespaceEnvVar, OperatorNamespaceEnvVar), }, }, { @@ -153,10 +153,10 @@ func TestInitOperatorService(t *testing.T) { }, { name: "Error no namespace and watchnamespace are empty", - envVars: map[string]string{OperatorNameEnvVar: operatorName, NamespaceEnvVar: "", WatchNamespaceEnvVar: ""}, + envVars: map[string]string{OperatorNameEnvVar: operatorName, OperatorNamespaceEnvVar: "", WatchNamespaceEnvVar: ""}, expectedOutput: Output{ service: nil, - err: fmt.Errorf("one of the env var %s or %s must not be empty", WatchNamespaceEnvVar, NamespaceEnvVar), + err: fmt.Errorf("one of the env var %s or %s must not be empty", WatchNamespaceEnvVar, OperatorNamespaceEnvVar), }, }, } diff --git a/pkg/scaffold/ansible/operator.go b/pkg/scaffold/ansible/operator.go index 869f67c8a09..055afb96558 100644 --- a/pkg/scaffold/ansible/operator.go +++ b/pkg/scaffold/ansible/operator.go @@ -59,6 +59,12 @@ spec: name: metrics imagePullPolicy: Always env: + {{- if .IsClusterScoped }} + - name: NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + {{- end}} - name: WATCH_NAMESPACE {{- if .IsClusterScoped }} value: "" From 59a33326045d32f367e866d8ef2d1efc3ecf476d Mon Sep 17 00:00:00 2001 From: David BENQUE Date: Thu, 13 Dec 2018 22:56:03 +0100 Subject: [PATCH 4/6] k8sutil: retrieve namespace from filesystem instead of env Fixes #835 --- pkg/k8sutil/constants.go | 5 -- pkg/k8sutil/k8sutil.go | 19 +++--- pkg/k8sutil/k8sutil_test.go | 102 ------------------------------- pkg/scaffold/ansible/operator.go | 6 -- pkg/scaffold/operator.go | 6 -- pkg/scaffold/operator_test.go | 4 -- 6 files changed, 10 insertions(+), 132 deletions(-) diff --git a/pkg/k8sutil/constants.go b/pkg/k8sutil/constants.go index 51941dedc8b..04d7968a5a9 100644 --- a/pkg/k8sutil/constants.go +++ b/pkg/k8sutil/constants.go @@ -24,11 +24,6 @@ const ( // this value is empty if the operator is running with clusterScope. WatchNamespaceEnvVar = "WATCH_NAMESPACE" - // OperatorNamespaceEnvVar is the constant for env variable NAMESPACE - // which is the namespace that the pod is currently running in. - // this value is set only if the operator is running with clusterScope - OperatorNamespaceEnvVar = "NAMESPACE" - // OperatorNameEnvVar is the constant for env variable OPERATOR_NAME // wich is the name of the current operator OperatorNameEnvVar = "OPERATOR_NAME" diff --git a/pkg/k8sutil/k8sutil.go b/pkg/k8sutil/k8sutil.go index ea2a2a2caa7..555919eeeae 100644 --- a/pkg/k8sutil/k8sutil.go +++ b/pkg/k8sutil/k8sutil.go @@ -16,7 +16,9 @@ package k8sutil import ( "fmt" + "io/ioutil" "os" + "strings" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -35,10 +37,14 @@ func GetWatchNamespace() (string, error) { // getOperatorNamespace returns the namespace the operator should be running in. func getOperatorNamespace() (string, error) { - ns, found := os.LookupEnv(OperatorNamespaceEnvVar) - if !found { - return "", fmt.Errorf("%s must be set", OperatorNamespaceEnvVar) + nsBytes, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") + if err != nil { + if os.IsNotExist(err) { + return "", fmt.Errorf("namespace not found for current environment") + } + return "", err } + ns := strings.TrimSpace(string(nsBytes)) return ns, nil } @@ -60,15 +66,10 @@ func InitOperatorService() (*v1.Service, error) { if err != nil { return nil, err } - namespace, err := GetWatchNamespace() + namespace, err := getOperatorNamespace() if err != nil { return nil, err } - if namespace == "" { // This will happen for clusterScoped operators - if namespace, err = getOperatorNamespace(); err != nil || namespace == "" { - return nil, fmt.Errorf("one of the env var %s or %s must not be empty", WatchNamespaceEnvVar, OperatorNamespaceEnvVar) - } - } service := &v1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: operatorName, diff --git a/pkg/k8sutil/k8sutil_test.go b/pkg/k8sutil/k8sutil_test.go index c2b98d289c6..5c5b71aed96 100644 --- a/pkg/k8sutil/k8sutil_test.go +++ b/pkg/k8sutil/k8sutil_test.go @@ -19,10 +19,6 @@ import ( "os" "reflect" "testing" - - v1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - intstr "k8s.io/apimachinery/pkg/util/intstr" ) func TestGetOperatorName(t *testing.T) { @@ -77,101 +73,3 @@ func TestGetOperatorName(t *testing.T) { _ = os.Unsetenv(test.envVarKey) } } - -func TestInitOperatorService(t *testing.T) { - operatorName := "myTestOperator" - namespace := "myTestNamespace" - - serviceExp := &v1.Service{ - ObjectMeta: metav1.ObjectMeta{ - Name: operatorName, - Namespace: namespace, - Labels: map[string]string{"name": operatorName}, - }, - TypeMeta: metav1.TypeMeta{ - Kind: "Service", - APIVersion: "v1", - }, - Spec: v1.ServiceSpec{ - Ports: []v1.ServicePort{ - { - Port: PrometheusMetricsPort, - Protocol: v1.ProtocolTCP, - TargetPort: intstr.IntOrString{ - Type: intstr.String, - StrVal: PrometheusMetricsPortName, - }, - Name: PrometheusMetricsPortName, - }, - }, - Selector: map[string]string{"name": operatorName}, - }, - } - - type Output struct { - service *v1.Service - err error - } - - type Scenario struct { - name string - envVars map[string]string - expectedOutput Output - } - tests := []Scenario{ - { - name: "WatchNamespace Case", - envVars: map[string]string{OperatorNameEnvVar: operatorName, WatchNamespaceEnvVar: namespace}, - expectedOutput: Output{ - service: serviceExp, - err: nil, - }, - }, - { - name: "ClusterScope Case", - envVars: map[string]string{OperatorNameEnvVar: operatorName, OperatorNamespaceEnvVar: namespace, WatchNamespaceEnvVar: ""}, - expectedOutput: Output{ - service: serviceExp, - err: nil, - }, - }, - { - name: "Error no namespace and empty watchnamespace", - envVars: map[string]string{OperatorNameEnvVar: operatorName, WatchNamespaceEnvVar: ""}, - expectedOutput: Output{ - service: nil, - err: fmt.Errorf("one of the env var %s or %s must not be empty", WatchNamespaceEnvVar, OperatorNamespaceEnvVar), - }, - }, - { - name: "Error no namespace no watchnamespace", - envVars: map[string]string{OperatorNameEnvVar: operatorName}, - expectedOutput: Output{ - service: nil, - err: fmt.Errorf("%s must be set", WatchNamespaceEnvVar), - }, - }, - { - name: "Error no namespace and watchnamespace are empty", - envVars: map[string]string{OperatorNameEnvVar: operatorName, OperatorNamespaceEnvVar: "", WatchNamespaceEnvVar: ""}, - expectedOutput: Output{ - service: nil, - err: fmt.Errorf("one of the env var %s or %s must not be empty", WatchNamespaceEnvVar, OperatorNamespaceEnvVar), - }, - }, - } - - for _, test := range tests { - for k, v := range test.envVars { - _ = os.Setenv(k, v) - } - service, err := InitOperatorService() - if !(reflect.DeepEqual(err, test.expectedOutput.err) && reflect.DeepEqual(service, test.expectedOutput.service)) { - t.Errorf("test %s failed, expected ouput: %s,%v; got: %s,%v", test.name, test.expectedOutput.service, test.expectedOutput.err, service, err) - } - - for k := range test.envVars { - _ = os.Unsetenv(k) - } - } -} diff --git a/pkg/scaffold/ansible/operator.go b/pkg/scaffold/ansible/operator.go index 055afb96558..869f67c8a09 100644 --- a/pkg/scaffold/ansible/operator.go +++ b/pkg/scaffold/ansible/operator.go @@ -59,12 +59,6 @@ spec: name: metrics imagePullPolicy: Always env: - {{- if .IsClusterScoped }} - - name: NAMESPACE - valueFrom: - fieldRef: - fieldPath: metadata.namespace - {{- end}} - name: WATCH_NAMESPACE {{- if .IsClusterScoped }} value: "" diff --git a/pkg/scaffold/operator.go b/pkg/scaffold/operator.go index 518ec4fc71d..df3cf2e6349 100644 --- a/pkg/scaffold/operator.go +++ b/pkg/scaffold/operator.go @@ -70,12 +70,6 @@ spec: periodSeconds: 10 failureThreshold: 1 env: - {{- if .IsClusterScoped }} - - name: NAMESPACE - valueFrom: - fieldRef: - fieldPath: metadata.namespace - {{- end}} - name: WATCH_NAMESPACE {{- if .IsClusterScoped }} value: "" diff --git a/pkg/scaffold/operator_test.go b/pkg/scaffold/operator_test.go index 049874cc73b..0638d95762b 100644 --- a/pkg/scaffold/operator_test.go +++ b/pkg/scaffold/operator_test.go @@ -126,10 +126,6 @@ spec: periodSeconds: 10 failureThreshold: 1 env: - - name: NAMESPACE - valueFrom: - fieldRef: - fieldPath: metadata.namespace - name: WATCH_NAMESPACE value: "" - name: POD_NAME From 5cb03de4babc49944f97fbd5860320357d93ed08 Mon Sep 17 00:00:00 2001 From: David BENQUE Date: Fri, 14 Dec 2018 23:59:13 +0100 Subject: [PATCH 5/6] k8sutil: replace leader.myNS by k8sutil.GetOperatorNamespace Fixes #835 --- pkg/k8sutil/constants.go | 2 +- pkg/k8sutil/k8sutil.go | 11 ++++++++--- pkg/leader/leader.go | 4 +++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/pkg/k8sutil/constants.go b/pkg/k8sutil/constants.go index 04d7968a5a9..52b6842cec7 100644 --- a/pkg/k8sutil/constants.go +++ b/pkg/k8sutil/constants.go @@ -20,7 +20,7 @@ const ( KubeConfigEnvVar = "KUBECONFIG" // WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE - // which is the namespace where the watch activity happens and where the pod is currently running in. + // which is the namespace where the watch activity happens. // this value is empty if the operator is running with clusterScope. WatchNamespaceEnvVar = "WATCH_NAMESPACE" diff --git a/pkg/k8sutil/k8sutil.go b/pkg/k8sutil/k8sutil.go index 555919eeeae..b5d82f12739 100644 --- a/pkg/k8sutil/k8sutil.go +++ b/pkg/k8sutil/k8sutil.go @@ -24,8 +24,11 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" intstr "k8s.io/apimachinery/pkg/util/intstr" discovery "k8s.io/client-go/discovery" + logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" ) +var log = logf.Log.WithName("k8sutil") + // GetWatchNamespace returns the namespace the operator should be watching for changes func GetWatchNamespace() (string, error) { ns, found := os.LookupEnv(WatchNamespaceEnvVar) @@ -35,16 +38,18 @@ func GetWatchNamespace() (string, error) { return ns, nil } -// getOperatorNamespace returns the namespace the operator should be running in. -func getOperatorNamespace() (string, error) { +// GetOperatorNamespace returns the namespace the operator should be running in. +func GetOperatorNamespace() (string, error) { nsBytes, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") if err != nil { if os.IsNotExist(err) { + log.V(1).Info("current namespace not found") return "", fmt.Errorf("namespace not found for current environment") } return "", err } ns := strings.TrimSpace(string(nsBytes)) + log.V(1).Info("found namespace", "Namespace", ns) return ns, nil } @@ -66,7 +71,7 @@ func InitOperatorService() (*v1.Service, error) { if err != nil { return nil, err } - namespace, err := getOperatorNamespace() + namespace, err := GetOperatorNamespace() if err != nil { return nil, err } diff --git a/pkg/leader/leader.go b/pkg/leader/leader.go index 4b977f9c028..4c8915c28ab 100644 --- a/pkg/leader/leader.go +++ b/pkg/leader/leader.go @@ -23,6 +23,8 @@ import ( "strings" "time" + "github.com/operator-framework/operator-sdk/pkg/k8sutil" + corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -54,7 +56,7 @@ const PodNameEnv = "POD_NAME" func Become(ctx context.Context, lockName string) error { log.Info("Trying to become the leader.") - ns, err := myNS() + ns, err := k8sutil.GetOperatorNamespace() if err != nil { if err == errNoNS { log.Info("Skipping leader election; not running in a cluster.") From 321a7c99a97205e4e9e1281cbb5e4b9cd51d0ff0 Mon Sep 17 00:00:00 2001 From: David BENQUE Date: Mon, 17 Dec 2018 09:44:20 +0100 Subject: [PATCH 6/6] k8sutil: remove myNs function Fixes #835 --- pkg/leader/leader.go | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/pkg/leader/leader.go b/pkg/leader/leader.go index 4c8915c28ab..38304136d03 100644 --- a/pkg/leader/leader.go +++ b/pkg/leader/leader.go @@ -18,9 +18,7 @@ import ( "context" "errors" "fmt" - "io/ioutil" "os" - "strings" "time" "github.com/operator-framework/operator-sdk/pkg/k8sutil" @@ -146,21 +144,6 @@ func Become(ctx context.Context, lockName string) error { } } -// myNS returns the name of the namespace in which this code is currently running. -func myNS() (string, error) { - nsBytes, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") - if err != nil { - if os.IsNotExist(err) { - log.V(1).Info("current namespace not found") - return "", errNoNS - } - return "", err - } - ns := strings.TrimSpace(string(nsBytes)) - log.V(1).Info("found namespace", "Namespace", ns) - return ns, nil -} - // myOwnerRef returns an OwnerReference that corresponds to the pod in which // this code is currently running. // It expects the environment variable POD_NAME to be set by the downwards API