Skip to content

Commit 2129a6f

Browse files
committed
Adjusting logic on re-registering the Node in mDNC case
1 parent 6717e48 commit 2129a6f

File tree

4 files changed

+93
-160
lines changed

4 files changed

+93
-160
lines changed

cns/NetworkContainerContract.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,7 @@ func (networkContainerRequestPolicy *NetworkContainerRequestPolicies) Validate()
373373

374374
// NodeInfoResponse - Struct to hold the node info response.
375375
type NodeInfoResponse struct {
376-
NetworkContainers []CreateNetworkContainerRequest
377-
NmAgentApisMissing bool
376+
NetworkContainers []CreateNetworkContainerRequest
378377
}
379378

380379
// NodeRegisterRequest - Struct to hold the node register request.

cns/restserver/internalapi.go

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import (
1010
"net/http"
1111
"net/http/httptest"
1212
"reflect"
13-
"runtime"
14-
"strconv"
15-
"time"
1613

1714
"github.com/Azure/azure-container-networking/cns"
1815
"github.com/Azure/azure-container-networking/cns/logger"
@@ -127,14 +124,6 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string,
127124
service.saveState()
128125
service.Unlock()
129126

130-
if nodeInfoResponse.NmAgentApisMissing {
131-
// RegisterNode again with NmAgent Apis list
132-
retErr := RegisterNode(httpc, service, dncEP, infraVnet, nodeID)
133-
if retErr != nil {
134-
logger.Errorf("[Azure-CNS] Failed to register Node ID: %s with error: %s", nodeID, err.Error())
135-
}
136-
}
137-
138127
// delete dangling NCs
139128
for nc := range ncsToBeDeleted {
140129
var body bytes.Buffer
@@ -262,89 +251,3 @@ func (service *HTTPRestService) CreateOrUpdateNetworkContainerInternal(req cns.C
262251

263252
return returnCode
264253
}
265-
266-
// RegisterNode - Tries to register node with DNC when CNS is started in managed DNC mode
267-
func RegisterNode(httpc *http.Client, httpRestService cns.HTTPService, dncEP, infraVnet, nodeID string) error {
268-
logger.Printf("[Azure CNS] Registering node %s with Infrastructure Network: %s PrivateEndpoint: %s", nodeID, infraVnet, dncEP)
269-
270-
var (
271-
numCPU = runtime.NumCPU()
272-
url = fmt.Sprintf(common.RegisterNodeURLFmt, dncEP, infraVnet, nodeID, dncApiVersion)
273-
nodeRegisterRequest cns.NodeRegisterRequest
274-
)
275-
276-
nodeRegisterRequest.NumCPU = numCPU
277-
supportedApis, retErr := nmagentclient.GetNmAgentSupportedApis(httpc, "")
278-
279-
if retErr != nil {
280-
logger.Errorf("[Azure CNS] Failed to retrieve SupportedApis from NMagent of node %s with Infrastructure Network: %s PrivateEndpoint: %s",
281-
nodeID, infraVnet, dncEP)
282-
return retErr
283-
}
284-
285-
//To avoid any null-pointer deferencing errors.
286-
if supportedApis == nil {
287-
supportedApis = []string{}
288-
}
289-
290-
nodeRegisterRequest.NmAgentSupportedApis = supportedApis
291-
292-
nodeRegisterTicker := time.NewTicker(common.FiveSeconds)
293-
responseChan := make(chan error)
294-
295-
for {
296-
select {
297-
case responseErr := <-responseChan:
298-
return responseErr
299-
case <-nodeRegisterTicker.C:
300-
go sendRegisterNodeRequest(httpc, httpRestService, nodeRegisterRequest, url, responseChan)
301-
}
302-
}
303-
}
304-
305-
// sendRegisterNodeRequest func helps in registering the node until there is an error.
306-
func sendRegisterNodeRequest(
307-
httpc *http.Client,
308-
httpRestService cns.HTTPService,
309-
nodeRegisterRequest cns.NodeRegisterRequest,
310-
registerURL string,
311-
responseChan chan<- error) {
312-
313-
var (
314-
body bytes.Buffer
315-
response *http.Response
316-
err = fmt.Errorf("")
317-
)
318-
319-
err = json.NewEncoder(&body).Encode(nodeRegisterRequest)
320-
if err != nil {
321-
log.Errorf("encoding json failed with %v", err)
322-
responseChan <- err
323-
return
324-
}
325-
326-
response, err = httpc.Post(registerURL, "application/json", &body)
327-
if err == nil {
328-
if response.StatusCode == http.StatusCreated {
329-
var req cns.SetOrchestratorTypeRequest
330-
decodeErr := json.NewDecoder(response.Body).Decode(&req)
331-
if decodeErr != nil {
332-
log.Errorf("decoding Node Resgister response json failed with %v", err)
333-
responseChan <- err
334-
return
335-
}
336-
httpRestService.SetNodeOrchestrator(&req)
337-
338-
logger.Printf("[Azure CNS] Node Registered")
339-
responseChan <- nil
340-
} else {
341-
err = fmt.Errorf("[Azure CNS] Failed to register node with http status code %s", strconv.Itoa(response.StatusCode))
342-
logger.Errorf(err.Error())
343-
responseChan <- err
344-
}
345-
346-
response.Body.Close()
347-
} else {
348-
logger.Errorf("[Azure CNS] Failed to register node with err: %+v", err)
349-
}
350-
}

cns/restserver/internalapi_test.go

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@
44
package restserver
55

66
import (
7-
"bytes"
87
"encoding/json"
98
"fmt"
10-
"io"
11-
"io/ioutil"
12-
"net/http"
139
"reflect"
1410
"strconv"
15-
"strings"
1611
"testing"
1712

1813
"github.com/Azure/azure-container-networking/cns"
@@ -221,17 +216,6 @@ func TestReconcileNCWithSystemPods(t *testing.T) {
221216
validateNCStateAfterReconcile(t, &req, expectedNcCount, expectedAllocatedPods)
222217
}
223218

224-
func TestRegisterNode(t *testing.T) {
225-
restartService()
226-
setEnv(t)
227-
setOrchestratorTypeInternal(cns.KubernetesCRD)
228-
229-
err := RegisterNode(NewTestClient(), svc, "localhost", "dummyvnet", "dummyNodeId")
230-
if err != nil {
231-
t.Errorf("Unexpected failure on register Node %s", err)
232-
}
233-
}
234-
235219
func setOrchestratorTypeInternal(orchestratorType string) {
236220
fmt.Println("setOrchestratorTypeInternal")
237221
svc.state.OrchestratorType = orchestratorType
@@ -470,47 +454,3 @@ func restartService() {
470454
service.Stop()
471455
startService()
472456
}
473-
474-
// RoundTripFunc .
475-
type RoundTripFunc func(req *http.Request) *http.Response
476-
477-
// RoundTrip .
478-
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
479-
return f(req), nil
480-
}
481-
482-
func mockRountTrip(req *http.Request) *http.Response {
483-
var (
484-
body io.ReadCloser
485-
returnCode = 200
486-
)
487-
// Test request parameters
488-
switch {
489-
case strings.Contains(req.URL.String(), "GetSupportedApis"):
490-
// Handle Call to NMAgent
491-
body = ioutil.NopCloser(bytes.NewBufferString(hostSupportedApis))
492-
493-
case strings.Contains(req.URL.String(), "dummyNodeId"):
494-
//Handle Call to register Node
495-
body = ioutil.NopCloser(bytes.NewBufferString("OK"))
496-
returnCode = 201
497-
498-
default:
499-
returnCode = 200
500-
}
501-
502-
return &http.Response{
503-
StatusCode: returnCode,
504-
// Send response to be tested
505-
Body: body,
506-
// Must be set to non-nil value or it panics
507-
Header: make(http.Header),
508-
}
509-
}
510-
511-
//NewTestClient returns *http.Client with Transport replaced to avoid making real calls
512-
func NewTestClient() *http.Client {
513-
return &http.Client{
514-
Transport: RoundTripFunc(mockRountTrip),
515-
}
516-
}

cns/service/main.go

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@
44
package main
55

66
import (
7+
"bytes"
78
"context"
89
"encoding/json"
910
"fmt"
11+
"net/http"
1012
"os"
1113
"os/signal"
14+
"runtime"
15+
"strconv"
1216
"strings"
1317
"syscall"
1418
"time"
1519

1620
localtls "github.com/Azure/azure-container-networking/server/tls"
1721

1822
"github.com/Azure/azure-container-networking/cns/ipampoolmonitor"
23+
"github.com/Azure/azure-container-networking/cns/nmagentclient"
1924

2025
"github.com/Azure/azure-container-networking/aitelemetry"
2126
"github.com/Azure/azure-container-networking/cnm/ipam"
@@ -243,6 +248,92 @@ func printVersion() {
243248
fmt.Printf("Version %v\n", version)
244249
}
245250

251+
// RegisterNode - Tries to register node with DNC when CNS is started in managed DNC mode
252+
func registerNode(httpc *http.Client, httpRestService cns.HTTPService, dncEP, infraVnet, nodeID string) error {
253+
logger.Printf("[Azure CNS] Registering node %s with Infrastructure Network: %s PrivateEndpoint: %s", nodeID, infraVnet, dncEP)
254+
255+
var (
256+
numCPU = runtime.NumCPU()
257+
url = fmt.Sprintf(acn.RegisterNodeURLFmt, dncEP, infraVnet, nodeID, dncApiVersion)
258+
nodeRegisterRequest cns.NodeRegisterRequest
259+
)
260+
261+
nodeRegisterRequest.NumCPU = numCPU
262+
supportedApis, retErr := nmagentclient.GetNmAgentSupportedApis(httpc, "")
263+
264+
if retErr != nil {
265+
logger.Errorf("[Azure CNS] Failed to retrieve SupportedApis from NMagent of node %s with Infrastructure Network: %s PrivateEndpoint: %s",
266+
nodeID, infraVnet, dncEP)
267+
return retErr
268+
}
269+
270+
//To avoid any null-pointer deferencing errors.
271+
if supportedApis == nil {
272+
supportedApis = []string{}
273+
}
274+
275+
nodeRegisterRequest.NmAgentSupportedApis = supportedApis
276+
277+
nodeRegisterTicker := time.NewTicker(acn.FiveSeconds)
278+
responseChan := make(chan error)
279+
280+
for {
281+
select {
282+
case responseErr := <-responseChan:
283+
return responseErr
284+
case <-nodeRegisterTicker.C:
285+
go sendRegisterNodeRequest(httpc, httpRestService, nodeRegisterRequest, url, responseChan)
286+
}
287+
}
288+
}
289+
290+
// sendRegisterNodeRequest func helps in registering the node until there is an error.
291+
func sendRegisterNodeRequest(
292+
httpc *http.Client,
293+
httpRestService cns.HTTPService,
294+
nodeRegisterRequest cns.NodeRegisterRequest,
295+
registerURL string,
296+
responseChan chan<- error) {
297+
298+
var (
299+
body bytes.Buffer
300+
response *http.Response
301+
err = fmt.Errorf("")
302+
)
303+
304+
err = json.NewEncoder(&body).Encode(nodeRegisterRequest)
305+
if err != nil {
306+
log.Errorf("encoding json failed with %v", err)
307+
responseChan <- err
308+
return
309+
}
310+
311+
response, err = httpc.Post(registerURL, "application/json", &body)
312+
if err == nil {
313+
if response.StatusCode == http.StatusCreated {
314+
var req cns.SetOrchestratorTypeRequest
315+
decodeErr := json.NewDecoder(response.Body).Decode(&req)
316+
if decodeErr != nil {
317+
log.Errorf("decoding Node Resgister response json failed with %v", err)
318+
responseChan <- err
319+
return
320+
}
321+
httpRestService.SetNodeOrchestrator(&req)
322+
323+
logger.Printf("[Azure CNS] Node Registered")
324+
responseChan <- nil
325+
} else {
326+
err = fmt.Errorf("[Azure CNS] Failed to register node with http status code %s", strconv.Itoa(response.StatusCode))
327+
logger.Errorf(err.Error())
328+
responseChan <- err
329+
}
330+
331+
response.Body.Close()
332+
} else {
333+
logger.Errorf("[Azure CNS] Failed to register node with err: %+v", err)
334+
}
335+
}
336+
246337
// Main is the entry point for CNS.
247338
func main() {
248339
// Initialize and parse command line arguments.
@@ -418,7 +509,7 @@ func main() {
418509
httpRestService.SetOption(acn.OptInfrastructureNetworkID, infravnet)
419510
httpRestService.SetOption(acn.OptNodeID, nodeID)
420511

421-
registerErr := restserver.RegisterNode(acn.GetHttpClient(), httpRestService, privateEndpoint, infravnet, nodeID)
512+
registerErr := registerNode(acn.GetHttpClient(), httpRestService, privateEndpoint, infravnet, nodeID)
422513
if registerErr != nil {
423514
logger.Errorf("[Azure CNS] Resgistering Node failed with error: %v PrivateEndpoint: %s InfrastructureNetworkID: %s NodeID: %s",
424515
registerErr,

0 commit comments

Comments
 (0)