github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/libnetwork/drivers/ipvlan/ipvlan_endpoint.go (about)

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