github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/network/netconflist.go (about)

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