github.com/containerd/nerdctl@v1.7.7/pkg/composer/up_network.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package composer 18 19 import ( 20 "context" 21 "fmt" 22 23 "github.com/containerd/log" 24 "github.com/containerd/nerdctl/pkg/labels" 25 "github.com/containerd/nerdctl/pkg/reflectutil" 26 ) 27 28 func (c *Composer) upNetwork(ctx context.Context, shortName string) error { 29 net, ok := c.project.Networks[shortName] 30 if !ok { 31 return fmt.Errorf("invalid network name %q", shortName) 32 } 33 if net.External.External { 34 // NOP 35 return nil 36 } 37 38 if unknown := reflectutil.UnknownNonEmptyFields(&net, "Name", "Ipam", "Driver", "DriverOpts"); len(unknown) > 0 { 39 log.G(ctx).Warnf("Ignoring: network %s: %+v", shortName, unknown) 40 } 41 42 // shortName is like "default", fullName is like "compose-wordpress_default" 43 fullName := net.Name 44 netExists, err := c.NetworkExists(fullName) 45 if err != nil { 46 return err 47 } else if !netExists { 48 log.G(ctx).Infof("Creating network %s", fullName) 49 //add metadata labels to network https://github.com/compose-spec/compose-spec/blob/master/spec.md#labels-1 50 createArgs := []string{ 51 fmt.Sprintf("--label=%s=%s", labels.ComposeProject, c.project.Name), 52 fmt.Sprintf("--label=%s=%s", labels.ComposeNetwork, shortName), 53 } 54 55 if net.Driver != "" { 56 createArgs = append(createArgs, fmt.Sprintf("--driver=%s", net.Driver)) 57 } 58 59 if net.DriverOpts != nil { 60 for k, v := range net.DriverOpts { 61 createArgs = append(createArgs, fmt.Sprintf("--opt=%s=%s", k, v)) 62 } 63 } 64 65 if net.Ipam.Config != nil { 66 if len(net.Ipam.Config) > 1 { 67 log.G(ctx).Warnf("Ignoring: network %s: imam.config %+v", shortName, net.Ipam.Config[1:]) 68 } 69 70 ipamConfig := net.Ipam.Config[0] 71 if unknown := reflectutil.UnknownNonEmptyFields(ipamConfig, "Subnet", "Gateway", "IPRange"); len(unknown) > 0 { 72 log.G(ctx).Warnf("Ignoring: network %s: ipam.config[0]: %+v", shortName, unknown) 73 } 74 if ipamConfig.Subnet != "" { 75 createArgs = append(createArgs, fmt.Sprintf("--subnet=%s", ipamConfig.Subnet)) 76 } 77 if ipamConfig.Gateway != "" { 78 createArgs = append(createArgs, fmt.Sprintf("--gateway=%s", ipamConfig.Gateway)) 79 } 80 if ipamConfig.IPRange != "" { 81 createArgs = append(createArgs, fmt.Sprintf("--ip-range=%s", ipamConfig.IPRange)) 82 } 83 } 84 85 createArgs = append(createArgs, fullName) 86 87 if c.DebugPrintFull { 88 log.G(ctx).Debugf("Creating network args: %s", createArgs) 89 } 90 91 if err := c.runNerdctlCmd(ctx, append([]string{"network", "create"}, createArgs...)...); err != nil { 92 return err 93 } 94 } 95 return nil 96 }