diff --git a/pkg/k8sutil/constants.go b/pkg/k8sutil/constants.go index 0ac85c02cc2..52b6842cec7 100644 --- a/pkg/k8sutil/constants.go +++ b/pkg/k8sutil/constants.go @@ -20,7 +20,8 @@ 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. + // this value is empty if the operator is running with clusterScope. WatchNamespaceEnvVar = "WATCH_NAMESPACE" // OperatorNameEnvVar is the constant for env variable OPERATOR_NAME diff --git a/pkg/k8sutil/k8sutil.go b/pkg/k8sutil/k8sutil.go index b0d0555151e..b5d82f12739 100644 --- a/pkg/k8sutil/k8sutil.go +++ b/pkg/k8sutil/k8sutil.go @@ -16,14 +16,19 @@ package k8sutil import ( "fmt" + "io/ioutil" "os" + "strings" v1 "k8s.io/api/core/v1" 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) @@ -33,6 +38,21 @@ func GetWatchNamespace() (string, error) { return ns, nil } +// 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 +} + // GetOperatorName return the operator name func GetOperatorName() (string, error) { operatorName, found := os.LookupEnv(OperatorNameEnvVar) @@ -51,7 +71,7 @@ func InitOperatorService() (*v1.Service, error) { if err != nil { return nil, err } - namespace, err := GetWatchNamespace() + namespace, err := GetOperatorNamespace() if err != nil { return nil, err } diff --git a/pkg/leader/leader.go b/pkg/leader/leader.go index 4b977f9c028..38304136d03 100644 --- a/pkg/leader/leader.go +++ b/pkg/leader/leader.go @@ -18,11 +18,11 @@ import ( "context" "errors" "fmt" - "io/ioutil" "os" - "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 +54,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.") @@ -144,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