Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixing HNS network already exist error in Stateless CNI #3297

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions network/network_windows.go
Original file line number Diff line number Diff line change
@@ -21,6 +21,11 @@ import (
"go.uber.org/zap"
)

var (
errHcnNetworkCreate = errors.New("failed to create hcn network")
errHcnNetworkGet = errors.New("failed to get hcn network")
)

const (
// HNS network types.
hnsL2bridge = "l2bridge"
@@ -360,13 +365,24 @@ func (nm *networkManager) newNetworkImplHnsV2(nwInfo *EndpointInfo, extIf *exter
if errors.As(err, &hcn.NetworkNotFoundError{}) {
logger.Info("Creating hcn network", zap.Any("hcnNetwork", hcnNetwork))
hnsResponse, err = Hnsv2.CreateNetwork(hcnNetwork)
if err != nil {
return nil, fmt.Errorf("Failed to create hcn network: %s due to error: %v", hcnNetwork.Name, err)
if err == nil {
logger.Info("Successfully created hcn network with response", zap.Any("hnsResponse", hnsResponse))
} else {
if strings.Contains(err.Error(), "already exists") {
// fetch the network name again since the parallel CNI Add call has created the HNS network
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets add a log printing err.Error() before calling get hns network

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

logger.Error("Failed to create hcn network.due to error:", zap.String("HNS Network Name", hcnNetwork.Name), zap.String("hnsResponse", err.Error()))
hnsResponse, err = Hnsv2.GetNetworkByName(hcnNetwork.Name)
if err != nil {
return nil, fmt.Errorf("%w: %s due to error %s", errHcnNetworkGet, hcnNetwork.Name, err.Error())
}
logger.Info("Successfully fetched hcn network with response", zap.Any("HNS Network ID", hnsResponse.Id), zap.String("HNS Network Name", hnsResponse.Name))
} else {
return nil, fmt.Errorf("%w: %s due to error %s", errHcnNetworkCreate, hcnNetwork.Name, err.Error())
}
}
logger.Info("Successfully created hcn network with response", zap.Any("hnsResponse", hnsResponse))
} else {
// we can't validate if the network already exists, don't continue
return nil, fmt.Errorf("Failed to create hcn network: %s, failed to query for existing network with error: %v", hcnNetwork.Name, err)
return nil, fmt.Errorf("%w: %s due to error %s", errHcnNetworkCreate, hcnNetwork.Name, err.Error())
}
} else {
if hcnNetwork.Type == hcn.Transparent {