-
Notifications
You must be signed in to change notification settings - Fork 260
Skip network creation if network already exists in HNS #1300
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ package network | |
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
|
|
@@ -310,14 +311,27 @@ func (nm *networkManager) newNetworkImplHnsV2(nwInfo *NetworkInfo, extIf *extern | |
| return nil, err | ||
| } | ||
|
|
||
| // Create the HNS network. | ||
| log.Printf("[net] Creating hcn network: %+v", hcnNetwork) | ||
| hnsResponse, err := hnsv2.CreateNetwork(hcnNetwork) | ||
| // check if network exists, only create the network does not exist | ||
| hnsResponse, err := hnsv2.GetNetworkByName(hcnNetwork.Name) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you also log if network exists
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, added it when err is empty |
||
|
|
||
| if err != nil { | ||
| return nil, fmt.Errorf("Failed to create hcn network: %s due to error: %v", hcnNetwork.Name, err) | ||
| } | ||
| // if network not found, create the HNS network. | ||
| if errors.As(err, &hcn.NetworkNotFoundError{}) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add ut for testing these cases?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
| log.Printf("[net] Creating hcn network: %+v", 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) | ||
| } | ||
|
|
||
| log.Printf("[net] Successfully created hcn network with response: %+v", hnsResponse) | ||
| log.Printf("[net] Successfully created hcn network with response: %+v", 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) | ||
| } | ||
| } else { | ||
| log.Printf("[net] Network with name %s already exists", hcnNetwork.Name) | ||
| } | ||
|
|
||
| var vlanid int | ||
| opt, _ := nwInfo.Options[genericData].(map[string]interface{}) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just make sure cni code doesn't dereference pointer without checking for error
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the hns mock wrapper and only used by test code, no real cni code uses this type so there's no concerns.