Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/k8sutil/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion pkg/k8sutil/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
21 changes: 3 additions & 18 deletions pkg/leader/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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
Expand Down