Skip to content

Commit 3ea96ed

Browse files
tamilmani1989sharmasushant
authored andcommitted
Update Windows CNI implementation to free resources in case of HNS failures (#223)
Update Windows CNI implementation to free resources in case of HNS failures
1 parent 95b911c commit 3ea96ed

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

cni/network/network_windows.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package network
22

33
import (
4+
"fmt"
45
"net"
56

67
"github.com/Azure/azure-container-networking/cni"
@@ -19,7 +20,7 @@ import (
1920
* Issue link: https://github.com/kubernetes/kubernetes/issues/57253
2021
*/
2122
func handleConsecutiveAdd(containerId, endpointId string, nwInfo *network.NetworkInfo, nwCfg *cni.NetworkConfig) (*cniTypesCurr.Result, error) {
22-
hnsEndpoint, _ := hcsshim.GetHNSEndpointByName(endpointId)
23+
hnsEndpoint, err := hcsshim.GetHNSEndpointByName(endpointId)
2324
if hnsEndpoint != nil {
2425
log.Printf("[net] Found existing endpoint through hcsshim: %+v", hnsEndpoint)
2526
log.Printf("[net] Attaching ep %v to container %v", hnsEndpoint.Id, containerId)
@@ -55,7 +56,8 @@ func handleConsecutiveAdd(containerId, endpointId string, nwInfo *network.Networ
5556
return result, nil
5657
}
5758

58-
return nil, nil
59+
err = fmt.Errorf("GetHNSEndpointByName for %v returned nil with err %v", endpointId, err)
60+
return nil, err
5961
}
6062

6163
func addDefaultRoute(gwIPString string, epInfo *network.EndpointInfo, result *cniTypesCurr.Result) {

network/endpoint_windows.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func ConstructEndpointID(containerID string, netNsPath string, ifName string) (s
4747
// newEndpointImpl creates a new endpoint in the network.
4848
func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
4949
// Get Infrastructure containerID. Handle ADD calls for workload container.
50+
var err error
5051
infraEpName, _ := ConstructEndpointID(epInfo.ContainerID, epInfo.NetNsPath, epInfo.IfName)
5152

5253
hnsEndpoint := &hcsshim.HNSEndpoint{
@@ -79,11 +80,20 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
7980
return nil, err
8081
}
8182

83+
defer func() {
84+
if err != nil {
85+
log.Printf("[net] HNSEndpointRequest DELETE id:%v", hnsResponse.Id)
86+
hnsResponse, err := hcsshim.HNSEndpointRequest("DELETE", hnsResponse.Id, "")
87+
log.Printf("[net] HNSEndpointRequest DELETE response:%+v err:%v.", hnsResponse, err)
88+
}
89+
}()
90+
8291
// Attach the endpoint.
8392
log.Printf("[net] Attaching endpoint %v to container %v.", hnsResponse.Id, epInfo.ContainerID)
8493
err = hcsshim.HotAttachEndpoint(epInfo.ContainerID, hnsResponse.Id)
8594
if err != nil {
8695
log.Printf("[net] Failed to attach endpoint: %v.", err)
96+
return nil, err
8797
}
8898

8999
// Create the endpoint object.

0 commit comments

Comments
 (0)