-
Notifications
You must be signed in to change notification settings - Fork 346
/
options.go
259 lines (239 loc) · 8.25 KB
/
options.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2019 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 main
import (
"fmt"
"io/ioutil"
"net"
"time"
"github.com/spf13/pflag"
"gopkg.in/yaml.v2"
"k8s.io/klog/v2"
"antrea.io/antrea/pkg/agent/config"
"antrea.io/antrea/pkg/apis"
"antrea.io/antrea/pkg/cni"
"antrea.io/antrea/pkg/features"
"antrea.io/antrea/pkg/ovs/ovsconfig"
"antrea.io/antrea/pkg/util/flowexport"
)
const (
defaultOVSBridge = "br-int"
defaultHostGateway = "antrea-gw0"
defaultHostProcPathPrefix = "/host"
defaultServiceCIDR = "10.96.0.0/12"
defaultTunnelType = ovsconfig.GeneveTunnel
defaultFlowCollectorAddress = "flow-aggregator.flow-aggregator.svc:4739:tls"
defaultFlowCollectorTransport = "tls"
defaultFlowCollectorPort = "4739"
defaultFlowPollInterval = 5 * time.Second
defaultActiveFlowExportTimeout = 30 * time.Second
defaultIdleFlowExportTimeout = 15 * time.Second
defaultNPLPortRange = "40000-41000"
)
type Options struct {
// The path of configuration file.
configFile string
// The configuration object
config *AgentConfig
// IPFIX flow collector address
flowCollectorAddr string
// IPFIX flow collector protocol
flowCollectorProto string
// Flow exporter poll interval
pollInterval time.Duration
// Active flow timeout to export records of active flows
activeFlowTimeout time.Duration
// Idle flow timeout to export records of inactive flows
idleFlowTimeout time.Duration
}
func newOptions() *Options {
return &Options{
config: &AgentConfig{
EnablePrometheusMetrics: true,
},
}
}
// addFlags adds flags to fs and binds them to options.
func (o *Options) addFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.configFile, "config", o.configFile, "The path to the configuration file")
}
// complete completes all the required options.
func (o *Options) complete(args []string) error {
if len(o.configFile) > 0 {
if err := o.loadConfigFromFile(); err != nil {
return err
}
}
err := features.DefaultMutableFeatureGate.SetFromMap(o.config.FeatureGates)
if err != nil {
return err
}
o.setDefaults()
return nil
}
// validate validates all the required options. It must be called after complete.
func (o *Options) validate(args []string) error {
if len(args) != 0 {
return fmt.Errorf("no positional arguments are supported")
}
// Validate service CIDR configuration
_, _, err := net.ParseCIDR(o.config.ServiceCIDR)
if err != nil {
return fmt.Errorf("Service CIDR %s is invalid", o.config.ServiceCIDR)
}
if o.config.ServiceCIDRv6 != "" {
_, _, err := net.ParseCIDR(o.config.ServiceCIDRv6)
if err != nil {
return fmt.Errorf("Service CIDR v6 %s is invalid", o.config.ServiceCIDRv6)
}
}
if o.config.TunnelType != ovsconfig.VXLANTunnel && o.config.TunnelType != ovsconfig.GeneveTunnel &&
o.config.TunnelType != ovsconfig.GRETunnel && o.config.TunnelType != ovsconfig.STTTunnel {
return fmt.Errorf("tunnel type %s is invalid", o.config.TunnelType)
}
if o.config.EnableIPSecTunnel && o.config.TunnelType != ovsconfig.GRETunnel {
return fmt.Errorf("IPSec encyption is supported only for GRE tunnel")
}
if o.config.OVSDatapathType != string(ovsconfig.OVSDatapathSystem) && o.config.OVSDatapathType != string(ovsconfig.OVSDatapathNetdev) {
return fmt.Errorf("OVS datapath type %s is not supported", o.config.OVSDatapathType)
}
ok, encapMode := config.GetTrafficEncapModeFromStr(o.config.TrafficEncapMode)
if !ok {
return fmt.Errorf("TrafficEncapMode %s is unknown", o.config.TrafficEncapMode)
}
// Check if the enabled features are supported on the OS.
err = o.checkUnsupportedFeatures()
if err != nil {
return err
}
if encapMode.SupportsNoEncap() {
if !features.DefaultFeatureGate.Enabled(features.AntreaProxy) {
return fmt.Errorf("TrafficEncapMode %s requires AntreaProxy to be enabled", o.config.TrafficEncapMode)
}
if o.config.EnableIPSecTunnel {
return fmt.Errorf("IPsec tunnel may only be enabled in %s mode", config.TrafficEncapModeEncap)
}
}
if o.config.NoSNAT && !(encapMode == config.TrafficEncapModeNoEncap || encapMode == config.TrafficEncapModeNetworkPolicyOnly) {
return fmt.Errorf("noSNAT is only applicable to the %s mode", config.TrafficEncapModeNoEncap)
}
if encapMode == config.TrafficEncapModeNetworkPolicyOnly {
// In the NetworkPolicyOnly mode, Antrea will not perform SNAT
// (but SNAT can be done by the primary CNI).
o.config.NoSNAT = true
}
if err := o.validateFlowExporterConfig(); err != nil {
return fmt.Errorf("failed to validate flow exporter config: %v", err)
}
return nil
}
func (o *Options) loadConfigFromFile() error {
data, err := ioutil.ReadFile(o.configFile)
if err != nil {
return err
}
return yaml.UnmarshalStrict(data, &o.config)
}
func (o *Options) setDefaults() {
if o.config.CNISocket == "" {
o.config.CNISocket = cni.AntreaCNISocketAddr
}
if o.config.OVSBridge == "" {
o.config.OVSBridge = defaultOVSBridge
}
if o.config.OVSDatapathType == "" {
o.config.OVSDatapathType = string(ovsconfig.OVSDatapathSystem)
}
if o.config.OVSRunDir == "" {
o.config.OVSRunDir = ovsconfig.DefaultOVSRunDir
}
if o.config.HostGateway == "" {
o.config.HostGateway = defaultHostGateway
}
if o.config.TrafficEncapMode == "" {
o.config.TrafficEncapMode = config.TrafficEncapModeEncap.String()
}
if o.config.TunnelType == "" {
o.config.TunnelType = defaultTunnelType
}
if o.config.HostProcPathPrefix == "" {
o.config.HostProcPathPrefix = defaultHostProcPathPrefix
}
if o.config.ServiceCIDR == "" {
o.config.ServiceCIDR = defaultServiceCIDR
}
if o.config.APIPort == 0 {
o.config.APIPort = apis.AntreaAgentAPIPort
}
if features.DefaultFeatureGate.Enabled(features.FlowExporter) {
if o.config.FlowCollectorAddr == "" {
o.config.FlowCollectorAddr = defaultFlowCollectorAddress
}
if o.config.FlowPollInterval == "" {
o.pollInterval = defaultFlowPollInterval
}
if o.config.ActiveFlowExportTimeout == "" {
o.activeFlowTimeout = defaultActiveFlowExportTimeout
}
if o.config.IdleFlowExportTimeout == "" {
o.idleFlowTimeout = defaultIdleFlowExportTimeout
}
}
if features.DefaultFeatureGate.Enabled(features.NodePortLocal) {
if o.config.NPLPortRange == "" {
o.config.NPLPortRange = defaultNPLPortRange
}
}
}
func (o *Options) validateFlowExporterConfig() error {
if features.DefaultFeatureGate.Enabled(features.FlowExporter) {
host, port, proto, err := flowexport.ParseFlowCollectorAddr(o.config.FlowCollectorAddr, defaultFlowCollectorPort, defaultFlowCollectorTransport)
if err != nil {
return err
}
o.flowCollectorAddr = net.JoinHostPort(host, port)
o.flowCollectorProto = proto
// Parse the given flowPollInterval config
if o.config.FlowPollInterval != "" {
flowPollInterval, err := flowexport.ParseFlowIntervalString(o.config.FlowPollInterval)
if err != nil {
return err
}
o.pollInterval = flowPollInterval
}
// Parse the given activeFlowExportTimeout config
if o.config.ActiveFlowExportTimeout != "" {
o.activeFlowTimeout, err = time.ParseDuration(o.config.ActiveFlowExportTimeout)
if err != nil {
return fmt.Errorf("ActiveFlowExportTimeout is not provided in right format")
}
if o.activeFlowTimeout < o.pollInterval {
o.activeFlowTimeout = o.pollInterval
klog.Warningf("ActiveFlowExportTimeout must be greater than or equal to FlowPollInterval")
}
}
// Parse the given inactiveFlowExportTimeout config
if o.config.IdleFlowExportTimeout != "" {
o.idleFlowTimeout, err = time.ParseDuration(o.config.IdleFlowExportTimeout)
if err != nil {
return fmt.Errorf("IdleFlowExportTimeout is not provided in right format")
}
if o.idleFlowTimeout < o.pollInterval {
o.activeFlowTimeout = o.pollInterval
klog.Warningf("IdleFlowExportTimeout must be greater than or equal to FlowPollInterval")
}
}
}
return nil
}