Skip to content

Commit cf34795

Browse files
committed
changing logic around noderegister
1 parent aafc041 commit cf34795

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

cns/service/main.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ const (
4949
configFileName = "config.json"
5050
dncApiVersion = "?api-version=2018-03-01"
5151
poolIPAMRefreshRateInMilliseconds = 1000
52+
53+
// 720 * acn.FiveSeconds sec sleeps = 1Hr
54+
maxRetryNodeRegister = 720
5255
)
5356

5457
// Version is populated by make during build.
@@ -274,27 +277,28 @@ func registerNode(httpc *http.Client, httpRestService cns.HTTPService, dncEP, in
274277

275278
nodeRegisterRequest.NmAgentSupportedApis = supportedApis
276279

277-
nodeRegisterTicker := time.NewTicker(acn.FiveSeconds)
278-
responseChan := make(chan error)
279-
280-
for {
281-
sendRegisterNodeRequest(httpc, httpRestService, nodeRegisterRequest, url, responseChan)
282-
select {
283-
case responseErr := <-responseChan:
284-
return responseErr
285-
case <-nodeRegisterTicker.C:
286-
continue
280+
//CNS tries to register Node for maximum of an hour.
281+
for tryNum := 0; tryNum <= maxRetryNodeRegister; tryNum++ {
282+
success, err := sendRegisterNodeRequest(httpc, httpRestService, nodeRegisterRequest, url)
283+
if err != nil {
284+
return err
287285
}
286+
if success {
287+
return nil
288+
}
289+
time.Sleep(acn.FiveSeconds)
288290
}
291+
logger.Errorf("[Azure CNS] Failed to register node %s after maximum reties for an hour with Infrastructure Network: %s PrivateEndpoint: %s",
292+
nodeID, infraVnet, dncEP)
293+
return nil
289294
}
290295

291296
// sendRegisterNodeRequest func helps in registering the node until there is an error.
292297
func sendRegisterNodeRequest(
293298
httpc *http.Client,
294299
httpRestService cns.HTTPService,
295300
nodeRegisterRequest cns.NodeRegisterRequest,
296-
registerURL string,
297-
responseChan chan<- error) {
301+
registerURL string) (bool, error) {
298302

299303
var (
300304
body bytes.Buffer
@@ -304,36 +308,33 @@ func sendRegisterNodeRequest(
304308

305309
err = json.NewEncoder(&body).Encode(nodeRegisterRequest)
306310
if err != nil {
307-
log.Errorf("encoding json failed with %v", err)
308-
responseChan <- err
309-
return
311+
log.Errorf("[Azure CNS] Failed to register node while encoding json failed with non-retriable err %v", err)
312+
return false, err
310313
}
311314

312315
response, err = httpc.Post(registerURL, "application/json", &body)
313316
if err != nil {
314-
logger.Errorf("[Azure CNS] Failed to register node with err: %+v", err)
315-
return
317+
logger.Errorf("[Azure CNS] Failed to register node with retriable err: %+v", err)
318+
return false, nil
316319
}
317320
defer response.Body.Close()
318321

319322
if response.StatusCode != http.StatusCreated {
320-
err = fmt.Errorf("[Azure CNS] Failed to register node with http status code %s", strconv.Itoa(response.StatusCode))
323+
err = fmt.Errorf("[Azure CNS] Failed to register node, DNC replied with http status code %s", strconv.Itoa(response.StatusCode))
321324
logger.Errorf(err.Error())
322-
responseChan <- err
323-
return
325+
return false, err
324326
}
325327

326328
var req cns.SetOrchestratorTypeRequest
327-
decodeErr := json.NewDecoder(response.Body).Decode(&req)
328-
if decodeErr != nil {
329-
log.Errorf("decoding Node Resgister response json failed with %v", err)
330-
responseChan <- err
331-
return
329+
err = json.NewDecoder(response.Body).Decode(&req)
330+
if err != nil {
331+
log.Errorf("[Azure CNS] decoding Node Resgister response json failed with non-retriable err %v", err)
332+
return false, err
332333
}
333334
httpRestService.SetNodeOrchestrator(&req)
334335

335336
logger.Printf("[Azure CNS] Node Registered")
336-
responseChan <- nil
337+
return true, nil
337338
}
338339

339340
// Main is the entry point for CNS.

0 commit comments

Comments
 (0)