-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathnetutil.go
673 lines (603 loc) · 17.9 KB
/
netutil.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
Copyright The containerd 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 netutil
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/containernetworking/cni/libcni"
containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/errdefs"
"github.com/containerd/log"
"github.com/containerd/nerdctl/v2/pkg/api/types"
"github.com/containerd/nerdctl/v2/pkg/labels"
"github.com/containerd/nerdctl/v2/pkg/lockutil"
"github.com/containerd/nerdctl/v2/pkg/netutil/nettype"
subnetutil "github.com/containerd/nerdctl/v2/pkg/netutil/subnet"
"github.com/containerd/nerdctl/v2/pkg/strutil"
)
type CNIEnv struct {
Path string
NetconfPath string
Namespace string
}
type CNIEnvOpt func(e *CNIEnv) error
func (e *CNIEnv) ListNetworksMatch(reqs []string, allowPseudoNetwork bool) (list map[string][]*NetworkConfig, errs []error) {
var err error
var networkConfigs []*NetworkConfig
err = lockutil.WithDirLock(e.NetconfPath, func() error {
networkConfigs, err = e.networkConfigList()
return err
})
if err != nil {
return nil, []error{err}
}
list = make(map[string][]*NetworkConfig)
for _, req := range reqs {
if !allowPseudoNetwork && (req == "host" || req == "none") {
errs = append(errs, fmt.Errorf("pseudo network not allowed: %s", req))
continue
}
result := []*NetworkConfig{}
// First match by name
for _, networkConfig := range networkConfigs {
if networkConfig.Name == req {
result = append(result, networkConfig)
}
}
// If nothing, try to match the id
if len(result) == 0 {
for _, networkConfig := range networkConfigs {
if networkConfig.NerdctlID != nil {
if len(req) <= len((*networkConfig.NerdctlID)) && (*networkConfig.NerdctlID)[0:len(req)] == req {
result = append(result, networkConfig)
}
}
}
}
list[req] = result
}
return list, errs
}
func UsedNetworks(ctx context.Context, client *containerd.Client) (map[string][]string, error) {
nsService := client.NamespaceService()
nsList, err := nsService.List(ctx)
if err != nil {
return nil, err
}
used := make(map[string][]string)
for _, ns := range nsList {
nsCtx := namespaces.WithNamespace(ctx, ns)
containers, err := client.Containers(nsCtx)
if err != nil {
return nil, err
}
nsUsedN, err := namespaceUsedNetworks(nsCtx, containers)
if err != nil {
return nil, err
}
// merge
for k, v := range nsUsedN {
if value, ok := used[k]; ok {
used[k] = append(value, v...)
} else {
used[k] = v
}
}
}
return used, nil
}
func namespaceUsedNetworks(ctx context.Context, containers []containerd.Container) (map[string][]string, error) {
used := make(map[string][]string)
for _, c := range containers {
// Only tasks under the ctx namespace can be obtained here
task, err := c.Task(ctx, nil)
if err != nil {
if errdefs.IsNotFound(err) {
log.G(ctx).Debugf("task not found - likely container %q was removed", c.ID())
continue
}
return nil, err
}
status, err := task.Status(ctx)
if err != nil {
if errdefs.IsNotFound(err) {
log.G(ctx).Debugf("task not found - likely container %q was removed", c.ID())
continue
}
return nil, err
}
switch status.Status {
case containerd.Paused, containerd.Running:
default:
continue
}
l, err := c.Labels(ctx)
if err != nil {
if errdefs.IsNotFound(err) {
log.G(ctx).Debugf("container %q is gone", c.ID())
continue
}
return nil, err
}
networkJSON, ok := l[labels.Networks]
if !ok {
continue
}
var networks []string
if err := json.Unmarshal([]byte(networkJSON), &networks); err != nil {
return nil, err
}
netType, err := nettype.Detect(networks)
if err != nil {
return nil, err
}
if netType != nettype.CNI {
continue
}
for _, n := range networks {
used[n] = append(used[n], c.ID())
}
}
return used, nil
}
func WithDefaultNetwork(bridgeIP string) CNIEnvOpt {
return func(e *CNIEnv) error {
return e.ensureDefaultNetworkConfig(bridgeIP)
}
}
func WithNamespace(namespace string) CNIEnvOpt {
return func(e *CNIEnv) error {
if err := os.MkdirAll(filepath.Join(e.NetconfPath, namespace), 0755); err != nil {
return err
}
e.Namespace = namespace
return nil
}
}
func NewCNIEnv(cniPath, cniConfPath string, opts ...CNIEnvOpt) (*CNIEnv, error) {
e := CNIEnv{
Path: cniPath,
NetconfPath: cniConfPath,
}
if err := os.MkdirAll(e.NetconfPath, 0755); err != nil {
return nil, err
}
for _, o := range opts {
if err := o(&e); err != nil {
return nil, err
}
}
return &e, nil
}
func (e *CNIEnv) NetworkList() ([]*NetworkConfig, error) {
var netConfigList []*NetworkConfig
var err error
fn := func() error {
netConfigList, err = e.networkConfigList()
return err
}
err = lockutil.WithDirLock(e.NetconfPath, fn)
return netConfigList, err
}
func (e *CNIEnv) NetworkMap() (map[string]*NetworkConfig, error) { //nolint:revive
networks, err := e.networkConfigList()
if err != nil {
return nil, err
}
m := make(map[string]*NetworkConfig, len(networks))
for _, n := range networks {
if original, exists := m[n.Name]; exists {
log.L.Warnf("duplicate network name %q, %#v will get superseded by %#v", n.Name, original, n)
}
m[n.Name] = n
}
return m, nil
}
func (e *CNIEnv) NetworkByNameOrID(key string) (*NetworkConfig, error) {
networks, err := e.networkConfigList()
if err != nil {
return nil, err
}
for _, n := range networks {
if n.Name == key {
return n, nil
}
if n.NerdctlID != nil && (*n.NerdctlID == key || (*n.NerdctlID)[0:12] == key) {
return n, nil
}
}
return nil, fmt.Errorf("no such network: %q", key)
}
func (e *CNIEnv) filterNetworks(filterf func(*NetworkConfig) bool) ([]*NetworkConfig, error) {
networkConfigs, err := e.networkConfigList()
if err != nil {
return nil, err
}
result := []*NetworkConfig{}
for _, networkConfig := range networkConfigs {
if filterf(networkConfig) {
result = append(result, networkConfig)
}
}
return result, nil
}
func (e *CNIEnv) getConfigPathForNetworkName(netName string) string {
if netName == DefaultNetworkName || e.Namespace == "" {
return filepath.Join(e.NetconfPath, "nerdctl-"+netName+".conflist")
}
return filepath.Join(e.NetconfPath, e.Namespace, "nerdctl-"+netName+".conflist")
}
func (e *CNIEnv) usedSubnets() ([]*net.IPNet, error) {
usedSubnets, err := subnetutil.GetLiveNetworkSubnets()
if err != nil {
return nil, err
}
networkConfigs, err := e.networkConfigList()
if err != nil {
return nil, err
}
for _, netConf := range networkConfigs {
usedSubnets = append(usedSubnets, netConf.subnets()...)
}
return usedSubnets, nil
}
type NetworkConfig struct {
*libcni.NetworkConfigList
NerdctlID *string
NerdctlLabels *map[string]string
File string
}
type cniNetworkConfig struct {
CNIVersion string `json:"cniVersion"`
Name string `json:"name"`
ID string `json:"nerdctlID"`
Labels map[string]string `json:"nerdctlLabels"`
Plugins []CNIPlugin `json:"plugins"`
}
func (e *CNIEnv) CreateNetwork(opts types.NetworkCreateOptions) (*NetworkConfig, error) { //nolint:revive
var netConf *NetworkConfig
fn := func() error {
netMap, err := e.NetworkMap()
if err != nil {
return err
}
if _, ok := netMap[opts.Name]; ok {
return errdefs.ErrAlreadyExists
}
ipam, err := e.generateIPAM(opts.IPAMDriver, opts.Subnets, opts.Gateway, opts.IPRange, opts.IPAMOptions, opts.IPv6)
if err != nil {
return err
}
plugins, err := e.generateCNIPlugins(opts.Driver, opts.Name, ipam, opts.Options, opts.IPv6)
if err != nil {
return err
}
netConf, err = e.generateNetworkConfig(opts.Name, opts.Labels, plugins)
if err != nil {
return err
}
return e.writeNetworkConfig(netConf)
}
err := lockutil.WithDirLock(e.NetconfPath, fn)
if err != nil {
return nil, err
}
return netConf, nil
}
func (e *CNIEnv) RemoveNetwork(net *NetworkConfig) error {
fn := func() error {
if err := os.RemoveAll(net.File); err != nil {
return err
}
return net.clean()
}
return lockutil.WithDirLock(e.NetconfPath, fn)
}
// GetDefaultNetworkConfig checks whether the default network exists
// by first searching for if any network bears the `labels.NerdctlDefaultNetwork`
// label, or falls back to checking whether any network bears the
// `DefaultNetworkName` name.
func (e *CNIEnv) GetDefaultNetworkConfig() (*NetworkConfig, error) {
// Search for networks bearing the `labels.NerdctlDefaultNetwork` label.
defaultLabelFilterF := func(nc *NetworkConfig) bool {
if nc.NerdctlLabels == nil {
return false
} else if _, ok := (*nc.NerdctlLabels)[labels.NerdctlDefaultNetwork]; ok {
return true
}
return false
}
labelMatches, err := e.filterNetworks(defaultLabelFilterF)
if err != nil {
return nil, err
}
if len(labelMatches) >= 1 {
if len(labelMatches) > 1 {
log.L.Warnf("returning the first network bearing the %q label out of the multiple found: %#v", labels.NerdctlDefaultNetwork, labelMatches)
}
return labelMatches[0], nil
}
// Search for networks bearing the DefaultNetworkName.
defaultNameFilterF := func(nc *NetworkConfig) bool {
return nc.Name == DefaultNetworkName
}
nameMatches, err := e.filterNetworks(defaultNameFilterF)
if err != nil {
return nil, err
}
if len(nameMatches) >= 1 {
if len(nameMatches) > 1 {
log.L.Warnf("returning the first network bearing the %q default network name out of the multiple found: %#v", DefaultNetworkName, nameMatches)
}
// Warn the user if the default network was not created by nerdctl.
match := nameMatches[0]
_, statErr := os.Stat(e.getConfigPathForNetworkName(DefaultNetworkName))
if match.NerdctlID == nil || statErr != nil {
log.L.Warnf("default network named %q does not have an internal nerdctl ID or nerdctl-managed config file, it was most likely NOT created by nerdctl", DefaultNetworkName)
}
return nameMatches[0], nil
}
return nil, nil
}
func (e *CNIEnv) ensureDefaultNetworkConfig(bridgeIP string) error {
defaultNet, err := e.GetDefaultNetworkConfig()
if err != nil {
return fmt.Errorf("failed to check for default network: %w", err)
}
if defaultNet == nil {
if err := e.createDefaultNetworkConfig(bridgeIP); err != nil {
return fmt.Errorf("failed to create default network: %w", err)
}
}
return nil
}
func (e *CNIEnv) createDefaultNetworkConfig(bridgeIP string) error {
filename := e.getConfigPathForNetworkName(DefaultNetworkName)
if _, err := os.Stat(filename); err == nil {
return fmt.Errorf("already found existing network config at %q, cannot create new network named %q", filename, DefaultNetworkName)
}
bridgeCIDR := DefaultCIDR
bridgeGatewayIP := ""
if bridgeIP != "" {
bIP, bCIDR, err := net.ParseCIDR(bridgeIP)
if err != nil {
return fmt.Errorf("invalid bridge ip %s: %w", bridgeIP, err)
}
bridgeGatewayIP = bIP.String()
bridgeCIDR = bCIDR.String()
}
opts := types.NetworkCreateOptions{
Name: DefaultNetworkName,
Driver: DefaultNetworkName,
Subnets: []string{bridgeCIDR},
Gateway: bridgeGatewayIP,
IPAMDriver: "default",
Labels: []string{fmt.Sprintf("%s=true", labels.NerdctlDefaultNetwork)},
}
_, err := e.CreateNetwork(opts)
if err != nil && !errdefs.IsAlreadyExists(err) {
return err
}
return nil
}
// generateNetworkConfig creates NetworkConfig.
// generateNetworkConfig does not fill "File" field.
func (e *CNIEnv) generateNetworkConfig(name string, labels []string, plugins []CNIPlugin) (*NetworkConfig, error) {
if name == "" || len(plugins) == 0 {
return nil, errdefs.ErrInvalidArgument
}
for _, f := range plugins {
p := filepath.Join(e.Path, f.GetPluginType())
if _, err := exec.LookPath(p); err != nil {
return nil, fmt.Errorf("needs CNI plugin %q to be installed in CNI_PATH (%q), see https://github.com/containernetworking/plugins/releases: %w", f.GetPluginType(), e.Path, err)
}
}
id := networkID(name)
labelsMap := strutil.ConvertKVStringsToMap(labels)
conf := &cniNetworkConfig{
CNIVersion: "1.0.0",
Name: name,
ID: id,
Labels: labelsMap,
Plugins: plugins,
}
confJSON, err := json.MarshalIndent(conf, "", " ")
if err != nil {
return nil, err
}
l, err := libcni.ConfListFromBytes(confJSON)
if err != nil {
return nil, err
}
return &NetworkConfig{
NetworkConfigList: l,
NerdctlID: &id,
NerdctlLabels: &labelsMap,
File: "",
}, nil
}
// writeNetworkConfig writes NetworkConfig file to cni config path.
func (e *CNIEnv) writeNetworkConfig(net *NetworkConfig) error {
filename := e.getConfigPathForNetworkName(net.Name)
if _, err := os.Stat(filename); err == nil {
return errdefs.ErrAlreadyExists
}
return os.WriteFile(filename, net.Bytes, 0644)
}
// networkConfigList loads config from dir if dir exists.
func (e *CNIEnv) networkConfigList() ([]*NetworkConfig, error) {
common, err := libcni.ConfFiles(e.NetconfPath, []string{".conf", ".conflist", ".json"})
if err != nil {
return nil, err
}
namespaced := []string{}
if e.Namespace != "" {
namespaced, err = libcni.ConfFiles(filepath.Join(e.NetconfPath, e.Namespace), []string{".conf", ".conflist", ".json"})
if err != nil {
return nil, err
}
}
return cniLoad(append(common, namespaced...))
}
func wrapCNIError(fileName string, err error) error {
return fmt.Errorf("failed marshalling json out of network configuration file %q: %w\n"+
"For details on the schema, see https://pkg.go.dev/github.com/containernetworking/cni/libcni#NetworkConfigList", fileName, err)
}
func cniLoad(fileNames []string) (configList []*NetworkConfig, err error) {
var fileName string
sort.Strings(fileNames)
for _, fileName = range fileNames {
var bytes []byte
bytes, err = os.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("error reading %s: %w", fileName, err)
}
var netConfigList *libcni.NetworkConfigList
if strings.HasSuffix(fileName, ".conflist") {
netConfigList, err = libcni.ConfListFromBytes(bytes)
if err != nil {
return nil, wrapCNIError(fileName, err)
}
} else {
var netConfig *libcni.NetworkConfig
netConfig, err = libcni.ConfFromBytes(bytes)
if err != nil {
return nil, wrapCNIError(fileName, err)
}
netConfigList, err = libcni.ConfListFromConf(netConfig)
if err != nil {
return nil, wrapCNIError(fileName, err)
}
}
id, nerdctlLabels := nerdctlIDLabels(netConfigList.Bytes)
configList = append(configList, &NetworkConfig{
NetworkConfigList: netConfigList,
NerdctlID: id,
NerdctlLabels: nerdctlLabels,
File: fileName,
})
}
return configList, nil
}
func nerdctlIDLabels(b []byte) (*string, *map[string]string) {
type idLabels struct {
ID *string `json:"nerdctlID,omitempty"`
Labels *map[string]string `json:"nerdctlLabels,omitempty"`
}
var idl idLabels
if err := json.Unmarshal(b, &idl); err != nil {
return nil, nil
}
return idl.ID, idl.Labels
}
func networkID(name string) string {
hash := sha256.Sum256([]byte(name))
return hex.EncodeToString(hash[:])
}
func (e *CNIEnv) parseSubnet(subnetStr string) (*net.IPNet, error) {
usedSubnets, err := e.usedSubnets()
if err != nil {
return nil, err
}
if subnetStr == "" {
_, defaultSubnet, _ := net.ParseCIDR(StartingCIDR)
subnet, err := subnetutil.GetFreeSubnet(defaultSubnet, usedSubnets)
if err != nil {
return nil, err
}
return subnet, nil
}
subnetIP, subnet, err := net.ParseCIDR(subnetStr)
if err != nil {
return nil, fmt.Errorf("failed to parse subnet %q", subnetStr)
}
if !subnet.IP.Equal(subnetIP) {
return nil, fmt.Errorf("unexpected subnet %q, maybe you meant %q?", subnetStr, subnet.String())
}
if subnetutil.IntersectsWithNetworks(subnet, usedSubnets) {
return nil, fmt.Errorf("subnet %s overlaps with other one on this address space", subnetStr)
}
return subnet, nil
}
func parseIPAMRange(subnet *net.IPNet, gatewayStr, ipRangeStr string) (*IPAMRange, error) {
var gateway, rangeStart, rangeEnd net.IP
if gatewayStr != "" {
gatewayIP := net.ParseIP(gatewayStr)
if gatewayIP == nil {
return nil, fmt.Errorf("failed to parse gateway %q", gatewayStr)
}
if !subnet.Contains(gatewayIP) {
return nil, fmt.Errorf("no matching subnet %q for gateway %q", subnet, gatewayStr)
}
gateway = gatewayIP
} else {
gateway, _ = subnetutil.FirstIPInSubnet(subnet)
}
res := &IPAMRange{
Subnet: subnet.String(),
Gateway: gateway.String(),
}
if ipRangeStr != "" {
_, ipRange, err := net.ParseCIDR(ipRangeStr)
if err != nil {
return nil, fmt.Errorf("failed to parse ip-range %q", ipRangeStr)
}
rangeStart, _ = subnetutil.FirstIPInSubnet(ipRange)
rangeEnd, _ = subnetutil.LastIPInSubnet(ipRange)
if !subnet.Contains(rangeStart) || !subnet.Contains(rangeEnd) {
return nil, fmt.Errorf("no matching subnet %q for ip-range %q", subnet, ipRangeStr)
}
res.RangeStart = rangeStart.String()
res.RangeEnd = rangeEnd.String()
res.IPRange = ipRangeStr
}
return res, nil
}
// convert the struct to a map
func structToMap(in interface{}) (map[string]interface{}, error) {
out := make(map[string]interface{})
data, err := json.Marshal(in)
if err != nil {
return nil, err
}
if err := json.Unmarshal(data, &out); err != nil {
return nil, err
}
return out, nil
}
// ParseMTU parses the mtu option
// nolint:unused
func parseMTU(mtu string) (int, error) {
if mtu == "" {
return 0, nil // default
}
m, err := strconv.Atoi(mtu)
if err != nil {
return 0, err
}
if m < 0 {
return 0, fmt.Errorf("mtu %d is less than zero", m)
}
return m, nil
}