@@ -43,11 +43,10 @@ var forceRunModeEnv = "OSDK_FORCE_RUN_MODE"
43
43
44
44
// errNoNamespace indicates that a namespace could not be found for the current
45
45
// environment
46
- var errNoNamespace = fmt .Errorf ("namespace not found for current environment " )
46
+ var errNoNamespace = fmt .Errorf ("Namespace is required when running outside the cluster " )
47
47
48
- // errRunLocal indicates that the operator is set to run in local mode (this error
49
- // is returned by functions that only work on operators running in cluster mode)
50
- var errRunLocal = fmt .Errorf ("operator run mode forced to local" )
48
+ // namespaceDir is the default location where namespace information is stored
49
+ var namespaceDir = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
51
50
52
51
// podNameEnvVar is the constant for env variable POD_NAME
53
52
// which is the name of the current pod.
@@ -59,23 +58,22 @@ var log = logf.Log.WithName("leader")
59
58
// attempts to become the leader.
60
59
const maxBackoffInterval = time .Second * 16
61
60
62
- // Become ensures that the current pod is the leader within its namespace. If
63
- // run outside a cluster, it will skip leader election and return nil . It
61
+ // Become ensures that the current pod is the leader within the given
62
+ // namespace. If run outside a cluster, it will return an error . It
64
63
// continuously tries to create a ConfigMap with the provided name and the
65
64
// current pod set as the owner reference. Only one can exist at a time with
66
65
// the same name, so the pod that successfully creates the ConfigMap is the
67
- // leader. Upon termination of that pod, the garbage collector will delete the
68
- // ConfigMap, enabling a different pod to become the leader.
69
- func Become (ctx context.Context , lockName string ) error {
66
+ // leader. Upon termination of that pod, the garbage collector will delete
67
+ // the ConfigMap, enabling a different pod to become the leader.
68
+ func Become (ctx context.Context , lockName string , ns string ) error {
70
69
log .Info ("Trying to become the leader." )
71
70
72
- ns , err := getOperatorNamespace ()
73
- if err != nil {
74
- if err == errNoNamespace || err == errRunLocal {
75
- log .Info ("Skipping leader election; not running in a cluster." )
76
- return nil
71
+ if ns == "" {
72
+ namespace , err := getOperatorNamespace ()
73
+ if err != nil {
74
+ return err
77
75
}
78
- return err
76
+ ns = namespace
79
77
}
80
78
81
79
config , err := config .GetConfig ()
@@ -210,10 +208,7 @@ func isPodEvicted(pod corev1.Pod) bool {
210
208
211
209
// getOperatorNamespace returns the namespace the operator should be running in.
212
210
func getOperatorNamespace () (string , error ) {
213
- if isRunModeLocal () {
214
- return "" , errRunLocal
215
- }
216
- nsBytes , err := ioutil .ReadFile ("/var/run/secrets/kubernetes.io/serviceaccount/namespace" )
211
+ nsBytes , err := ioutil .ReadFile (namespaceDir )
217
212
if err != nil {
218
213
if os .IsNotExist (err ) {
219
214
return "" , errNoNamespace
@@ -225,17 +220,10 @@ func getOperatorNamespace() (string, error) {
225
220
return ns , nil
226
221
}
227
222
228
- func isRunModeLocal () bool {
229
- return os .Getenv (forceRunModeEnv ) == string (localRunMode )
230
- }
231
-
232
223
// getPod returns a Pod object that corresponds to the pod in which the code
233
224
// is currently running.
234
225
// It expects the environment variable POD_NAME to be set by the downwards API.
235
226
func getPod (ctx context.Context , client crclient.Client , ns string ) (* corev1.Pod , error ) {
236
- if isRunModeLocal () {
237
- return nil , errRunLocal
238
- }
239
227
podName := os .Getenv (podNameEnvVar )
240
228
if podName == "" {
241
229
return nil , fmt .Errorf ("required env %s not set, please configure downward API" , podNameEnvVar )
0 commit comments