github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/libpod/network/netconflist.go (about) 1 package network 2 3 import ( 4 "net" 5 "os" 6 "path/filepath" 7 ) 8 9 const ( 10 defaultIPv4Route = "0.0.0.0/0" 11 defaultIPv6Route = "::/0" 12 ) 13 14 // NcList describes a generic map 15 type NcList map[string]interface{} 16 17 // NewNcList creates a generic map of values with string 18 // keys and adds in version and network name 19 func NewNcList(name, version string) NcList { 20 n := NcList{} 21 n["cniVersion"] = version 22 n["name"] = name 23 return n 24 } 25 26 // NewHostLocalBridge creates a new LocalBridge for host-local 27 func NewHostLocalBridge(name string, isGateWay, isDefaultGW, ipMasq bool, ipamConf IPAMHostLocalConf) *HostLocalBridge { 28 hostLocalBridge := HostLocalBridge{ 29 PluginType: "bridge", 30 BrName: name, 31 IPMasq: ipMasq, 32 HairpinMode: true, 33 IPAM: ipamConf, 34 } 35 if isGateWay { 36 hostLocalBridge.IsGW = true 37 } 38 if isDefaultGW { 39 hostLocalBridge.IsDefaultGW = true 40 } 41 return &hostLocalBridge 42 } 43 44 // NewIPAMHostLocalConf creates a new IPAMHostLocal configfuration 45 func NewIPAMHostLocalConf(routes []IPAMRoute, ipamRanges [][]IPAMLocalHostRangeConf) (IPAMHostLocalConf, error) { 46 ipamConf := IPAMHostLocalConf{ 47 PluginType: "host-local", 48 Routes: routes, 49 // Possible future support ? Leaving for clues 50 //ResolveConf: "", 51 //DataDir: "" 52 } 53 54 ipamConf.Ranges = ipamRanges 55 return ipamConf, nil 56 } 57 58 // NewIPAMLocalHostRange create a new IPAM range 59 func NewIPAMLocalHostRange(subnet *net.IPNet, ipRange *net.IPNet, gw net.IP) ([]IPAMLocalHostRangeConf, error) { //nolint:interfacer 60 var ranges []IPAMLocalHostRangeConf 61 hostRange := IPAMLocalHostRangeConf{ 62 Subnet: subnet.String(), 63 } 64 // an user provided a range, we add it here 65 if ipRange != nil && ipRange.IP != nil { 66 first, err := FirstIPInSubnet(ipRange) 67 if err != nil { 68 return nil, err 69 } 70 last, err := LastIPInSubnet(ipRange) 71 if err != nil { 72 return nil, err 73 } 74 hostRange.RangeStart = first.String() 75 hostRange.RangeEnd = last.String() 76 } 77 if gw != nil { 78 hostRange.Gateway = gw.String() 79 } 80 ranges = append(ranges, hostRange) 81 return ranges, nil 82 } 83 84 // NewIPAMRoute creates a new IPAM route configuration 85 func NewIPAMRoute(r *net.IPNet) IPAMRoute { //nolint:interfacer 86 return IPAMRoute{Dest: r.String()} 87 } 88 89 // NewIPAMDefaultRoute creates a new IPAMDefault route of 90 // 0.0.0.0/0 for IPv4 or ::/0 for IPv6 91 func NewIPAMDefaultRoute(isIPv6 bool) (IPAMRoute, error) { 92 route := defaultIPv4Route 93 if isIPv6 { 94 route = defaultIPv6Route 95 } 96 _, n, err := net.ParseCIDR(route) 97 if err != nil { 98 return IPAMRoute{}, err 99 } 100 return NewIPAMRoute(n), nil 101 } 102 103 // NewPortMapPlugin creates a predefined, default portmapping 104 // configuration 105 func NewPortMapPlugin() PortMapConfig { 106 caps := make(map[string]bool) 107 caps["portMappings"] = true 108 p := PortMapConfig{ 109 PluginType: "portmap", 110 Capabilities: caps, 111 } 112 return p 113 } 114 115 // NewFirewallPlugin creates a generic firewall plugin 116 func NewFirewallPlugin() FirewallConfig { 117 return FirewallConfig{ 118 PluginType: "firewall", 119 } 120 } 121 122 // NewTuningPlugin creates a generic tuning section 123 func NewTuningPlugin() TuningConfig { 124 return TuningConfig{ 125 PluginType: "tuning", 126 } 127 } 128 129 // NewDNSNamePlugin creates the dnsname config with a given 130 // domainname 131 func NewDNSNamePlugin(domainName string) DNSNameConfig { 132 caps := make(map[string]bool, 1) 133 caps["aliases"] = true 134 return DNSNameConfig{ 135 PluginType: "dnsname", 136 DomainName: domainName, 137 Capabilities: caps, 138 } 139 } 140 141 // HasDNSNamePlugin looks to see if the dnsname cni plugin is present 142 func HasDNSNamePlugin(paths []string) bool { 143 for _, p := range paths { 144 if _, err := os.Stat(filepath.Join(p, "dnsname")); err == nil { 145 return true 146 } 147 } 148 return false 149 } 150 151 // NewMacVLANPlugin creates a macvlanconfig with a given device name 152 func NewMacVLANPlugin(device string) MacVLANConfig { 153 i := IPAMDHCP{DHCP: "dhcp"} 154 155 m := MacVLANConfig{ 156 PluginType: "macvlan", 157 Master: device, 158 IPAM: i, 159 } 160 return m 161 }