/
config.go
76 lines (66 loc) · 2.01 KB
/
config.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
package cloud
import (
"fmt"
"os"
"strconv"
"github.com/spf13/pflag"
)
const (
defaultVpcID = ""
defaultRegion = ""
defaultAPIMaxRetries = 10
defaultAPIDebug = false
)
type Config struct {
ClusterName string
VpcID string
Region string
APIMaxRetries int
APIDebug bool
}
func (cfg *Config) BindFlags(fs *pflag.FlagSet) {
fs.StringVar(&cfg.ClusterName, "cluster-name", "", `Kubernetes cluster name (required)`)
fs.StringVar(&cfg.VpcID, "aws-vpc-id", defaultVpcID,
`AWS VPC ID for the kubernetes cluster`)
fs.StringVar(&cfg.Region, "aws-region", defaultRegion,
`AWS Region for the kubernetes cluster`)
fs.IntVar(&cfg.APIMaxRetries, "aws-max-retries", defaultAPIMaxRetries,
`Maximum number of times to retry the AWS API.`)
fs.BoolVar(&cfg.APIDebug, "aws-api-debug", defaultAPIDebug,
`Enable debug logging of AWS API`)
}
func (cfg *Config) BindEnv() error {
if len(cfg.ClusterName) == 0 {
if s, ok := os.LookupEnv("CLUSTER_NAME"); ok {
// logger.Info("Environment variable configuration is deprecated, switch to the --cluster-name flag.")
cfg.ClusterName = s
}
}
if len(cfg.VpcID) == 0 {
if s, ok := os.LookupEnv("AWS_VPC_ID"); ok {
// logger.Info("Environment variable configuration is deprecated, switch to the --aws-vpc-id flag.")
cfg.VpcID = s
}
}
if len(cfg.Region) == 0 {
if s, ok := os.LookupEnv("AWS_REGION"); ok {
// logger.Info("Environment variable configuration is deprecated, switch to the --aws-region flag.")
cfg.Region = s
}
}
if s, ok := os.LookupEnv("AWS_MAX_RETRIES"); ok {
// logger.Info("Environment variable configuration is deprecated, switch to the --aws-max-retries flag.")
v, err := strconv.ParseInt(s, 0, 32)
if err != nil {
return fmt.Errorf("AWS_MAX_RETRIES environment variable must be an integer. Value was: %s", s)
}
cfg.APIMaxRetries = int(v)
}
return nil
}
func (cfg *Config) Validate() error {
if len(cfg.ClusterName) == 0 {
return fmt.Errorf("clusterName must be specified")
}
return nil
}