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

     1  package overlay
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net"
     8  
     9  	"github.com/Microsoft/hcsshim"
    10  	"github.com/containerd/log"
    11  	"github.com/Prakhar-Agarwal-byte/moby/libnetwork/types"
    12  )
    13  
    14  const ovPeerTable = "overlay_peer_table"
    15  
    16  func (d *driver) peerAdd(nid, eid string, peerIP net.IP, peerIPMask net.IPMask, peerMac net.HardwareAddr, vtep net.IP, updateDb bool) error {
    17  	log.G(context.TODO()).Debugf("WINOVERLAY: Enter peerAdd for ca ip %s with ca mac %s", peerIP.String(), peerMac.String())
    18  
    19  	if err := validateID(nid, eid); err != nil {
    20  		return err
    21  	}
    22  
    23  	n := d.network(nid)
    24  	if n == nil {
    25  		return nil
    26  	}
    27  
    28  	if updateDb {
    29  		log.G(context.TODO()).Info("WINOVERLAY: peerAdd: notifying HNS of the REMOTE endpoint")
    30  
    31  		hnsEndpoint := &hcsshim.HNSEndpoint{
    32  			Name:             eid,
    33  			VirtualNetwork:   n.hnsID,
    34  			MacAddress:       peerMac.String(),
    35  			IPAddress:        peerIP,
    36  			IsRemoteEndpoint: true,
    37  		}
    38  
    39  		paPolicy, err := json.Marshal(hcsshim.PaPolicy{
    40  			Type: "PA",
    41  			PA:   vtep.String(),
    42  		})
    43  		if err != nil {
    44  			return err
    45  		}
    46  
    47  		hnsEndpoint.Policies = append(hnsEndpoint.Policies, paPolicy)
    48  
    49  		configurationb, err := json.Marshal(hnsEndpoint)
    50  		if err != nil {
    51  			return err
    52  		}
    53  
    54  		// Temp: We have to create an endpoint object to keep track of the HNS ID for
    55  		// this endpoint so that we can retrieve it later when the endpoint is deleted.
    56  		// This seems unnecessary when we already have dockers EID. See if we can pass
    57  		// the global EID to HNS to use as it's ID, rather than having each HNS assign
    58  		// it's own local ID for the endpoint
    59  
    60  		addr, err := types.ParseCIDR(peerIP.String() + "/32")
    61  		if err != nil {
    62  			return err
    63  		}
    64  
    65  		n.removeEndpointWithAddress(addr)
    66  		hnsresponse, err := endpointRequest("POST", "", string(configurationb))
    67  		if err != nil {
    68  			return err
    69  		}
    70  
    71  		ep := &endpoint{
    72  			id:        eid,
    73  			nid:       nid,
    74  			addr:      addr,
    75  			mac:       peerMac,
    76  			profileID: hnsresponse.Id,
    77  			remote:    true,
    78  		}
    79  
    80  		n.addEndpoint(ep)
    81  	}
    82  
    83  	return nil
    84  }
    85  
    86  func (d *driver) peerDelete(nid, eid string, peerIP net.IP, peerIPMask net.IPMask, peerMac net.HardwareAddr, vtep net.IP, updateDb bool) error {
    87  	log.G(context.TODO()).Infof("WINOVERLAY: Enter peerDelete for endpoint %s and peer ip %s", eid, peerIP.String())
    88  
    89  	if err := validateID(nid, eid); err != nil {
    90  		return err
    91  	}
    92  
    93  	n := d.network(nid)
    94  	if n == nil {
    95  		return nil
    96  	}
    97  
    98  	ep := n.endpoint(eid)
    99  	if ep == nil {
   100  		return fmt.Errorf("could not find endpoint with id %s", eid)
   101  	}
   102  
   103  	if updateDb {
   104  		_, err := endpointRequest("DELETE", ep.profileID, "")
   105  		if err != nil {
   106  			return err
   107  		}
   108  
   109  		n.deleteEndpoint(eid)
   110  	}
   111  
   112  	return nil
   113  }