github.com/rawahars/moby@v24.0.4+incompatible/libnetwork/driverapi/driverapi.go (about)

     1  package driverapi
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/docker/docker/libnetwork/discoverapi"
     7  	"github.com/docker/docker/pkg/plugingetter"
     8  )
     9  
    10  // NetworkPluginEndpointType represents the Endpoint Type used by Plugin system
    11  const NetworkPluginEndpointType = "NetworkDriver"
    12  
    13  // Driver is an interface that every plugin driver needs to implement.
    14  type Driver interface {
    15  	discoverapi.Discover
    16  
    17  	// NetworkAllocate invokes the driver method to allocate network
    18  	// specific resources passing network id and network specific config.
    19  	// It returns a key,value pair of network specific driver allocations
    20  	// to the caller.
    21  	NetworkAllocate(nid string, options map[string]string, ipV4Data, ipV6Data []IPAMData) (map[string]string, error)
    22  
    23  	// NetworkFree invokes the driver method to free network specific resources
    24  	// associated with a given network id.
    25  	NetworkFree(nid string) error
    26  
    27  	// CreateNetwork invokes the driver method to create a network
    28  	// passing the network id and network specific config. The
    29  	// config mechanism will eventually be replaced with labels
    30  	// which are yet to be introduced. The driver can return a
    31  	// list of table names for which it is interested in receiving
    32  	// notification when a CRUD operation is performed on any
    33  	// entry in that table. This will be ignored for local scope
    34  	// drivers.
    35  	CreateNetwork(nid string, options map[string]interface{}, nInfo NetworkInfo, ipV4Data, ipV6Data []IPAMData) error
    36  
    37  	// DeleteNetwork invokes the driver method to delete network passing
    38  	// the network id.
    39  	DeleteNetwork(nid string) error
    40  
    41  	// CreateEndpoint invokes the driver method to create an endpoint
    42  	// passing the network id, endpoint id endpoint information and driver
    43  	// specific config. The endpoint information can be either consumed by
    44  	// the driver or populated by the driver. The config mechanism will
    45  	// eventually be replaced with labels which are yet to be introduced.
    46  	CreateEndpoint(nid, eid string, ifInfo InterfaceInfo, options map[string]interface{}) error
    47  
    48  	// DeleteEndpoint invokes the driver method to delete an endpoint
    49  	// passing the network id and endpoint id.
    50  	DeleteEndpoint(nid, eid string) error
    51  
    52  	// EndpointOperInfo retrieves from the driver the operational data related to the specified endpoint
    53  	EndpointOperInfo(nid, eid string) (map[string]interface{}, error)
    54  
    55  	// Join method is invoked when a Sandbox is attached to an endpoint.
    56  	Join(nid, eid string, sboxKey string, jinfo JoinInfo, options map[string]interface{}) error
    57  
    58  	// Leave method is invoked when a Sandbox detaches from an endpoint.
    59  	Leave(nid, eid string) error
    60  
    61  	// ProgramExternalConnectivity invokes the driver method which does the necessary
    62  	// programming to allow the external connectivity dictated by the passed options
    63  	ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error
    64  
    65  	// RevokeExternalConnectivity asks the driver to remove any external connectivity
    66  	// programming that was done so far
    67  	RevokeExternalConnectivity(nid, eid string) error
    68  
    69  	// EventNotify notifies the driver when a CRUD operation has
    70  	// happened on a table of its interest as soon as this node
    71  	// receives such an event in the gossip layer. This method is
    72  	// only invoked for the global scope driver.
    73  	EventNotify(event EventType, nid string, tableName string, key string, value []byte)
    74  
    75  	// DecodeTableEntry passes the driver a key, value pair from table it registered
    76  	// with libnetwork. Driver should return {object ID, map[string]string} tuple.
    77  	// If DecodeTableEntry is called for a table associated with NetworkObject or
    78  	// EndpointObject the return object ID should be the network id or endpoint id
    79  	// associated with that entry. map should have information about the object that
    80  	// can be presented to the user.
    81  	// For example: overlay driver returns the VTEP IP of the host that has the endpoint
    82  	// which is shown in 'network inspect --verbose'
    83  	DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string)
    84  
    85  	// Type returns the type of this driver, the network type this driver manages
    86  	Type() string
    87  
    88  	// IsBuiltIn returns true if it is a built-in driver
    89  	IsBuiltIn() bool
    90  }
    91  
    92  // NetworkInfo provides a go interface for drivers to provide network
    93  // specific information to libnetwork.
    94  type NetworkInfo interface {
    95  	// TableEventRegister registers driver interest in a given
    96  	// table name.
    97  	TableEventRegister(tableName string, objType ObjectType) error
    98  
    99  	// UpdateIPamConfig updates the networks IPAM configuration
   100  	// based on information from the driver.  In windows, the OS (HNS) chooses
   101  	// the IP address space if the user does not specify an address space.
   102  	UpdateIpamConfig(ipV4Data []IPAMData)
   103  }
   104  
   105  // InterfaceInfo provides a go interface for drivers to retrieve
   106  // network information to interface resources.
   107  type InterfaceInfo interface {
   108  	// SetMacAddress allows the driver to set the mac address to the endpoint interface
   109  	// during the call to CreateEndpoint, if the mac address is not already set.
   110  	SetMacAddress(mac net.HardwareAddr) error
   111  
   112  	// SetIPAddress allows the driver to set the ip address to the endpoint interface
   113  	// during the call to CreateEndpoint, if the address is not already set.
   114  	// The API is to be used to assign both the IPv4 and IPv6 address types.
   115  	SetIPAddress(ip *net.IPNet) error
   116  
   117  	// MacAddress returns the MAC address.
   118  	MacAddress() net.HardwareAddr
   119  
   120  	// Address returns the IPv4 address.
   121  	Address() *net.IPNet
   122  
   123  	// AddressIPv6 returns the IPv6 address.
   124  	AddressIPv6() *net.IPNet
   125  }
   126  
   127  // InterfaceNameInfo provides a go interface for the drivers to assign names
   128  // to interfaces.
   129  type InterfaceNameInfo interface {
   130  	// SetNames method assigns the srcName and dstPrefix for the interface.
   131  	SetNames(srcName, dstPrefix string) error
   132  }
   133  
   134  // JoinInfo represents a set of resources that the driver has the ability to provide during
   135  // join time.
   136  type JoinInfo interface {
   137  	// InterfaceName returns an InterfaceNameInfo go interface to facilitate
   138  	// setting the names for the interface.
   139  	InterfaceName() InterfaceNameInfo
   140  
   141  	// SetGateway sets the default IPv4 gateway when a container joins the endpoint.
   142  	SetGateway(net.IP) error
   143  
   144  	// SetGatewayIPv6 sets the default IPv6 gateway when a container joins the endpoint.
   145  	SetGatewayIPv6(net.IP) error
   146  
   147  	// AddStaticRoute adds a route to the sandbox.
   148  	// It may be used in addition to or instead of a default gateway (as above).
   149  	AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error
   150  
   151  	// DisableGatewayService tells libnetwork not to provide Default GW for the container
   152  	DisableGatewayService()
   153  
   154  	// AddTableEntry adds a table entry to the gossip layer
   155  	// passing the table name, key and an opaque value.
   156  	AddTableEntry(tableName string, key string, value []byte) error
   157  }
   158  
   159  // Registerer provides a way for network drivers to be dynamically registered.
   160  type Registerer interface {
   161  	RegisterDriver(name string, driver Driver, capability Capability) error
   162  }
   163  
   164  // DriverCallback provides a Callback interface for Drivers into LibNetwork
   165  type DriverCallback interface {
   166  	Registerer
   167  	// GetPluginGetter returns the pluginv2 getter.
   168  	GetPluginGetter() plugingetter.PluginGetter
   169  }
   170  
   171  // Capability represents the high level capabilities of the drivers which libnetwork can make use of
   172  type Capability struct {
   173  	DataScope         string
   174  	ConnectivityScope string
   175  }
   176  
   177  // IPAMData represents the per-network ip related
   178  // operational information libnetwork will send
   179  // to the network driver during CreateNetwork()
   180  type IPAMData struct {
   181  	AddressSpace string
   182  	Pool         *net.IPNet
   183  	Gateway      *net.IPNet
   184  	AuxAddresses map[string]*net.IPNet
   185  }
   186  
   187  // EventType defines a type for the CRUD event
   188  type EventType uint8
   189  
   190  const (
   191  	// Create event is generated when a table entry is created,
   192  	Create EventType = 1 + iota
   193  	// Update event is generated when a table entry is updated.
   194  	Update
   195  	// Delete event is generated when a table entry is deleted.
   196  	Delete
   197  )
   198  
   199  // ObjectType represents the type of object driver wants to store in libnetwork's networkDB
   200  type ObjectType int
   201  
   202  const (
   203  	// EndpointObject should be set for libnetwork endpoint object related data
   204  	EndpointObject ObjectType = 1 + iota
   205  	// NetworkObject should be set for libnetwork network object related data
   206  	NetworkObject
   207  	// OpaqueObject is for driver specific data with no corresponding libnetwork object
   208  	OpaqueObject
   209  )
   210  
   211  // IsValidType validates the passed in type against the valid object types
   212  func IsValidType(objType ObjectType) bool {
   213  	switch objType {
   214  	case EndpointObject:
   215  		fallthrough
   216  	case NetworkObject:
   217  		fallthrough
   218  	case OpaqueObject:
   219  		return true
   220  	}
   221  	return false
   222  }