-
Notifications
You must be signed in to change notification settings - Fork 127
/
config.go
386 lines (319 loc) · 10.9 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Package config includes configuration utilities for whereabouts
package config
import (
"encoding/json"
"fmt"
"io"
"net"
"os"
"strings"
cnitypes "github.com/containernetworking/cni/pkg/types"
"github.com/imdario/mergo"
netutils "k8s.io/utils/net"
"github.com/k8snetworkplumbingwg/whereabouts/pkg/logging"
"github.com/k8snetworkplumbingwg/whereabouts/pkg/types"
)
// canonicalizeIP makes sure a provided ip is in standard form
func canonicalizeIP(ip *net.IP) error {
if ip.To4() != nil {
*ip = ip.To4()
return nil
} else if ip.To16() != nil {
*ip = ip.To16()
return nil
}
return fmt.Errorf("IP %s not v4 nor v6", *ip)
}
// LoadIPAMConfig creates IPAMConfig using json encoded configuration provided
// as `bytes`. At the moment values provided in envArgs are ignored so there
// is no possibility to overload the json configuration using envArgs
func LoadIPAMConfig(bytes []byte, envArgs string, extraConfigPaths ...string) (*types.IPAMConfig, string, error) {
var n types.Net
if err := json.Unmarshal(bytes, &n); err != nil {
return nil, "", fmt.Errorf("LoadIPAMConfig - JSON Parsing Error: %s / bytes: %s", err, bytes)
}
if n.IPAM == nil {
return nil, "", fmt.Errorf("IPAM config missing 'ipam' key")
} else if !isNetworkRelevant(n.IPAM) {
return nil, "", NewInvalidPluginError(n.IPAM.Type)
}
args := types.IPAMEnvArgs{}
if err := cnitypes.LoadArgs(envArgs, &args); err != nil {
return nil, "", fmt.Errorf("LoadArgs - CNI Args Parsing Error: %s", err)
}
n.IPAM.PodName = string(args.K8S_POD_NAME)
n.IPAM.PodNamespace = string(args.K8S_POD_NAMESPACE)
flatipam, foundflatfile, err := GetFlatIPAM(false, n.IPAM, extraConfigPaths...)
if err != nil {
return nil, "", err
}
// Now let's try to merge the configurations...
// NB: Don't try to do any initialization before this point or it won't account for merged flat file.
var OverlappingRanges bool = n.IPAM.OverlappingRanges
if err := mergo.Merge(&n, flatipam); err != nil {
logging.Errorf("Merge error with flat file: %s", err)
}
n.IPAM.OverlappingRanges = OverlappingRanges
// Logging
if n.IPAM.LogFile != "" {
logging.SetLogFile(n.IPAM.LogFile)
}
if n.IPAM.LogLevel != "" {
logging.SetLogLevel(n.IPAM.LogLevel)
}
if foundflatfile != "" {
logging.Debugf("Used defaults from parsed flat file config @ %s", foundflatfile)
}
if n.IPAM.Range != "" {
oldRange := types.RangeConfiguration{
OmitRanges: n.IPAM.OmitRanges,
Range: n.IPAM.Range,
RangeStart: n.IPAM.RangeStart,
RangeEnd: n.IPAM.RangeEnd,
}
n.IPAM.IPRanges = append([]types.RangeConfiguration{oldRange}, n.IPAM.IPRanges...)
}
for idx := range n.IPAM.IPRanges {
if r := strings.SplitN(n.IPAM.IPRanges[idx].Range, "-", 2); len(r) == 2 {
firstip := netutils.ParseIPSloppy(r[0])
if firstip == nil {
return nil, "", fmt.Errorf("invalid range start IP: %s", r[0])
}
lastip, ipNet, err := netutils.ParseCIDRSloppy(r[1])
if err != nil {
return nil, "", fmt.Errorf("invalid CIDR (do you have the 'range' parameter set for Whereabouts?) '%s': %s", r[1], err)
}
if !ipNet.Contains(firstip) {
return nil, "", fmt.Errorf("invalid range start for CIDR %s: %s", ipNet.String(), firstip)
}
n.IPAM.IPRanges[idx].Range = ipNet.String()
n.IPAM.IPRanges[idx].RangeStart = firstip
n.IPAM.IPRanges[idx].RangeEnd = lastip
} else {
firstip, ipNet, err := netutils.ParseCIDRSloppy(n.IPAM.IPRanges[idx].Range)
if err != nil {
logging.Debugf("invalid cidr error on range %v, within ranges %v", n.IPAM.IPRanges[idx].Range, n.IPAM.IPRanges)
return nil, "", fmt.Errorf("invalid CIDR %s: %s", n.IPAM.IPRanges[idx].Range, err)
}
n.IPAM.IPRanges[idx].Range = ipNet.String()
if n.IPAM.IPRanges[idx].RangeStart == nil {
firstip = netutils.ParseIPSloppy(firstip.Mask(ipNet.Mask).String()) // if range_start is not net then pick the first network address
n.IPAM.IPRanges[idx].RangeStart = firstip
}
}
}
n.IPAM.OmitRanges = nil
n.IPAM.Range = ""
n.IPAM.RangeStart = nil
n.IPAM.RangeEnd = nil
if n.IPAM.Kubernetes.KubeConfigPath == "" {
return nil, "", storageError()
}
if n.IPAM.GatewayStr != "" {
gwip := netutils.ParseIPSloppy(n.IPAM.GatewayStr)
if gwip == nil {
return nil, "", fmt.Errorf("couldn't parse gateway IP: %s", n.IPAM.GatewayStr)
}
n.IPAM.Gateway = gwip
}
for i := range n.IPAM.OmitRanges {
_, _, err := netutils.ParseCIDRSloppy(n.IPAM.OmitRanges[i])
if err != nil {
return nil, "", fmt.Errorf("invalid CIDR in exclude list %s: %s", n.IPAM.OmitRanges[i], err)
}
}
if err := configureStatic(&n, args); err != nil {
return nil, "", err
}
if n.IPAM.LeaderLeaseDuration == 0 {
n.IPAM.LeaderLeaseDuration = types.DefaultLeaderLeaseDuration
}
if n.IPAM.LeaderRenewDeadline == 0 {
n.IPAM.LeaderRenewDeadline = types.DefaultLeaderRenewDeadline
}
if n.IPAM.LeaderRetryPeriod == 0 {
n.IPAM.LeaderRetryPeriod = types.DefaultLeaderRetryPeriod
}
// Copy net name into IPAM so not to drag Net struct around
n.IPAM.Name = n.Name
return n.IPAM, n.CNIVersion, nil
}
func pathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return true
}
func configureStatic(n *types.Net, args types.IPAMEnvArgs) error {
// Validate all ranges
numV4 := 0
numV6 := 0
for i := range n.IPAM.Addresses {
ip, addr, err := netutils.ParseCIDRSloppy(n.IPAM.Addresses[i].AddressStr)
if err != nil {
return fmt.Errorf("invalid CIDR in addresses %s: %s", n.IPAM.Addresses[i].AddressStr, err)
}
n.IPAM.Addresses[i].Address = *addr
n.IPAM.Addresses[i].Address.IP = ip
if err := canonicalizeIP(&n.IPAM.Addresses[i].Address.IP); err != nil {
return fmt.Errorf("invalid address %d: %s", i, err)
}
if n.IPAM.Addresses[i].Address.IP.To4() != nil {
n.IPAM.Addresses[i].Version = "4"
numV4++
} else {
n.IPAM.Addresses[i].Version = "6"
numV6++
}
}
newnumV6, newnumV4, err := handleEnvArgs(n, numV6, numV4, args)
if err != nil {
return err
}
numV4 = newnumV4
numV6 = newnumV6
// CNI spec 0.2.0 and below supported only one v4 and v6 address
if numV4 > 1 || numV6 > 1 {
for _, v := range []string{"", "0.1.0", "0.2.0"} {
if n.CNIVersion == v {
return fmt.Errorf("CNI version %v does not support more than 1 address per family", n.CNIVersion)
}
}
}
return nil
}
func GetFlatIPAM(isControlLoop bool, IPAM *types.IPAMConfig, extraConfigPaths ...string) (types.Net, string, error) {
// Once we have our basics, let's look for our (optional) configuration file
confdirs := []string{"/etc/kubernetes/cni/net.d/whereabouts.d/whereabouts.conf", "/etc/cni/net.d/whereabouts.d/whereabouts.conf", "/host/etc/cni/net.d/whereabouts.d/whereabouts.conf"}
confdirs = append(confdirs, extraConfigPaths...)
// We prefix the optional configuration path (so we look there first)
if !isControlLoop && IPAM != nil {
if IPAM.ConfigurationPath != "" {
confdirs = append([]string{IPAM.ConfigurationPath}, confdirs...)
}
}
// Cycle through the path and parse the JSON config
flatipam := types.Net{}
foundflatfile := ""
for _, confpath := range confdirs {
if pathExists(confpath) {
jsonFile, err := os.Open(confpath)
if err != nil {
return flatipam, foundflatfile, fmt.Errorf("error opening flat configuration file @ %s with: %s", confpath, err)
}
defer jsonFile.Close()
jsonBytes, err := io.ReadAll(jsonFile)
if err != nil {
return flatipam, foundflatfile, fmt.Errorf("LoadIPAMConfig Flatfile (%s) - io.ReadAll error: %s", confpath, err)
}
if err := json.Unmarshal(jsonBytes, &flatipam.IPAM); err != nil {
return flatipam, foundflatfile, fmt.Errorf("LoadIPAMConfig Flatfile (%s) - JSON Parsing Error: %s / bytes: %s", confpath, err, jsonBytes)
}
foundflatfile = confpath
return flatipam, foundflatfile, nil
}
}
return flatipam, foundflatfile, NewConfigFileNotFoundError()
}
func handleEnvArgs(n *types.Net, numV6 int, numV4 int, args types.IPAMEnvArgs) (int, int, error) {
if args.IP != "" {
for _, item := range strings.Split(string(args.IP), ",") {
ipstr := strings.TrimSpace(item)
ip, subnet, err := netutils.ParseCIDRSloppy(ipstr)
if err != nil {
return numV6, numV4, fmt.Errorf("invalid CIDR %s: %s", ipstr, err)
}
addr := types.Address{Address: net.IPNet{IP: ip, Mask: subnet.Mask}}
if addr.Address.IP.To4() != nil {
addr.Version = "4"
numV4++
} else {
addr.Version = "6"
numV6++
}
n.IPAM.Addresses = append(n.IPAM.Addresses, addr)
}
}
if args.GATEWAY != "" {
for _, item := range strings.Split(string(args.GATEWAY), ",") {
gwip := netutils.ParseIPSloppy(strings.TrimSpace(item))
if gwip == nil {
return numV6, numV4, fmt.Errorf("invalid gateway address: %s", item)
}
for i := range n.IPAM.Addresses {
if n.IPAM.Addresses[i].Address.Contains(gwip) {
n.IPAM.Addresses[i].Gateway = gwip
}
}
}
}
return numV6, numV4, nil
}
func LoadIPAMConfiguration(bytes []byte, envArgs string, extraConfigPaths ...string) (*types.IPAMConfig, error) {
pluginConfig, err := loadPluginConfig(bytes)
if err != nil {
return nil, err
}
if pluginConfig.Type == "" {
pluginConfigList, err := loadPluginConfigList(bytes)
if err != nil {
return nil, err
}
pluginConfigList.Plugins[0].CNIVersion = pluginConfig.CNIVersion
firstPluginBytes, err := json.Marshal(pluginConfigList.Plugins[0])
if err != nil {
return nil, err
}
ipamConfig, _, err := LoadIPAMConfig(firstPluginBytes, envArgs, extraConfigPaths...)
if err != nil {
return nil, err
}
return ipamConfig, nil
}
ipamConfig, _, err := LoadIPAMConfig(bytes, envArgs, extraConfigPaths...)
if err != nil {
return nil, err
}
return ipamConfig, nil
}
func loadPluginConfigList(bytes []byte) (*types.NetConfList, error) {
var netConfList types.NetConfList
if err := json.Unmarshal(bytes, &netConfList); err != nil {
return nil, err
}
return &netConfList, nil
}
func loadPluginConfig(bytes []byte) (*cnitypes.NetConf, error) {
var pluginConfig cnitypes.NetConf
if err := json.Unmarshal(bytes, &pluginConfig); err != nil {
return nil, err
}
return &pluginConfig, nil
}
func isNetworkRelevant(ipamConfig *types.IPAMConfig) bool {
const relevantIPAMType = "whereabouts"
return ipamConfig.Type == relevantIPAMType
}
type InvalidPluginError struct {
ipamType string
}
func NewInvalidPluginError(ipamType string) *InvalidPluginError {
return &InvalidPluginError{ipamType: ipamType}
}
func (e *InvalidPluginError) Error() string {
return fmt.Sprintf("only interested in networks whose IPAM type is 'whereabouts'. This one was: %s", e.ipamType)
}
type ConfigFileNotFoundError struct{}
func NewConfigFileNotFoundError() *ConfigFileNotFoundError {
return &ConfigFileNotFoundError{}
}
func (e *ConfigFileNotFoundError) Error() string {
return "config file not found"
}
func storageError() error {
return fmt.Errorf("you have not configured the storage engine (looks like you're using an invalid `kubernetes.kubeconfig` parameter in your config)")
}