-
Notifications
You must be signed in to change notification settings - Fork 772
Expand file tree
/
Copy pathnetwork_create.go
More file actions
104 lines (92 loc) · 2.82 KB
/
network_create.go
File metadata and controls
104 lines (92 loc) · 2.82 KB
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
/*
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 main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/identifiers"
"github.com/containerd/nerdctl/pkg/lockutil"
"github.com/containerd/nerdctl/pkg/netutil"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
var networkCreateCommand = &cli.Command{
Name: "create",
Usage: "Create a network",
Description: "NOTE: To isolate CNI bridge, CNI isolation plugin needs to be installed: https://github.com/AkihiroSuda/cni-isolation",
ArgsUsage: "[flags] NETWORK",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "subnet",
Usage: "Subnet in CIDR format that represents a network segment, e.g. \"10.5.0.0/16\"",
},
},
Action: networkCreateAction,
}
func networkCreateAction(clicontext *cli.Context) error {
if clicontext.NArg() != 1 {
return errors.Errorf("requires exactly 1 argument")
}
name := clicontext.Args().First()
if err := identifiers.Validate(name); err != nil {
return errors.Wrapf(err, "malformed name %s", name)
}
netconfpath := clicontext.String("cni-netconfpath")
if err := os.MkdirAll(netconfpath, 0755); err != nil {
return err
}
fn := func() error {
e := &netutil.CNIEnv{
Path: clicontext.String("cni-path"),
NetconfPath: netconfpath,
}
ll, err := netutil.ConfigLists(e)
if err != nil {
return err
}
for _, l := range ll {
if l.Name == name {
return errors.Errorf("network with name %s already exists", name)
}
// TODO: check CIDR collision
}
id, err := netutil.AcquireNextID(ll)
if err != nil {
return err
}
subnet := clicontext.String("subnet")
if subnet == "" {
if id > 255 {
return errors.Errorf("cannot determine subnet for ID %d, specify --subnet manually", id)
}
subnet = fmt.Sprintf("10.4.%d.0/24", id)
}
l, err := netutil.GenerateConfigList(e, id, name, subnet)
if err != nil {
return err
}
filename := filepath.Join(netconfpath, "nerdctl-"+name+".conflist")
if _, err := os.Stat(filename); err == nil {
return errdefs.ErrAlreadyExists
}
if err := ioutil.WriteFile(filename, l.Bytes, 0644); err != nil {
return err
}
fmt.Fprintf(clicontext.App.Writer, "%d\n", id)
return nil
}
return lockutil.WithDirLock(netconfpath, fn)
}