-
Notifications
You must be signed in to change notification settings - Fork 260
Add a go routine to update NC host version from NMAgent periodically. #714
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
37d4948
d9c9b95
0d2e3cd
19afda8
8dc5632
3e32105
88748fe
9ee26fd
7b4d200
02f2800
d494cf6
2f2cc80
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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright 2020 Microsoft. All rights reserved. | ||
| // MIT License | ||
|
|
||
| package fakes | ||
|
|
||
| // NMAgentClientTest can be used to query to VM Host info. | ||
| type NMAgentClientTest struct { | ||
| } | ||
|
|
||
| // NewFakeNMAgentClient return a mock implemetation of NMAgentClient | ||
| func NewFakeNMAgentClient() *NMAgentClientTest { | ||
| return &NMAgentClientTest{} | ||
| } | ||
|
|
||
| // GetNcVersionListWithOutToken is mock implementation to return nc version list. | ||
| func (nmagentclient *NMAgentClientTest) GetNcVersionListWithOutToken(ncNeedUpdateList []string) map[string]int { | ||
| ncVersionList := make(map[string]int) | ||
| for _, ncID := range ncNeedUpdateList { | ||
| ncVersionList[ncID] = 0 | ||
| } | ||
| return ncVersionList | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,20 +5,25 @@ import ( | |
| "encoding/json" | ||
| "encoding/xml" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "net/http" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/Azure/azure-container-networking/cns/logger" | ||
| "github.com/Azure/azure-container-networking/common" | ||
| ) | ||
|
|
||
| const ( | ||
| //GetNmAgentSupportedApiURLFmt Api endpoint to get supported Apis of NMAgent | ||
| GetNmAgentSupportedApiURLFmt = "http://%s/machine/plugins/?comp=nmagent&type=GetSupportedApis" | ||
| GetNetworkContainerVersionURLFmt = "http://%s/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/%s/networkContainers/%s/version/authenticationToken/%s/api-version/1" | ||
| GetNmAgentSupportedApiURLFmt = "http://%s/machine/plugins/?comp=nmagent&type=GetSupportedApis" | ||
| GetNetworkContainerVersionURLFmt = "http://%s/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/%s/networkContainers/%s/version/authenticationToken/%s/api-version/1" | ||
| GetNcVersionListWithOutTokenURLFmt = "http://%s/machine/plugins/?comp=nmagent&type=NetworkManagement/interfaces/api-version/%s" | ||
| ) | ||
|
|
||
| //WireServerIP - wire server ip | ||
| var WireserverIP = "168.63.129.16" | ||
| var getNcVersionListWithOutTokenURLVersion = "2" | ||
|
|
||
| // NMANetworkContainerResponse - NMAgent response. | ||
| type NMANetworkContainerResponse struct { | ||
|
|
@@ -31,6 +36,36 @@ type NMAgentSupportedApisResponseXML struct { | |
| SupportedApis []string `xml:"type"` | ||
| } | ||
|
|
||
| type ContainerInfo struct { | ||
| NetworkContainerID string `json:"networkContainerId"` | ||
| Version string `json:"version"` | ||
| } | ||
|
|
||
| type NMANetworkContainerListResponse struct { | ||
| ResponseCode string `json:"httpStatusCode"` | ||
| Containers []ContainerInfo `json:"networkContainers"` | ||
| } | ||
|
|
||
| // NMAgentClient is client to handle queries to nmagent | ||
| type NMAgentClient struct { | ||
| connectionURL string | ||
| } | ||
|
|
||
| // NMAgentClientInterface has interface that nmagent client will handle | ||
| type NMAgentClientInterface interface { | ||
| GetNcVersionListWithOutToken(ncNeedUpdateList []string) map[string]int | ||
| } | ||
|
|
||
| // NewNMAgentClient create a new nmagent client. | ||
| func NewNMAgentClient(url string) (*NMAgentClient, error) { | ||
| if url == "" { | ||
| url = fmt.Sprintf(GetNcVersionListWithOutTokenURLFmt, WireserverIP, getNcVersionListWithOutTokenURLVersion) | ||
| } | ||
| return &NMAgentClient{ | ||
| connectionURL: url, | ||
| }, nil | ||
| } | ||
|
|
||
| // JoinNetwork joins the given network | ||
| func JoinNetwork( | ||
| networkID string, | ||
|
|
@@ -149,3 +184,42 @@ func GetNmAgentSupportedApis( | |
| logger.Printf("[NMAgentClient][Response] GetNmAgentSupportedApis. Response: %+v.", response) | ||
| return xmlDoc.SupportedApis, nil | ||
| } | ||
|
|
||
| // GetNcVersionListWithOutToken query nmagent for programmed container version. | ||
| func (nmagentclient *NMAgentClient) GetNcVersionListWithOutToken(ncNeedUpdateList []string) map[string]int { | ||
| ncVersionList := make(map[string]int) | ||
| now := time.Now() | ||
| response, err := http.Get(nmagentclient.connectionURL) | ||
| latency := time.Since(now) | ||
| logger.Printf("[NMAgentClient][Response] GetNcVersionListWithOutToken response: %+v, latency is %d", response, latency.Milliseconds()) | ||
|
|
||
| if response.StatusCode != http.StatusOK { | ||
| logger.Printf("[NMAgentClient][Response] GetNcVersionListWithOutToken failed with %d, err is %v", response.StatusCode, err) | ||
| return nil | ||
|
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. Do you think it would be better to return (map[string]int, error) from this function? That way we can send the error like this to the caller and handle the response based on the error being non nil
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. Could I keep as it is for current version? No matter what error we got, we'll keep retrying. Let me change the return value in next PR when we extend this logic to other CNS scenario. Then we need to choose different behavior when different error occur, e.g. whether keep retrying or fall back to token API |
||
| } | ||
|
|
||
| var nmaNcListResponse NMANetworkContainerListResponse | ||
| rBytes, _ := ioutil.ReadAll(response.Body) | ||
| logger.Printf("Response body is %v", rBytes) | ||
| json.Unmarshal(rBytes, &nmaNcListResponse) | ||
| if nmaNcListResponse.ResponseCode != strconv.Itoa(http.StatusOK) { | ||
| logger.Printf("[NMAgentClient][Response] GetNcVersionListWithOutToken unmarshal failed with %s", rBytes) | ||
| return nil | ||
| } | ||
|
|
||
| var receivedNcVersionListInMap = make(map[string]string) | ||
| for _, containers := range nmaNcListResponse.Containers { | ||
| receivedNcVersionListInMap[containers.NetworkContainerID] = containers.Version | ||
| } | ||
| for _, ncID := range ncNeedUpdateList { | ||
| if version, ok := receivedNcVersionListInMap[ncID]; ok { | ||
| if versionInInt, err := strconv.Atoi(version); err != nil { | ||
| logger.Printf("[NMAgentClient][Response] GetNcVersionListWithOutToken translate version %s to int failed with %s", version, err) | ||
| } else { | ||
| ncVersionList[ncID] = versionInInt | ||
| logger.Printf("Containers id is %s, programmed NC version is %d", ncID, versionInInt) | ||
| } | ||
| } | ||
| } | ||
| return ncVersionList | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.