github.com/jfrazelle/docker@v1.1.2-0.20210712172922-bf78e25fe508/libnetwork/drivers/ipvlan/ipvlan_endpoint.go (about)

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