github.com/nmstate/kubernetes-nmstate@v0.82.0/pkg/environment/environment.go (about) 1 /* 2 Copyright The Kubernetes NMState Authors. 3 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16 */ 17 18 package environment 19 20 import ( 21 "os" 22 "time" 23 24 "github.com/pkg/errors" 25 ) 26 27 // IsOperator returns true when RUN_OPERATOR env var is present 28 func IsOperator() bool { 29 _, runOperator := os.LookupEnv("RUN_OPERATOR") 30 return runOperator 31 } 32 33 // IsWebhook returns true when RUN_WEBHOOK_SERVER env var is present 34 func IsWebhook() bool { 35 _, runWebhook := os.LookupEnv("RUN_WEBHOOK_SERVER") 36 return runWebhook 37 } 38 39 // IsCertManager return true when RUN_CERT_MANAGER env var is present 40 func IsCertManager() bool { 41 _, runCertManager := os.LookupEnv("RUN_CERT_MANAGER") 42 return runCertManager 43 } 44 45 // IsCertManager return true when RUN_CERT_MANAGER env var is present 46 func IsMetricsManager() bool { 47 _, runMetricsManager := os.LookupEnv("RUN_METRICS_MANAGER") 48 return runMetricsManager 49 } 50 51 // IsHandler returns true if it's not the operator or webhook server 52 func IsHandler() bool { 53 return !IsWebhook() && !IsOperator() && !IsCertManager() && !IsMetricsManager() 54 } 55 56 // Returns node name runnig the pod 57 func NodeName() string { 58 return os.Getenv("NODE_NAME") 59 } 60 61 func LookupAsDuration(varName string) (time.Duration, error) { 62 duration := time.Duration(0) 63 varValue, ok := os.LookupEnv(varName) 64 if !ok { 65 return duration, errors.Errorf("Failed to load %s from environment", varName) 66 } 67 68 duration, err := time.ParseDuration(varValue) 69 if err != nil { 70 return duration, errors.Wrapf(err, "Failed to convert %s value to time.Duration", varName) 71 } 72 return duration, nil 73 } 74 75 func GetEnvVar(key, fallback string) string { 76 if value, ok := os.LookupEnv(key); ok { 77 return value 78 } 79 return fallback 80 }