|
| 1 | +package endpointmanager |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/Azure/azure-container-networking/cns" |
| 7 | + "github.com/Azure/azure-container-networking/cns/hnsclient" |
| 8 | + "github.com/Azure/azure-container-networking/cns/logger" |
| 9 | + "github.com/pkg/errors" |
| 10 | +) |
| 11 | + |
| 12 | +// ReleaseIPs implements an Interface in fsnotify for async delete of the HNS endpoint and IP addresses |
| 13 | +func (em *EndpointManager) ReleaseIPs(ctx context.Context, ipconfigreq cns.IPConfigsRequest) error { |
| 14 | + logger.Printf("deleting HNS Endpoint asynchronously") |
| 15 | + // remove HNS endpoint |
| 16 | + if err := em.deleteEndpoint(ctx, ipconfigreq.InfraContainerID); err != nil { |
| 17 | + logger.Errorf("failed to remove HNS endpoint %s", err.Error()) |
| 18 | + } |
| 19 | + return errors.Wrap(em.cli.ReleaseIPs(ctx, ipconfigreq), "failed to release IP from CNS") |
| 20 | +} |
| 21 | + |
| 22 | +// deleteEndpoint API to get the state and then remove assiciated HNS |
| 23 | +func (em *EndpointManager) deleteEndpoint(ctx context.Context, containerid string) error { |
| 24 | + endpointResponse, err := em.cli.GetEndpoint(ctx, containerid) |
| 25 | + if err != nil { |
| 26 | + return errors.Wrap(err, "failed to read the endpoint from CNS state") |
| 27 | + } |
| 28 | + for _, ipInfo := range endpointResponse.EndpointInfo.IfnameToIPMap { |
| 29 | + hnsEndpointID := ipInfo.HnsEndpointID |
| 30 | + // we need to get the HNSENdpoint via the IP address if the HNSEndpointID is not present in the statefile |
| 31 | + if ipInfo.HnsEndpointID == "" { |
| 32 | + if hnsEndpointID, err = hnsclient.GetHNSEndpointbyIP(ipInfo.IPv4, ipInfo.IPv6); err != nil { |
| 33 | + return errors.Wrap(err, "failed to find HNS endpoint with id") |
| 34 | + } |
| 35 | + } |
| 36 | + logger.Printf("deleting HNS Endpoint with id %v", hnsEndpointID) |
| 37 | + if err := hnsclient.DeleteHNSEndpointbyID(hnsEndpointID); err != nil { |
| 38 | + return errors.Wrap(err, "failed to delete HNS endpoint with id "+ipInfo.HnsEndpointID) |
| 39 | + } |
| 40 | + } |
| 41 | + return nil |
| 42 | +} |
0 commit comments