-
Notifications
You must be signed in to change notification settings - Fork 260
Restore CNS State #101
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
Merged
Merged
Restore CNS State #101
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ import ( | |
| "github.com/Azure/azure-container-networking/cns/ipamclient" | ||
| "github.com/Azure/azure-container-networking/cns/routes" | ||
| "github.com/Azure/azure-container-networking/log" | ||
| "github.com/Azure/azure-container-networking/platform" | ||
| "github.com/Azure/azure-container-networking/store" | ||
| ) | ||
|
|
||
|
|
@@ -32,17 +33,24 @@ type httpRestService struct { | |
| ipamClient *ipamclient.IpamClient | ||
| routingTable *routes.RoutingTable | ||
| store store.KeyValueStore | ||
| state httpRestServiceState | ||
| state *httpRestServiceState | ||
| } | ||
|
|
||
| // httpRestServiceState contains the state we would like to persist. | ||
| type httpRestServiceState struct { | ||
| Location string | ||
| NetworkType string | ||
| Initialized bool | ||
| Networks map[string]*networkInfo | ||
| TimeStamp time.Time | ||
| } | ||
|
|
||
| type networkInfo struct { | ||
| NetworkName string | ||
| NicInfo *imdsclient.InterfaceInfo | ||
| Options map[string]interface{} | ||
| } | ||
|
|
||
| // HTTPService describes the min API interface that every service should have. | ||
| type HTTPService interface { | ||
| common.ServiceAPI | ||
|
|
@@ -67,14 +75,19 @@ func NewHTTPRestService(config *common.ServiceConfig) (HTTPService, error) { | |
| return nil, err | ||
| } | ||
|
|
||
| serviceState := &httpRestServiceState{} | ||
| serviceState.Networks = make(map[string]*networkInfo) | ||
|
|
||
| return &httpRestService{ | ||
| Service: service, | ||
| store: service.Service.Store, | ||
| dockerClient: dc, | ||
| imdsClient: imdsClient, | ||
| ipamClient: ic, | ||
| routingTable: routingTable, | ||
| state: serviceState, | ||
| }, nil | ||
|
|
||
|
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. extra line |
||
| } | ||
|
|
||
| // Start starts the CNS listener. | ||
|
|
@@ -86,6 +99,18 @@ func (service *httpRestService) Start(config *common.ServiceConfig) error { | |
| return err | ||
| } | ||
|
|
||
| err = service.restoreState() | ||
| if err != nil { | ||
| log.Printf("[Azure CNS] Failed to restore state, err:%v.", err) | ||
| return err | ||
| } | ||
|
|
||
| err = service.restoreNetworkState() | ||
| if err != nil { | ||
| log.Printf("[Azure CNS] Failed to restore state, err:%v.", err) | ||
| return err | ||
| } | ||
|
|
||
| // Add handlers. | ||
| listener := service.Listener | ||
| // default handlers | ||
|
|
@@ -186,7 +211,14 @@ func (service *httpRestService) createNetwork(w http.ResponseWriter, r *http.Req | |
| log.Printf("[Azure CNS] Unable to get routing table from node, %+v.", err.Error()) | ||
| } | ||
|
|
||
| err = dc.CreateNetwork(req.NetworkName, req.Options) | ||
| nicInfo, err := service.imdsClient.GetPrimaryInterfaceInfoFromHost() | ||
| if err != nil { | ||
| returnMessage = fmt.Sprintf("[Azure CNS] Error. CreateNetwork failed %v.", err.Error()) | ||
| returnCode = UnexpectedError | ||
| break | ||
| } | ||
|
|
||
| err = dc.CreateNetwork(req.NetworkName, nicInfo, req.Options) | ||
| if err != nil { | ||
| returnMessage = fmt.Sprintf("[Azure CNS] Error. CreateNetwork failed %v.", err.Error()) | ||
| returnCode = UnexpectedError | ||
|
|
@@ -197,6 +229,14 @@ func (service *httpRestService) createNetwork(w http.ResponseWriter, r *http.Req | |
| log.Printf("[Azure CNS] Unable to restore routing table on node, %+v.", err.Error()) | ||
| } | ||
|
|
||
| networkInfo := &networkInfo{ | ||
| NetworkName: req.NetworkName, | ||
| NicInfo: nicInfo, | ||
| Options: req.Options, | ||
| } | ||
|
|
||
| service.state.Networks[req.NetworkName] = networkInfo | ||
|
|
||
| case "StandAlone": | ||
| returnMessage = fmt.Sprintf("[Azure CNS] Error. Underlay network is not supported in StandAlone environment. %v.", err.Error()) | ||
| returnCode = UnsupportedEnvironment | ||
|
|
@@ -228,6 +268,10 @@ func (service *httpRestService) createNetwork(w http.ResponseWriter, r *http.Req | |
|
|
||
| err = service.Listener.Encode(w, &resp) | ||
|
|
||
| if returnCode == 0 { | ||
| service.saveState() | ||
| } | ||
|
|
||
| log.Response(service.Name, resp, err) | ||
| } | ||
|
|
||
|
|
@@ -279,6 +323,11 @@ func (service *httpRestService) deleteNetwork(w http.ResponseWriter, r *http.Req | |
|
|
||
| err = service.Listener.Encode(w, &resp) | ||
|
|
||
| if returnCode == 0 { | ||
| delete(service.state.Networks, req.NetworkName) | ||
| service.saveState() | ||
| } | ||
|
|
||
| log.Response(service.Name, resp, err) | ||
| } | ||
|
|
||
|
|
@@ -723,3 +772,45 @@ func (service *httpRestService) restoreState() error { | |
| log.Printf("[Azure CNS] Restored state, %+v\n", service.state) | ||
| return nil | ||
| } | ||
|
|
||
| // restoreNetworkState restores Network state that existed before reboot. | ||
| func (service *httpRestService) restoreNetworkState() error { | ||
| log.Printf("[Azure CNS] Enter Restoring Network State") | ||
|
|
||
| rebooted := false | ||
|
|
||
| modTime, err := service.store.GetModificationTime() | ||
| if err == nil { | ||
| log.Printf("[Azure CNS] Store timestamp is %v.", modTime) | ||
|
|
||
| rebootTime, err := platform.GetLastRebootTime() | ||
| if err == nil && rebootTime.After(modTime) { | ||
| log.Printf("[Azure CNS] reboot time %v mod time %v", rebootTime, modTime) | ||
| rebooted = true | ||
| } | ||
| } | ||
|
|
||
| if rebooted { | ||
| for _, nwInfo := range service.state.Networks { | ||
| enableSnat := true | ||
|
|
||
| log.Printf("[Azure CNS] Restore nwinfo %v", nwInfo) | ||
|
|
||
| if nwInfo.Options != nil { | ||
| if _, ok := nwInfo.Options[dockerclient.OptDisableSnat]; ok { | ||
| enableSnat = false | ||
| } | ||
| } | ||
|
|
||
| if enableSnat { | ||
| err := common.SetOutboundSNAT(nwInfo.NicInfo.Subnet) | ||
| if err != nil { | ||
| log.Printf("[Azure CNS] Error setting up SNAT outbound rule %v", err) | ||
| return err | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
For iptables commands, we can use this library:
https://github.com/coreos/go-iptables