forked from aws-controllers-k8s/runtime
/
config.go
146 lines (135 loc) · 4.57 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 config
import (
"errors"
"net/url"
flag "github.com/spf13/pflag"
"go.uber.org/zap/zapcore"
ctrlrt "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
const (
flagBindPort = "bind-port"
flagEnableLeaderElection = "enable-leader-election"
flagMetricAddr = "metrics-addr"
flagEnableDevLogging = "enable-development-logging"
flagAWSAccountID = "aws-account-id"
flagAWSRegion = "aws-region"
flagAWSEndpointURL = "aws-endpoint-url"
flagLogLevel = "log-level"
flagResourceTags = "resource-tags"
flagListenNamespace = "listen-namespace"
)
// Config contains configuration otpions for ACK service controllers
type Config struct {
BindPort int
MetricsAddr string
EnableLeaderElection bool
EnableDevelopmentLogging bool
AccountID string
Region string
EndpointURL string
LogLevel string
ResourceTags []string
ListenNamespace string
}
// BindFlags defines CLI/runtime configuration options
func (cfg *Config) BindFlags() {
flag.IntVar(
&cfg.BindPort, flagBindPort,
9443,
"The port the service controller binds to.",
)
flag.StringVar(
&cfg.MetricsAddr, flagMetricAddr,
"0.0.0.0:8080",
"The address the metric endpoint binds to.",
)
flag.BoolVar(
&cfg.EnableLeaderElection, flagEnableLeaderElection,
false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.",
)
flag.BoolVar(
&cfg.EnableDevelopmentLogging, flagEnableDevLogging,
false,
"Configures the logger to use a Zap development config (encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn, no sampling), "+
"otherwise a Zap production config will be used (encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error), sampling).",
)
flag.StringVar(
&cfg.AccountID, flagAWSAccountID,
"",
"The AWS Account ID in which the service controller will create resources",
)
flag.StringVar(
&cfg.Region, flagAWSRegion,
"",
"The AWS Region in which the service controller will create its resources",
)
flag.StringVar(
&cfg.EndpointURL, flagAWSEndpointURL,
"",
"The AWS endpoint URL the service controller will use to create its resources. This is an optional" +
" flag that can be used to override the default behaviour of aws-sdk-go that constructs endpoint URLs" +
" automatically based on service and region",
)
flag.StringVar(
&cfg.LogLevel, flagLogLevel,
"info",
"The log level. Default is info. We use logr interface which only supports info and debug level",
)
flag.StringSliceVar(
&cfg.ResourceTags, flagResourceTags,
[]string{},
"Configures the ACK service controller to always set key/value pairs tags on resources that it manages.",
)
flag.StringVar(
&cfg.ListenNamespace, flagListenNamespace,
"",
"The namespace the controller will listen to.",
)
}
// SetupLogger initializes the logger used in the service controller
func (cfg *Config) SetupLogger() {
var lvl zapcore.LevelEnabler
switch cfg.LogLevel {
case "debug":
lvl = zapcore.DebugLevel
default:
lvl = zapcore.InfoLevel
}
zapOptions := zap.Options{
Development: cfg.EnableDevelopmentLogging,
Level: lvl,
}
ctrlrt.SetLogger(zap.New(zap.UseFlagOptions(&zapOptions)))
}
// Validate ensures the options are valid
func (cfg *Config) Validate() error {
if cfg.AccountID == "" {
return errors.New("unable to start service controller as account ID is nil. Please pass --aws-account-id flag")
}
if cfg.Region == "" {
return errors.New("unable to start service controller as AWS region is nil. Please pass --aws-region flag")
}
if cfg.EndpointURL != "" {
endpoint, err := url.Parse(cfg.EndpointURL)
if err != nil || endpoint.Scheme != "https" && endpoint.Host != "" {
return errors.New("invalid service endpoint. Please refer to " +
"https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html for more details")
}
}
return nil
}