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

     1  package network
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"net"
     7  )
     8  
     9  // TODO once the libpod.conf file stuff is worked out, this should be modified
    10  // to honor defines in the libpod.conf as well as overrides?
    11  
    12  const (
    13  	// CNIConfigDir is the path where CNI config files exist
    14  	CNIConfigDir = "/etc/cni/net.d"
    15  	// CNIDeviceName is the default network device name and in
    16  	// reality should have an int appended to it (cni-podman4)
    17  	CNIDeviceName = "cni-podman"
    18  	// DefaultPodmanDomainName is used for the dnsname plugin to define
    19  	// a localized domain name for a created network
    20  	DefaultPodmanDomainName = "dns.podman"
    21  )
    22  
    23  var (
    24  	ErrNetworkNotFound = errors.New("network not found")
    25  )
    26  
    27  // GetDefaultPodmanNetwork outputs the default network for podman
    28  func GetDefaultPodmanNetwork() (*net.IPNet, error) {
    29  	_, n, err := net.ParseCIDR("10.88.1.0/24")
    30  	return n, err
    31  }
    32  
    33  // CNIPlugins is a way of marshalling a CNI network configuration to disk
    34  type CNIPlugins interface {
    35  	Bytes() ([]byte, error)
    36  }
    37  
    38  // HostLocalBridge describes a configuration for a bridge plugin
    39  // https://github.com/containernetworking/plugins/tree/master/plugins/main/bridge#network-configuration-reference
    40  type HostLocalBridge struct {
    41  	PluginType   string            `json:"type"`
    42  	BrName       string            `json:"bridge,omitempty"`
    43  	IsGW         bool              `json:"isGateway"`
    44  	IsDefaultGW  bool              `json:"isDefaultGateway,omitempty"`
    45  	ForceAddress bool              `json:"forceAddress,omitempty"`
    46  	IPMasq       bool              `json:"ipMasq,omitempty"`
    47  	MTU          int               `json:"mtu,omitempty"`
    48  	HairpinMode  bool              `json:"hairpinMode,omitempty"`
    49  	PromiscMode  bool              `json:"promiscMode,omitempty"`
    50  	Vlan         int               `json:"vlan,omitempty"`
    51  	IPAM         IPAMHostLocalConf `json:"ipam"`
    52  }
    53  
    54  // Bytes outputs []byte
    55  func (h *HostLocalBridge) Bytes() ([]byte, error) {
    56  	return json.MarshalIndent(h, "", "\t")
    57  }
    58  
    59  // IPAMHostLocalConf describes an IPAM configuration
    60  // https://github.com/containernetworking/plugins/tree/master/plugins/ipam/host-local#network-configuration-reference
    61  type IPAMHostLocalConf struct {
    62  	PluginType  string                     `json:"type"`
    63  	Routes      []IPAMRoute                `json:"routes,omitempty"`
    64  	ResolveConf string                     `json:"resolveConf,omitempty"`
    65  	DataDir     string                     `json:"dataDir,omitempty"`
    66  	Ranges      [][]IPAMLocalHostRangeConf `json:"ranges,omitempty"`
    67  }
    68  
    69  // IPAMLocalHostRangeConf describes the new style IPAM ranges
    70  type IPAMLocalHostRangeConf struct {
    71  	Subnet     string `json:"subnet"`
    72  	RangeStart string `json:"rangeStart,omitempty"`
    73  	RangeEnd   string `json:"rangeEnd,omitempty"`
    74  	Gateway    string `json:"gateway,omitempty"`
    75  }
    76  
    77  // Bytes outputs the configuration as []byte
    78  func (i IPAMHostLocalConf) Bytes() ([]byte, error) {
    79  	return json.MarshalIndent(i, "", "\t")
    80  }
    81  
    82  // IPAMRoute describes a route in an ipam config
    83  type IPAMRoute struct {
    84  	Dest string `json:"dst"`
    85  }
    86  
    87  // PortMapConfig describes the default portmapping config
    88  type PortMapConfig struct {
    89  	PluginType   string          `json:"type"`
    90  	Capabilities map[string]bool `json:"capabilities"`
    91  }
    92  
    93  // Bytes outputs the configuration as []byte
    94  func (p PortMapConfig) Bytes() ([]byte, error) {
    95  	return json.MarshalIndent(p, "", "\t")
    96  }
    97  
    98  // IPAMDHCP describes the ipamdhcp config
    99  type IPAMDHCP struct {
   100  	DHCP string `json:"type"`
   101  }
   102  
   103  // MacVLANConfig describes the macvlan config
   104  type MacVLANConfig struct {
   105  	PluginType string   `json:"type"`
   106  	Master     string   `json:"master"`
   107  	IPAM       IPAMDHCP `json:"ipam"`
   108  }
   109  
   110  // Bytes outputs the configuration as []byte
   111  func (p MacVLANConfig) Bytes() ([]byte, error) {
   112  	return json.MarshalIndent(p, "", "\t")
   113  }
   114  
   115  // FirewallConfig describes the firewall plugin
   116  type FirewallConfig struct {
   117  	PluginType string `json:"type"`
   118  	Backend    string `json:"backend"`
   119  }
   120  
   121  // Bytes outputs the configuration as []byte
   122  func (f FirewallConfig) Bytes() ([]byte, error) {
   123  	return json.MarshalIndent(f, "", "\t")
   124  }
   125  
   126  // DNSNameConfig describes the dns container name resolution plugin config
   127  type DNSNameConfig struct {
   128  	PluginType string `json:"type"`
   129  	DomainName string `json:"domainName"`
   130  }
   131  
   132  // Bytes outputs the configuration as []byte
   133  func (d DNSNameConfig) Bytes() ([]byte, error) {
   134  	return json.MarshalIndent(d, "", "\t")
   135  }