-
Notifications
You must be signed in to change notification settings - Fork 260
Add option in CNS to pre-provision hns network #323
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
8cea83f
ef256d8
7915b89
6ebc467
254f821
afea655
93c809f
9933429
c0241bc
d85f56f
b386a64
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 |
|---|---|---|
|
|
@@ -154,6 +154,8 @@ func (service *HTTPRestService) Start(config *common.ServiceConfig) error { | |
| listener.AddHandler(cns.GetNetworkContainerByOrchestratorContext, service.getNetworkContainerByOrchestratorContext) | ||
| listener.AddHandler(cns.AttachContainerToNetwork, service.attachNetworkContainerToNetwork) | ||
| listener.AddHandler(cns.DetachContainerFromNetwork, service.detachNetworkContainerFromNetwork) | ||
| listener.AddHandler(cns.CreateHnsNetworkPath, service.createHnsNetwork) | ||
| listener.AddHandler(cns.DeleteHnsNetworkPath, service.deleteHnsNetwork) | ||
|
|
||
| // handlers for v0.2 | ||
| listener.AddHandler(cns.V2Prefix+cns.SetEnvironmentPath, service.setEnvironment) | ||
|
|
@@ -172,6 +174,8 @@ func (service *HTTPRestService) Start(config *common.ServiceConfig) error { | |
| listener.AddHandler(cns.V2Prefix+cns.GetNetworkContainerByOrchestratorContext, service.getNetworkContainerByOrchestratorContext) | ||
| listener.AddHandler(cns.V2Prefix+cns.AttachContainerToNetwork, service.attachNetworkContainerToNetwork) | ||
| listener.AddHandler(cns.V2Prefix+cns.DetachContainerFromNetwork, service.detachNetworkContainerFromNetwork) | ||
| listener.AddHandler(cns.V2Prefix+cns.CreateHnsNetworkPath, service.createHnsNetwork) | ||
| listener.AddHandler(cns.V2Prefix+cns.DeleteHnsNetworkPath, service.deleteHnsNetwork) | ||
|
|
||
| log.Printf("[Azure CNS] Listening.") | ||
| return nil | ||
|
|
@@ -248,7 +252,7 @@ func (service *HTTPRestService) createNetwork(w http.ResponseWriter, r *http.Req | |
| case "Underlay": | ||
| switch service.state.Location { | ||
| case "Azure": | ||
| log.Printf("[Azure CNS] Goign to create network with name %v.", req.NetworkName) | ||
| log.Printf("[Azure CNS] Creating network with name %v.", req.NetworkName) | ||
|
|
||
| err = rt.GetRoutingTable() | ||
| if err != nil { | ||
|
|
@@ -344,7 +348,7 @@ func (service *HTTPRestService) deleteNetwork(w http.ResponseWriter, r *http.Req | |
|
|
||
| // Network does exist | ||
| if err == nil { | ||
| log.Printf("[Azure CNS] Goign to delete network with name %v.", req.NetworkName) | ||
| log.Printf("[Azure CNS] Deleting network with name %v.", req.NetworkName) | ||
| err := dc.DeleteNetwork(req.NetworkName) | ||
| if err != nil { | ||
| returnMessage = fmt.Sprintf("[Azure CNS] Error. DeleteNetwork failed %v.", err.Error()) | ||
|
|
@@ -379,6 +383,115 @@ func (service *HTTPRestService) deleteNetwork(w http.ResponseWriter, r *http.Req | |
| log.Response(service.Name, resp, resp.ReturnCode, ReturnCodeToString(resp.ReturnCode), err) | ||
| } | ||
|
|
||
| // Handles CreateHnsNetwork requests. | ||
| func (service *HTTPRestService) createHnsNetwork(w http.ResponseWriter, r *http.Request) { | ||
| log.Printf("[Azure CNS] createHnsNetwork") | ||
|
|
||
| var err error | ||
| returnCode := 0 | ||
| returnMessage := "" | ||
|
|
||
| var req cns.CreateHnsNetworkRequest | ||
| err = service.Listener.Decode(w, r, &req) | ||
| log.Request(service.Name, &req, err) | ||
|
|
||
| if err != nil { | ||
| returnMessage = fmt.Sprintf("[Azure CNS] Error. Unable to decode input request.") | ||
ashvindeodhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| returnCode = InvalidParameter | ||
| } else { | ||
| switch r.Method { | ||
ashvindeodhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case "POST": | ||
| if err := platform.CreateHnsNetwork(req); err == nil { | ||
| // Save the newly created HnsNetwork name. CNS deleteHnsNetwork API | ||
| // will only allow deleting these networks. | ||
| networkInfo := &networkInfo{ | ||
| NetworkName: req.NetworkName, | ||
| } | ||
| service.lock.Lock() | ||
| service.state.Networks[req.NetworkName] = networkInfo | ||
ashvindeodhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| service.lock.Unlock() | ||
| returnMessage = fmt.Sprintf("[Azure CNS] Successfully created HNS network: %s", req.NetworkName) | ||
| } else { | ||
| returnMessage = fmt.Sprintf("[Azure CNS] CreateHnsNetwork failed with error %v", err.Error()) | ||
| returnCode = UnexpectedError | ||
| } | ||
| default: | ||
| returnMessage = "[Azure CNS] Error. CreateHnsNetwork did not receive a POST." | ||
| returnCode = InvalidParameter | ||
| } | ||
| } | ||
|
|
||
| resp := &cns.Response{ | ||
| ReturnCode: returnCode, | ||
| Message: returnMessage, | ||
| } | ||
|
|
||
| err = service.Listener.Encode(w, &resp) | ||
|
|
||
ashvindeodhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if returnCode == 0 { | ||
| service.lock.Lock() | ||
| service.saveState() | ||
ashvindeodhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| service.lock.Unlock() | ||
| } | ||
|
|
||
| log.Response(service.Name, resp, resp.ReturnCode, ReturnCodeToString(resp.ReturnCode), err) | ||
| } | ||
|
|
||
| // Handles deleteHnsNetwork requests. | ||
| func (service *HTTPRestService) deleteHnsNetwork(w http.ResponseWriter, r *http.Request) { | ||
| log.Printf("[Azure CNS] deleteHnsNetwork") | ||
|
|
||
| var err error | ||
| var req cns.DeleteHnsNetworkRequest | ||
| returnCode := 0 | ||
| returnMessage := "" | ||
|
|
||
| err = service.Listener.Decode(w, r, &req) | ||
| log.Request(service.Name, &req, err) | ||
|
|
||
| if err != nil { | ||
| returnMessage = fmt.Sprintf("[Azure CNS] Error. Unable to decode input request.") | ||
| returnCode = InvalidParameter | ||
ashvindeodhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| switch r.Method { | ||
| case "POST": | ||
| service.lock.Lock() | ||
| networkInfo, ok := service.state.Networks[req.NetworkName] | ||
|
Contributor
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. I will encapsulate this in a function, so that people don't forget to acquire lock.
Member
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. Keeping it as is. Don't understand the comment. I don't think moving the networkName validity check warrants a new func if that's what you are asking for. |
||
| if ok && networkInfo.NetworkName == req.NetworkName { | ||
| if err = platform.DeleteHnsNetwork(req.NetworkName); err == nil { | ||
| returnMessage = fmt.Sprintf("[Azure CNS] Successfully deleted HNS network: %s", req.NetworkName) | ||
| } else { | ||
| returnMessage = fmt.Sprintf("[Azure CNS] DeleteHnsNetwork failed with error %v", err.Error()) | ||
| returnCode = UnexpectedError | ||
| } | ||
| } else { | ||
| returnMessage = fmt.Sprintf("[Azure CNS] Network %s not found", req.NetworkName) | ||
| returnCode = InvalidParameter | ||
ashvindeodhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| service.lock.Unlock() | ||
| default: | ||
| returnMessage = "[Azure CNS] Error. DeleteHnsNetwork did not receive a POST." | ||
| returnCode = InvalidParameter | ||
| } | ||
| } | ||
|
|
||
| resp := &cns.Response{ | ||
| ReturnCode: returnCode, | ||
| Message: returnMessage, | ||
| } | ||
|
|
||
| err = service.Listener.Encode(w, &resp) | ||
|
|
||
| if returnCode == 0 { | ||
| service.lock.Lock() | ||
| delete(service.state.Networks, req.NetworkName) | ||
| service.saveState() | ||
| service.lock.Unlock() | ||
ashvindeodhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| log.Response(service.Name, resp, resp.ReturnCode, ReturnCodeToString(resp.ReturnCode), err) | ||
| } | ||
|
|
||
| // Handles ip reservation requests. | ||
| func (service *HTTPRestService) reserveIPAddress(w http.ResponseWriter, r *http.Request) { | ||
| log.Printf("[Azure CNS] reserveIPAddress") | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.