github.com/containerd/nerdctl@v1.7.7/pkg/netutil/netutil_windows.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 netutil 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "net" 23 24 "github.com/mitchellh/mapstructure" 25 ) 26 27 const ( 28 DefaultNetworkName = "nat" 29 DefaultCIDR = "10.4.0.0/24" 30 31 // When creating non-default network without passing in `--subnet` option, 32 // nerdctl assigns subnet address for the creation starting from `StartingCIDR` 33 // This prevents subnet address overlapping with `DefaultCIDR` used by the default networkß 34 StartingCIDR = "10.4.1.0/24" 35 ) 36 37 func (n *NetworkConfig) subnets() []*net.IPNet { 38 var subnets []*net.IPNet 39 if n.Plugins[0].Network.Type == "nat" { 40 var nat natConfig 41 if err := json.Unmarshal(n.Plugins[0].Bytes, &nat); err != nil { 42 return subnets 43 } 44 var ipam windowsIpamConfig 45 if err := mapstructure.Decode(nat.IPAM, &ipam); err != nil { 46 return subnets 47 } 48 _, subnet, err := net.ParseCIDR(ipam.Subnet) 49 if err != nil { 50 return subnets 51 } 52 subnets = append(subnets, subnet) 53 } 54 return subnets 55 } 56 57 func (n *NetworkConfig) clean() error { 58 return nil 59 } 60 61 func (e *CNIEnv) generateCNIPlugins(driver string, name string, ipam map[string]interface{}, opts map[string]string, ipv6 bool) ([]CNIPlugin, error) { 62 var plugins []CNIPlugin 63 switch driver { 64 case "nat": 65 nat := newNatPlugin("Ethernet") 66 nat.IPAM = ipam 67 plugins = []CNIPlugin{nat} 68 default: 69 return nil, fmt.Errorf("unsupported cni driver %q", driver) 70 } 71 return plugins, nil 72 } 73 74 func (e *CNIEnv) generateIPAM(driver string, subnets []string, gatewayStr, ipRangeStr string, opts map[string]string, ipv6 bool) (map[string]interface{}, error) { 75 switch driver { 76 case "default": 77 default: 78 return nil, fmt.Errorf("unsupported ipam driver %q", driver) 79 } 80 81 ipamConfig := newWindowsIPAMConfig() 82 subnet, err := e.parseSubnet(subnets[0]) 83 if err != nil { 84 return nil, err 85 } 86 ipamRange, err := parseIPAMRange(subnet, gatewayStr, ipRangeStr) 87 if err != nil { 88 return nil, err 89 } 90 ipamConfig.Subnet = ipamRange.Subnet 91 ipamConfig.Routes = append(ipamConfig.Routes, IPAMRoute{Gateway: ipamRange.Gateway}) 92 ipam, err := structToMap(ipamConfig) 93 if err != nil { 94 return nil, err 95 } 96 return ipam, nil 97 } 98 99 func removeBridgeNetworkInterface(name string) error { 100 return nil 101 }