Skip to content

Commit 4193874

Browse files
committed
re-added back
1 parent 63ae218 commit 4193874

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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/restserver"
8+
)
9+
10+
type EndpointManager struct {
11+
cli releaseIPsClient // nolint
12+
}
13+
14+
type releaseIPsClient interface {
15+
ReleaseIPs(ctx context.Context, ipconfig cns.IPConfigsRequest) error
16+
GetEndpoint(ctx context.Context, endpointID string) (*restserver.GetEndpointResponse, error)
17+
}
18+
19+
func WithPlatformReleaseIPsManager(cli releaseIPsClient) *EndpointManager {
20+
return &EndpointManager{cli: cli}
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package endpointmanager
2+
3+
import (
4+
"context"
5+
6+
"github.com/Azure/azure-container-networking/cns"
7+
"github.com/pkg/errors"
8+
)
9+
10+
// ReleaseIPs implements an Interface in fsnotify for async delete of the HNS endpoint and IP addresses
11+
func (em *EndpointManager) ReleaseIPs(ctx context.Context, ipconfigreq cns.IPConfigsRequest) error {
12+
return errors.Wrap(em.cli.ReleaseIPs(ctx, ipconfigreq), "failed to release IP from CNS")
13+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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

Comments
 (0)