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