Skip to content

Commit d19e04f

Browse files
authored
Fix to bubble up the error and restart in case it fails to create CRD… (#867)
* Fix to bubble up the error and restart in case it fails to create CRD controllers * Fix returned error
1 parent 6312309 commit d19e04f

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

cns/service/main.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,11 @@ func main() {
504504
if config.ChannelMode == cns.CRD {
505505
requestControllerStopChannel := make(chan struct{})
506506
defer close(requestControllerStopChannel)
507-
IniitalizeCRDState(httpRestService, cnsconfig, requestControllerStopChannel)
507+
err = IniitalizeCRDState(httpRestService, cnsconfig, requestControllerStopChannel)
508+
if err != nil {
509+
logger.Errorf("Failed to start CRD Controller, err:%v.\n", err)
510+
return
511+
}
508512
}
509513

510514
logger.Printf("[Azure CNS] Start HTTP listener")
@@ -667,22 +671,23 @@ func main() {
667671
}
668672

669673
// initializeCRD state
670-
func IniitalizeCRDState(httpRestService cns.HTTPService, cnsconfig configuration.CNSConfig, exitChan <-chan struct{}) {
674+
func IniitalizeCRDState(httpRestService cns.HTTPService, cnsconfig configuration.CNSConfig, exitChan <-chan struct{}) error {
671675
var requestController requestcontroller.RequestController
672676

673677
logger.Printf("[Azure CNS] Starting request controller")
674678

675679
kubeConfig, err := kubecontroller.GetKubeConfig()
676680
if err != nil {
677681
logger.Errorf("[Azure CNS] Failed to get kubeconfig for request controller: %v", err)
678-
return
682+
return err
679683
}
680684

681685
//convert interface type to implementation type
682686
httpRestServiceImplementation, ok := httpRestService.(*restserver.HTTPRestService)
683687
if !ok {
684688
logger.Errorf("[Azure CNS] Failed to convert interface httpRestService to implementation: %v", httpRestService)
685-
return
689+
return fmt.Errorf("[Azure CNS] Failed to convert interface httpRestService to implementation: %v",
690+
httpRestService)
686691
}
687692

688693
// Set orchestrator type
@@ -695,7 +700,7 @@ func IniitalizeCRDState(httpRestService cns.HTTPService, cnsconfig configuration
695700
requestController, err = kubecontroller.NewCrdRequestController(httpRestServiceImplementation, kubeConfig)
696701
if err != nil {
697702
logger.Errorf("[Azure CNS] Failed to make crd request controller :%v", err)
698-
return
703+
return err
699704
}
700705

701706
// initialize the ipam pool monitor
@@ -704,7 +709,7 @@ func IniitalizeCRDState(httpRestService cns.HTTPService, cnsconfig configuration
704709
err = requestController.InitRequestController()
705710
if err != nil {
706711
logger.Errorf("[Azure CNS] Failed to initialized cns state :%v", err)
707-
return
712+
return err
708713
}
709714

710715
//Start the RequestController which starts the reconcile loop
@@ -737,11 +742,18 @@ func IniitalizeCRDState(httpRestService cns.HTTPService, cnsconfig configuration
737742
ctx := context.Background()
738743
logger.Printf("Starting IPAM Pool Monitor")
739744
go func() {
740-
if err := httpRestServiceImplementation.IPAMPoolMonitor.Start(ctx, poolIPAMRefreshRateInMilliseconds); err != nil {
741-
logger.Errorf("[Azure CNS] Failed to start pool monitor with err: %v", err)
742-
}
745+
for {
746+
if err := httpRestServiceImplementation.IPAMPoolMonitor.Start(ctx, poolIPAMRefreshRateInMilliseconds); err != nil {
747+
logger.Errorf("[Azure CNS] Failed to start pool monitor with err: %v", err)
748+
// todo: add a CNS metric to count # of failures
749+
} else {
750+
logger.Printf("[Azure CNS] Exiting IPAM Pool Monitor")
751+
return
752+
}
743753

744-
logger.Printf("[Azure CNS] Exiting IPAM Pool Monitor")
754+
// Retry after 1sec
755+
time.Sleep(time.Second)
756+
}
745757
}()
746758

747759
logger.Printf("Starting SyncHostNCVersion")
@@ -755,4 +767,6 @@ func IniitalizeCRDState(httpRestService cns.HTTPService, cnsconfig configuration
755767

756768
logger.Printf("[Azure CNS] Exiting SyncHostNCVersion")
757769
}()
770+
771+
return nil
758772
}

0 commit comments

Comments
 (0)