/
args.go
53 lines (43 loc) · 1.33 KB
/
args.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
package agent
import (
"fmt"
"os"
)
// EnvArgs stores the static configuration data assigned to the autoscaler agent by its
// environment
type EnvArgs struct {
// ConfigPath gives the path to read static configuration from. It is taken from the CONFIG_PATH
// environment variable.
ConfigPath string
// K8sNodeName is the Kubernetes node the autoscaler agent is running on. It is taken from the
// K8S_NODE_NAME environment variable, which is set equal to the pod's Spec.NodeName.
//
// The Kubernetes documentation doesn't say this, but the NodeName is always populated with the
// final node the pod was placed on by the time the environment variables are set.
K8sNodeName string
// K8sPodIP is the IP address of the Kubernetes pod that this autoscaler-agent is running in
K8sPodIP string
}
func getEnvVar(err *error, require_nonempty bool, varName string) string {
if *err != nil {
return ""
}
s := os.Getenv(varName)
if s == "" && require_nonempty {
*err = fmt.Errorf("Missing %s in environment", varName)
}
return s
}
func ArgsFromEnv() (EnvArgs, error) {
var err error
args := EnvArgs{
ConfigPath: getEnvVar(&err, true, "CONFIG_PATH"),
K8sNodeName: getEnvVar(&err, true, "K8S_NODE_NAME"),
K8sPodIP: getEnvVar(&err, true, "K8S_POD_IP"),
}
if err != nil {
return EnvArgs{}, err
} else {
return args, err
}
}