@@ -14,6 +14,7 @@ import (
1414 "github.com/Azure/azure-container-networking/network/policy"
1515 "github.com/Microsoft/hcsshim"
1616 "github.com/Microsoft/hcsshim/hcn"
17+ "github.com/pkg/errors"
1718)
1819
1920const (
@@ -75,6 +76,9 @@ const (
7576
7677 // signals a APIPA endpoint type
7778 apipaEndpointType = "APIPA"
79+
80+ // default network name used by HNS
81+ defaultNetworkName = "azure"
7882)
7983
8084// Named Lock for network and endpoint creation/deletion
@@ -685,3 +689,68 @@ func DeleteHostNCApipaEndpoint(
685689
686690 return nil
687691}
692+
693+ // DeleteHNSEndpointbyID deletes the HNS endpoint
694+ func DeleteHNSEndpointbyID (hnsEndpointID string ) error {
695+ var (
696+ hcnEndpoint * hcn.HostComputeEndpoint
697+ err error
698+ )
699+
700+ logger .Printf ("Deleting hcn endpoint with id %v" , hnsEndpointID )
701+ hcnEndpoint , err = hcn .GetEndpointByID (hnsEndpointID )
702+ if err != nil {
703+ // If error is anything other than EndpointNotFoundError, return error.
704+ // else log the error but don't return error because endpoint is already deleted.
705+ var notFoundErr hcn.EndpointNotFoundError
706+ if errors .As (err , & notFoundErr ) {
707+ return fmt .Errorf ("Failed to get hcn endpoint with id: %s due to err: %w" , hnsEndpointID , err )
708+ }
709+
710+ logger .Errorf ("Delete called on the Endpoint which doesn't exist. Error:%v" , err )
711+ return nil
712+ }
713+
714+ // Remove this endpoint from the namespace
715+ if err = hcn .RemoveNamespaceEndpoint (hcnEndpoint .HostComputeNamespace , hcnEndpoint .Id ); err != nil {
716+ logger .Errorf ("Failed to remove hcn endpoint %s from namespace %s due to err: %v" , hcnEndpoint .Id , hcnEndpoint .HostComputeNamespace , err )
717+ }
718+
719+ if err = hcnEndpoint .Delete (); err != nil {
720+ return fmt .Errorf ("Failed to delete endpoint: %s. Error: %w" , hnsEndpointID , err )
721+ }
722+
723+ logger .Errorf ("[Azure CNS] Successfully deleted endpoint: %+v" , hnsEndpointID )
724+
725+ return nil
726+ }
727+
728+ // GetHNSEndpointbyIP returns an HNSEndpoint with the corrsponding HNS Endpoint ID that matches an specific IP Address.
729+ func GetHNSEndpointbyIP (ipv4 , ipv6 []net.IPNet ) (string , error ) {
730+ logger .Printf ("Fetching missing HNS endpoint id for endpoints in network with id %s" , defaultNetworkName )
731+ hnsResponse , err := hcn .GetNetworkByName (defaultNetworkName )
732+ if err != nil || hnsResponse == nil {
733+ return "" , errors .Wrapf (err , "HNS Network or endpoints not found" )
734+ }
735+ hcnEndpoints , err := hcn .ListEndpointsOfNetwork (hnsResponse .Id )
736+ if err != nil {
737+ return "" , errors .Wrapf (err , "failed to fetch HNS endpoints for the given network" )
738+ }
739+ for i := range hcnEndpoints {
740+ for _ , ipConfiguration := range hcnEndpoints [i ].IpConfigurations {
741+ for _ , ip := range ipv4 {
742+ if ipConfiguration .IpAddress == ip .IP .String () {
743+ logger .Printf ("Successfully found hcn endpoint id for endpoint %s with ip %s" , hcnEndpoints [i ].Id , ip .IP .String ())
744+ return hcnEndpoints [i ].Id , nil
745+ }
746+ }
747+ for _ , ip := range ipv6 {
748+ if ipConfiguration .IpAddress == ip .IP .String () {
749+ logger .Printf ("Successfully found hcn endpoint id for endpoint %s with ip %s" , hcnEndpoints [i ].Id , ip .IP .String ())
750+ return hcnEndpoints [i ].Id , nil
751+ }
752+ }
753+ }
754+ }
755+ return "" , errors .Wrapf (err , "No HNSEndpointID matches the IPAddress" )
756+ }
0 commit comments