/
options.go
175 lines (153 loc) · 4.79 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
// 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 (
"errors"
"fmt"
"io/ioutil"
"net"
"github.com/spf13/pflag"
"gopkg.in/yaml.v2"
netutils "k8s.io/utils/net"
"antrea.io/antrea/pkg/apis"
controllerconfig "antrea.io/antrea/pkg/config/controller"
"antrea.io/antrea/pkg/features"
)
const (
ipamIPv4MaskLo = 16
ipamIPv4MaskHi = 30
ipamIPv4MaskDefault = 24
ipamIPv6MaskLo = 64
ipamIPv6MaskHi = 126
ipamIPv6MaskDefault = 64
)
type Options struct {
// The path of configuration file.
configFile string
// The configuration object
config *controllerconfig.ControllerConfig
}
func newOptions() *Options {
return &Options{
config: &controllerconfig.ControllerConfig{},
}
}
// 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
}
}
o.setDefaults()
return features.DefaultMutableFeatureGate.SetFromMap(o.config.FeatureGates)
}
// validate validates all the required options.
func (o *Options) validate(args []string) error {
if len(args) != 0 {
return errors.New("no positional arguments are supported")
}
if o.config.NodeIPAM.EnableNodeIPAM {
err := o.validateNodeIPAMControllerOptions()
if err != nil {
return err
}
}
return nil
}
func (o *Options) validateNodeIPAMControllerOptions() error {
// Validate ClusterCIDRs
cidrs, err := netutils.ParseCIDRs(o.config.NodeIPAM.ClusterCIDRs)
if err != nil {
return fmt.Errorf("cluster CIDRs %v is invalid", o.config.NodeIPAM.ClusterCIDRs)
}
if len(cidrs) == 0 {
return fmt.Errorf("at least one cluster CIDR must be specified")
}
if len(cidrs) > 2 {
return fmt.Errorf("at most two cluster CIDRs may be specified")
}
hasIP4, hasIP6 := false, false
for _, cidr := range cidrs {
if cidr.IP.To4() == nil {
hasIP6 = true
} else {
hasIP4 = true
}
}
dualStack := hasIP4 && hasIP6
if len(cidrs) > 1 && !dualStack {
return fmt.Errorf("at most one cluster CIDR may be specified for each IP family")
}
if hasIP4 {
if o.config.NodeIPAM.NodeCIDRMaskSizeIPv4 < ipamIPv4MaskLo || o.config.NodeIPAM.NodeCIDRMaskSizeIPv4 > ipamIPv4MaskHi {
return fmt.Errorf("node IPv4 CIDR mask size %d is invalid, should be between %d and %d",
o.config.NodeIPAM.NodeCIDRMaskSizeIPv4, ipamIPv4MaskLo, ipamIPv4MaskHi)
}
}
if hasIP6 {
if o.config.NodeIPAM.NodeCIDRMaskSizeIPv6 < ipamIPv6MaskLo || o.config.NodeIPAM.NodeCIDRMaskSizeIPv6 > ipamIPv6MaskHi {
return fmt.Errorf("node IPv6 CIDR mask size %d is invalid, should be between %d and %d",
o.config.NodeIPAM.NodeCIDRMaskSizeIPv6, ipamIPv6MaskLo, ipamIPv6MaskHi)
}
}
// Validate ServiceCIDR and ServiceCIDRv6. Service CIDRs can be empty when there is no overlap with ClusterCIDR
if o.config.NodeIPAM.ServiceCIDR != "" {
_, _, err = net.ParseCIDR(o.config.NodeIPAM.ServiceCIDR)
if err != nil {
return fmt.Errorf("service CIDR %s is invalid", o.config.NodeIPAM.ServiceCIDR)
}
}
if o.config.NodeIPAM.ServiceCIDRv6 != "" {
_, _, err = net.ParseCIDR(o.config.NodeIPAM.ServiceCIDRv6)
if err != nil {
return fmt.Errorf("secondary service CIDR %s is invalid", o.config.NodeIPAM.ServiceCIDRv6)
}
}
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.APIPort == 0 {
o.config.APIPort = apis.AntreaControllerAPIPort
}
if o.config.EnablePrometheusMetrics == nil {
o.config.EnablePrometheusMetrics = new(bool)
*o.config.EnablePrometheusMetrics = true
}
if o.config.SelfSignedCert == nil {
o.config.SelfSignedCert = new(bool)
*o.config.SelfSignedCert = true
}
if o.config.LegacyCRDMirroring == nil {
o.config.LegacyCRDMirroring = new(bool)
*o.config.LegacyCRDMirroring = true
}
if o.config.NodeIPAM.NodeCIDRMaskSizeIPv4 == 0 {
o.config.NodeIPAM.NodeCIDRMaskSizeIPv4 = ipamIPv4MaskDefault
}
if o.config.NodeIPAM.NodeCIDRMaskSizeIPv6 == 0 {
o.config.NodeIPAM.NodeCIDRMaskSizeIPv6 = ipamIPv6MaskDefault
}
}