github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/libnetwork/drivers/ipvlan/ipvlan_endpoint.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package ipvlan
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/docker/docker/libnetwork/driverapi"
    10  	"github.com/docker/docker/libnetwork/netlabel"
    11  	"github.com/docker/docker/libnetwork/ns"
    12  	"github.com/docker/docker/libnetwork/types"
    13  	"github.com/sirupsen/logrus"
    14  )
    15  
    16  // CreateEndpoint assigns the mac, ip and endpoint id for the new container
    17  func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
    18  	epOptions map[string]interface{}) error {
    19  
    20  	if err := validateID(nid, eid); err != nil {
    21  		return err
    22  	}
    23  	n, err := d.getNetwork(nid)
    24  	if err != nil {
    25  		return fmt.Errorf("network id %q not found", nid)
    26  	}
    27  	if ifInfo.MacAddress() != nil {
    28  		return fmt.Errorf("ipvlan interfaces do not support custom mac address assignment")
    29  	}
    30  	ep := &endpoint{
    31  		id:     eid,
    32  		nid:    nid,
    33  		addr:   ifInfo.Address(),
    34  		addrv6: ifInfo.AddressIPv6(),
    35  	}
    36  	if ep.addr == nil {
    37  		return fmt.Errorf("create endpoint was not passed an IP address")
    38  	}
    39  	// disallow port mapping -p
    40  	if opt, ok := epOptions[netlabel.PortMap]; ok {
    41  		if _, ok := opt.([]types.PortBinding); ok {
    42  			if len(opt.([]types.PortBinding)) > 0 {
    43  				logrus.Warnf("ipvlan driver does not support port mappings")
    44  			}
    45  		}
    46  	}
    47  	// disallow port exposure --expose
    48  	if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
    49  		if _, ok := opt.([]types.TransportPort); ok {
    50  			if len(opt.([]types.TransportPort)) > 0 {
    51  				logrus.Warnf("ipvlan driver does not support port exposures")
    52  			}
    53  		}
    54  	}
    55  
    56  	if err := d.storeUpdate(ep); err != nil {
    57  		return fmt.Errorf("failed to save ipvlan endpoint %.7s to store: %v", ep.id, err)
    58  	}
    59  
    60  	n.addEndpoint(ep)
    61  
    62  	return nil
    63  }
    64  
    65  // DeleteEndpoint remove the endpoint and associated netlink interface
    66  func (d *driver) DeleteEndpoint(nid, eid string) error {
    67  	if err := validateID(nid, eid); err != nil {
    68  		return err
    69  	}
    70  	n := d.network(nid)
    71  	if n == nil {
    72  		return fmt.Errorf("network id %q not found", nid)
    73  	}
    74  	ep := n.endpoint(eid)
    75  	if ep == nil {
    76  		return fmt.Errorf("endpoint id %q not found", eid)
    77  	}
    78  	if link, err := ns.NlHandle().LinkByName(ep.srcName); err == nil {
    79  		if err := ns.NlHandle().LinkDel(link); err != nil {
    80  			logrus.WithError(err).Warnf("Failed to delete interface (%s)'s link on endpoint (%s) delete", ep.srcName, ep.id)
    81  		}
    82  	}
    83  
    84  	if err := d.storeDelete(ep); err != nil {
    85  		logrus.Warnf("Failed to remove ipvlan endpoint %.7s from store: %v", ep.id, err)
    86  	}
    87  	n.deleteEndpoint(ep.id)
    88  	return nil
    89  }