-
Notifications
You must be signed in to change notification settings - Fork 346
/
env.go
131 lines (115 loc) · 4.04 KB
/
env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2020 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package env
import (
"os"
"runtime"
"strconv"
"strings"
"k8s.io/klog/v2"
)
const (
NodeNameEnvKey = "NODE_NAME"
podNameEnvKey = "POD_NAME"
podNamespaceEnvKey = "POD_NAMESPACE"
svcAcctNameEnvKey = "SERVICEACCOUNT_NAME"
antreaConfigMapEnvKey = "ANTREA_CONFIG_MAP_NAME"
antreaCloudEKSEnvKey = "ANTREA_CLOUD_EKS"
defaultAntreaNamespace = "kube-system"
allowNoEncapWithoutAntreaProxyEnvKey = "ALLOW_NO_ENCAP_WITHOUT_ANTREA_PROXY"
)
// GetNodeName returns the node's name used in Kubernetes, based on the priority:
// - Environment variable NODE_NAME, which should be set by Downward API
// - OS's hostname
func GetNodeName() (string, error) {
nodeName := os.Getenv(NodeNameEnvKey)
if nodeName != "" {
return nodeName, nil
}
klog.Infof("Environment variable %s not found, using hostname instead", NodeNameEnvKey)
var err error
nodeName, err = os.Hostname()
if err != nil {
klog.Errorf("Failed to get local hostname: %v", err)
return "", err
}
if runtime.GOOS == "windows" {
return strings.ToLower(nodeName), nil
}
return nodeName, nil
}
// GetPodName returns name of the Pod where the code executes.
func GetPodName() string {
podName := os.Getenv(podNameEnvKey)
if podName == "" {
klog.Warningf("Environment variable %s not found", podNameEnvKey)
}
return podName
}
// GetAntreaConfigMapName returns the configMap name of Antrea config.
func GetAntreaConfigMapName() string {
configMapName := os.Getenv(antreaConfigMapEnvKey)
if configMapName == "" {
klog.Warningf("Environment variable %s not found", antreaConfigMapEnvKey)
}
return configMapName
}
// GetPodNamespace returns Namespace of the Pod where the code executes.
func GetPodNamespace() string {
podNamespace := os.Getenv(podNamespaceEnvKey)
if podNamespace == "" {
klog.Warningf("Environment variable %s not found", podNamespaceEnvKey)
}
return podNamespace
}
// GetAntreaControllerServiceAccountName returns the ServiceAccount name associated with antrea-controller.
func GetAntreaControllerServiceAccount() string {
svcAcctName := os.Getenv(svcAcctNameEnvKey)
if svcAcctName == "" {
// default value set for antrea-controller
svcAcctName = "antrea-controller"
}
return svcAcctName
}
func getBoolEnvVar(name string, defaultValue bool) bool {
if strValue := os.Getenv(name); strValue != "" {
parsedValue, err := strconv.ParseBool(strValue)
if err != nil {
klog.Errorf("Failed to parse env variable '%s' (using default '%t'): %v", name, defaultValue, err)
return defaultValue
}
return parsedValue
}
return defaultValue
}
// IsCloudEKS returns true if Antrea is used to enforce NetworkPolicies in an EKS cluster.
func IsCloudEKS() bool {
return getBoolEnvVar(antreaCloudEKSEnvKey, false)
}
// GetAntreaNamespace tries to determine the Namespace in which Antrea is running by looking at the
// POD_NAMESPACE environment variable. If this environment variable is not set (e.g. because the
// Antrea component is not run as a Pod), "kube-system" is returned.
func GetAntreaNamespace() string {
namespace := GetPodNamespace()
if namespace == "" {
klog.Warningf("Failed to get Pod Namespace from environment. Using \"%s\" as the Antrea Service Namespace", defaultAntreaNamespace)
namespace = defaultAntreaNamespace
}
return namespace
}
// GetAllowNoEncapWithoutAntreaProxy returns whether AntreaProxy can be disabled for traffic
// modes which support noEncap.
func GetAllowNoEncapWithoutAntreaProxy() bool {
return getBoolEnvVar(allowNoEncapWithoutAntreaProxyEnvKey, false)
}