@@ -19,8 +19,8 @@ package main
19
19
import (
20
20
"fmt"
21
21
22
+ "github.com/containerd/containerd/errdefs"
22
23
"github.com/containerd/containerd/identifiers"
23
- "github.com/containerd/nerdctl/pkg/lockutil"
24
24
"github.com/containerd/nerdctl/pkg/netutil"
25
25
"github.com/containerd/nerdctl/pkg/strutil"
26
26
@@ -95,52 +95,36 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
95
95
if err != nil {
96
96
return err
97
97
}
98
+ labels = strutil .DedupeStrSlice (labels )
98
99
99
- fn := func () error {
100
- e , err := netutil .NewCNIEnv (cniPath , cniNetconfpath )
101
- if err != nil {
102
- return err
103
- }
104
- for _ , n := range e .Networks {
105
- if n .Name == name {
106
- return fmt .Errorf ("network with name %s already exists" , name )
107
- }
108
- // TODO: check CIDR collision
109
- }
110
- id , err := e .AcquireNextID ()
111
- if err != nil {
112
- return err
113
- }
114
-
115
- if subnetStr == "" {
116
- if gatewayStr != "" || ipRangeStr != "" {
117
- return fmt .Errorf ("cannot set gateway or ip-range without subnet, specify --subnet manually" )
118
- }
119
- if id > 255 {
120
- return fmt .Errorf ("cannot determine subnet for ID %d, specify --subnet manually" , id )
121
- }
122
- subnetStr = fmt .Sprintf ("10.4.%d.0/24" , id )
100
+ if subnetStr == "" {
101
+ if gatewayStr != "" || ipRangeStr != "" {
102
+ return fmt .Errorf ("cannot set gateway or ip-range without subnet, specify --subnet manually" )
123
103
}
104
+ }
124
105
125
- labels := strutil .DedupeStrSlice (labels )
126
- ipam , err := netutil .GenerateIPAM (ipamDriver , subnetStr , gatewayStr , ipRangeStr , strutil .ConvertKVStringsToMap (ipamOpts ))
127
- if err != nil {
128
- return err
129
- }
130
- cniPlugins , err := e .GenerateCNIPlugins (driver , id , name , ipam , strutil .ConvertKVStringsToMap (opts ))
131
- if err != nil {
132
- return err
133
- }
134
- net , err := e .GenerateNetworkConfig (labels , id , name , cniPlugins )
135
- if err != nil {
136
- return err
137
- }
138
- if err := e .WriteNetworkConfig (net ); err != nil {
139
- return err
106
+ e , err := netutil .NewCNIEnv (cniPath , cniNetconfpath )
107
+ if err != nil {
108
+ return err
109
+ }
110
+ createOpts := netutil.CreateOptions {
111
+ Name : name ,
112
+ Driver : driver ,
113
+ Options : strutil .ConvertKVStringsToMap (opts ),
114
+ IPAMDriver : ipamDriver ,
115
+ IPAMOptions : strutil .ConvertKVStringsToMap (ipamOpts ),
116
+ Subnet : subnetStr ,
117
+ Gateway : gatewayStr ,
118
+ IPRange : ipRangeStr ,
119
+ Labels : labels ,
120
+ }
121
+ net , err := e .CreateNetwork (createOpts )
122
+ if err != nil {
123
+ if errdefs .IsAlreadyExists (err ) {
124
+ return fmt .Errorf ("network with name %s already exists" , name )
140
125
}
141
- fmt .Fprintf (cmd .OutOrStdout (), "%d\n " , id )
142
- return nil
126
+ return err
143
127
}
144
-
145
- return lockutil . WithDirLock ( cniNetconfpath , fn )
128
+ _ , err = fmt . Fprintf ( cmd . OutOrStdout (), "%s \n " , * net . NerdctlID )
129
+ return err
146
130
}
0 commit comments