github.com/openebs/api@v1.12.0/pkg/util/env.go (about) 1 /* 2 Copyright 2020 The OpenEBS Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "os" 21 "strings" 22 ) 23 24 const ( 25 26 // OpenEBSNamespace is the environment variable to get openebs namespace 27 // 28 // This environment variable is set via kubernetes downward API 29 OpenEBSNamespace = "OPENEBS_NAMESPACE" 30 31 // OpenEBSBaseDir is the environment variable to get base directory of 32 // openebs 33 OpenEBSBaseDir = "OPENEBS_IO_BASE_DIR" 34 35 // Namespace is the environment variable to get openebs namespace 36 // 37 // This environment variable is set via kubernetes downward API 38 Namespace = "NAMESPACE" 39 40 // DefaultOpenEBSServiceAccount name of the default openebs service accout with 41 // required permissions 42 DefaultOpenEBSServiceAccount = "openebs-maya-operator" 43 44 // OpenEBSServiceAccount is the environment variable to get operator service 45 // account name 46 // 47 // This environment variable is set via kubernetes downward API in cvc and 48 // cspc operators deployments 49 OpenEBSServiceAccount = "OPENEBS_SERVICEACCOUNT_NAME" 50 ) 51 52 // LookupOrFalse looks up an environment variable and returns a string "false" 53 // if environment variable is not present. It returns appropriate values for 54 // other cases. 55 func LookupOrFalse(envKey string) string { 56 val, present := lookupEnv(envKey) 57 if !present { 58 return "false" 59 } 60 return strings.TrimSpace(val) 61 } 62 63 // GetEnv fetches the provided environment variable's value 64 func GetEnv(envKey string) (value string) { 65 return strings.TrimSpace(os.Getenv(envKey)) 66 } 67 68 // lookupEnv looks up the provided environment variable 69 func lookupEnv(envKey string) (value string, present bool) { 70 value, present = os.LookupEnv(envKey) 71 value = strings.TrimSpace(value) 72 return 73 } 74 75 // GetOpenebsBaseDirPath returns the base path to store openebs related files on 76 // host machine 77 func GetOpenebsBaseDirPath() string { 78 baseDir, isPresent := lookupEnv(string(OpenEBSBaseDir)) 79 if !isPresent { 80 return "/var/openebs" 81 } 82 return baseDir 83 } 84 85 // GetNamespace gets the namespace OPENEBS_NAMESPACE env value which is set by the 86 // downward API where CVC-Operator has been deployed 87 func GetNamespace() string { 88 return GetEnv(OpenEBSNamespace) 89 } 90 91 // GetServiceAccountName gets the name of OPENEBS_SERVICEACCOUNT_NAME env value which is set by the 92 // downward API of cvc and cspc operator deployments 93 func GetServiceAccountName() string { 94 name, present := os.LookupEnv(OpenEBSServiceAccount) 95 if !present { 96 name = DefaultOpenEBSServiceAccount 97 } 98 return name 99 }