Skip to content

Commit 510ce73

Browse files
author
Jaeryn
committed
add nc version check via NMA
1 parent bb2c374 commit 510ce73

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

cns/nmagentclient/nmagentclient.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import (
99
"github.com/Azure/azure-container-networking/common"
1010
)
1111

12+
const (
13+
WireserverIP = "168.63.129.16"
14+
)
15+
1216
// NetworkContainerResponse - NMAgent response.
1317
type NetworkContainerResponse struct {
1418
ResponseCode string `json:"httpStatusCode"`

cns/restserver/restserver.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ type networkInfo struct {
9595
type HTTPService interface {
9696
common.ServiceAPI
9797
SendNCSnapShotPeriodically(int, chan bool)
98+
SetNodeOrchestrator(*cns.SetOrchestratorTypeRequest)
9899
SyncNodeStatus(string, string, string, json.RawMessage) (int, string)
99100
}
100101

@@ -2063,6 +2064,14 @@ func (service *HTTPRestService) SendNCSnapShotPeriodically(ncSnapshotIntervalInM
20632064
}
20642065
}
20652066

2067+
// SetNodeOrchestrator :- Set node orchestrator after registering with mDNC
2068+
func (service *HTTPRestService) SetNodeOrchestrator(r *cns.SetOrchestratorTypeRequest) {
2069+
body, _ := json.Marshal(r)
2070+
req, _ := http.NewRequest(http.MethodPost, "", bytes.NewBuffer(body))
2071+
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
2072+
service.setOrchestratorType(httptest.NewRecorder(), req)
2073+
}
2074+
20662075
// SyncNodeStatus :- Retrieve the latest node state from DNC & returns the first occurence of returnCode and error with respect to contextFromCNI
20672076
func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string, contextFromCNI json.RawMessage) (returnCode int, errStr string) {
20682077
logger.Printf("[Azure CNS] SyncNodeStatus")
@@ -2096,8 +2105,8 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string,
20962105
}
20972106

20982107
var (
2099-
ncsToBeAdded map[string]cns.CreateNetworkContainerRequest
2100-
ncsToBeDeleted map[string]bool
2108+
ncsToBeAdded = make(map[string]cns.CreateNetworkContainerRequest)
2109+
ncsToBeDeleted = make(map[string]bool)
21012110
)
21022111

21032112
// determine new NCs and NCs to be deleted
@@ -2108,25 +2117,28 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string,
21082117

21092118
for _, nc := range nodeInfoResponse.NetworkContainers {
21102119
ncid := cns.SwiftPrefix + nc.NetworkContainerid
2120+
delete(ncsToBeDeleted, ncid)
21112121
if _, exists := service.state.ContainerStatus[ncid]; !exists {
21122122
ncsToBeAdded[ncid] = nc
2113-
} else {
2114-
delete(ncsToBeDeleted, ncid)
21152123
}
21162124
}
21172125
service.lock.Unlock()
21182126

21192127
// check if the version is valid and save it to service state
21202128
for _, nc := range ncsToBeAdded {
21212129
response, err = nmagentclient.GetNetworkContainerVersion(nc.NetworkContainerid,
2122-
fmt.Sprintf(nodeInfoResponse.GetNCVersionURLFmt, nc.PrimaryInterfaceIdentifier, nc.NetworkContainerid, nc.AuthorizationToken))
2130+
fmt.Sprintf(nodeInfoResponse.GetNCVersionURLFmt,
2131+
nmagentclient.WireserverIP,
2132+
nc.PrimaryInterfaceIdentifier,
2133+
nc.NetworkContainerid,
2134+
nc.AuthorizationToken))
21232135
if err == nil {
21242136
if response.StatusCode == http.StatusOK {
21252137
var versionResponse nmagentclient.NetworkContainerResponse
2126-
if err = json.NewDecoder(response.Body).Decode(&versionResponse); err != nil {
2127-
response.Body.Close()
2128-
2129-
tmpErrStr = fmt.Sprintf("[Azure-CNS] Failed to decode GetNCVersion response for NC %s with error: %+v", nc.NetworkContainerid, err)
2138+
rBytes, _ := ioutil.ReadAll(response.Body)
2139+
json.Unmarshal(rBytes, &versionResponse)
2140+
if versionResponse.ResponseCode == "" {
2141+
tmpErrStr = fmt.Sprintf("[Azure-CNS] Failed to unmarshal GetNCVersion response %s for NC %s", rBytes, nc.NetworkContainerid)
21302142
if errStr == "" && bytes.Compare(nc.OrchestratorContext, contextFromCNI) == 0 {
21312143
returnCode = UnexpectedError
21322144
errStr = tmpErrStr
@@ -2144,7 +2156,7 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string,
21442156
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
21452157
service.createOrUpdateNetworkContainer(httptest.NewRecorder(), req)
21462158
} else {
2147-
tmpErrStr = fmt.Sprintf("[Azure-CNS] Network container %s v%d had not propagated to respective NMA", nc.NetworkContainerid, v1)
2159+
tmpErrStr = fmt.Sprintf("[Azure-CNS] Network container %s v%d had not propagated to respective NMA w/ v%d", nc.NetworkContainerid, v1, v2)
21482160
if errStr == "" && bytes.Compare(nc.OrchestratorContext, contextFromCNI) == 0 {
21492161
returnCode = NetworkContainerPendingStatePropagation
21502162
errStr = tmpErrStr

cns/service/main.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/Azure/azure-container-networking/aitelemetry"
2020
"github.com/Azure/azure-container-networking/cnm/ipam"
2121
"github.com/Azure/azure-container-networking/cnm/network"
22+
"github.com/Azure/azure-container-networking/cns"
2223
"github.com/Azure/azure-container-networking/cns/common"
2324
"github.com/Azure/azure-container-networking/cns/configuration"
2425
"github.com/Azure/azure-container-networking/cns/hnsclient"
@@ -216,8 +217,7 @@ func printVersion() {
216217
}
217218

218219
// Try to register node with DNC when CNS is started in managed DNC mode
219-
func registerNode(dncEP, infraVnet, nodeID string) {
220-
logger.Printf("[Azure CNS] SyncNodeStatus")
220+
func registerNode(httpRestService restserver.HTTPService, dncEP, infraVnet, nodeID string) {
221221
var (
222222
numCPU = runtime.NumCPU()
223223
response *http.Response
@@ -227,19 +227,21 @@ func registerNode(dncEP, infraVnet, nodeID string) {
227227
)
228228

229229
for err != nil {
230-
response, err = httpc.Post(fmt.Sprintf("%s/%s/node/%s/nclimit/%d%s", dncEP, infraVnet, nodeID, numCPU, dncApiVersion), "application/json", &body)
230+
response, err = httpc.Post(fmt.Sprintf("%s/%s/node/%s/cores/%d%s", dncEP, infraVnet, nodeID, numCPU, dncApiVersion), "application/json", &body)
231231
if err == nil {
232-
break
232+
if response.StatusCode == http.StatusCreated {
233+
var req cns.SetOrchestratorTypeRequest
234+
json.NewDecoder(response.Body).Decode(&req)
235+
httpRestService.SetNodeOrchestrator(&req)
236+
} else {
237+
logger.Errorf("[Azure CNS] Failed to register node with managed DNC with http status code: " + strconv.Itoa(response.StatusCode))
238+
}
239+
240+
response.Body.Close()
233241
}
234242
time.Sleep(time.Second * 5)
235243
}
236244

237-
response.Body.Close()
238-
239-
if response.StatusCode != http.StatusOK {
240-
panic("[Azure CNS] Failed to register node with managed DNC with http status code: " + strconv.Itoa(response.StatusCode))
241-
}
242-
243245
logger.Printf("[Azure CNS] Node Registered")
244246
}
245247

@@ -381,7 +383,7 @@ func main() {
381383
httpRestService.SetOption(acn.OptInfrastructureNetwork, infravnet)
382384
httpRestService.SetOption(acn.OptNodeID, nodeID)
383385

384-
registerNode(privateEndpoint, infravnet, nodeID)
386+
registerNode(httpRestService, privateEndpoint, infravnet, nodeID)
385387
go func(ep, vnet, node string) {
386388
// Periodically poll (30s) DNC for node updates
387389
for {

0 commit comments

Comments
 (0)