From 86fb135895efa53aa1827e39b7699fdf514448fb Mon Sep 17 00:00:00 2001 From: Allen Li Date: Thu, 5 Dec 2024 12:06:17 -0500 Subject: [PATCH 1/6] add channel Signed-off-by: Allen Li --- controllers/goroutines/cleanup_resources.go | 3 ++- controllers/goroutines/operator_status.go | 3 ++- controllers/goroutines/waitToCreateCsCR.go | 3 ++- main.go | 13 +++++++++---- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/controllers/goroutines/cleanup_resources.go b/controllers/goroutines/cleanup_resources.go index dc0e87104..1446c2766 100644 --- a/controllers/goroutines/cleanup_resources.go +++ b/controllers/goroutines/cleanup_resources.go @@ -137,7 +137,8 @@ func CleanupMongodbPreloadCm(bs *bootstrap.Bootstrap) { } } -func CleanupResources(bs *bootstrap.Bootstrap) { +func CleanupResources(ch chan *bootstrap.Bootstrap) { + bs := <-ch go CleanupKeycloakCert(bs) go CleanupMongodbPreloadCm(bs) } diff --git a/controllers/goroutines/operator_status.go b/controllers/goroutines/operator_status.go index 7d57fc1b1..06e195eff 100644 --- a/controllers/goroutines/operator_status.go +++ b/controllers/goroutines/operator_status.go @@ -35,8 +35,9 @@ import ( var ctx = context.Background() // UpdateCsCrStatus will update cs cr status according to each bedrock operator -func UpdateCsCrStatus(bs *bootstrap.Bootstrap) { +func UpdateCsCrStatus(ch chan *bootstrap.Bootstrap) { for { + bs := <-ch instance := &apiv3.CommonService{} if err := bs.Reader.Get(ctx, types.NamespacedName{Name: "common-service", Namespace: bs.CSData.OperatorNs}, instance); err != nil { if !errors.IsNotFound(err) { diff --git a/controllers/goroutines/waitToCreateCsCR.go b/controllers/goroutines/waitToCreateCsCR.go index 2a75336dd..3771673b9 100644 --- a/controllers/goroutines/waitToCreateCsCR.go +++ b/controllers/goroutines/waitToCreateCsCR.go @@ -28,8 +28,9 @@ import ( ) // WaitToCreateCsCR waits for the creation of the CommonService CR in the operator namespace. -func WaitToCreateCsCR(bs *bootstrap.Bootstrap) { +func WaitToCreateCsCR(ch chan *bootstrap.Bootstrap) { for { + bs := <-ch klog.Infof("Start to Create CommonService CR in the namespace %s", bs.CSData.OperatorNs) if err := bs.CreateCsCR(); err != nil { if strings.Contains(fmt.Sprint(err), "failed to call webhook") { diff --git a/main.go b/main.go index cee370c26..252d267b9 100644 --- a/main.go +++ b/main.go @@ -154,12 +154,15 @@ func main() { klog.Errorf("Cleanup Webhook Resources failed: %v", err) os.Exit(1) } - // Update CS CR Status - go goroutines.UpdateCsCrStatus(bs) + + // Setup a channel to suspend go routines + ch := make(chan *bootstrap.Bootstrap) + // Update CS CR Status, wait for signal from channel + go goroutines.UpdateCsCrStatus(ch) // Create CS CR - go goroutines.WaitToCreateCsCR(bs) + go goroutines.WaitToCreateCsCR(ch) // Delete Keycloak Cert - go goroutines.CleanupResources(bs) + go goroutines.CleanupResources(ch) if err = (&controllers.CommonServiceReconciler{ Bootstrap: bs, @@ -201,6 +204,8 @@ func main() { klog.Error(err, "unable to create controller", "controller", "V1AddLabel") os.Exit(1) } + // start go routines + ch <- bs } } else { klog.Infof("Common Service Operator goes dormant in the namespace %s", operatorNs) From 751158a035d3831712f708efa47c3555dce171ad Mon Sep 17 00:00:00 2001 From: Allen Li Date: Thu, 5 Dec 2024 09:20:46 -0800 Subject: [PATCH 2/6] add debug message Signed-off-by: Allen Li --- controllers/goroutines/operator_status.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/controllers/goroutines/operator_status.go b/controllers/goroutines/operator_status.go index 06e195eff..bc6a7bfdd 100644 --- a/controllers/goroutines/operator_status.go +++ b/controllers/goroutines/operator_status.go @@ -36,8 +36,10 @@ var ctx = context.Background() // UpdateCsCrStatus will update cs cr status according to each bedrock operator func UpdateCsCrStatus(ch chan *bootstrap.Bootstrap) { + klog.Info("start update CSCR") for { bs := <-ch + klog.Info("Get bs from channel") instance := &apiv3.CommonService{} if err := bs.Reader.Get(ctx, types.NamespacedName{Name: "common-service", Namespace: bs.CSData.OperatorNs}, instance); err != nil { if !errors.IsNotFound(err) { From 03805a1e4f205ee0d42ddda2cd82e7ad83e4e039 Mon Sep 17 00:00:00 2001 From: Allen Li Date: Thu, 5 Dec 2024 09:21:26 -0800 Subject: [PATCH 3/6] remove debug message Signed-off-by: Allen Li --- controllers/goroutines/operator_status.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/controllers/goroutines/operator_status.go b/controllers/goroutines/operator_status.go index bc6a7bfdd..06e195eff 100644 --- a/controllers/goroutines/operator_status.go +++ b/controllers/goroutines/operator_status.go @@ -36,10 +36,8 @@ var ctx = context.Background() // UpdateCsCrStatus will update cs cr status according to each bedrock operator func UpdateCsCrStatus(ch chan *bootstrap.Bootstrap) { - klog.Info("start update CSCR") for { bs := <-ch - klog.Info("Get bs from channel") instance := &apiv3.CommonService{} if err := bs.Reader.Get(ctx, types.NamespacedName{Name: "common-service", Namespace: bs.CSData.OperatorNs}, instance); err != nil { if !errors.IsNotFound(err) { From 3d012bafb61b7bc1b583d486cc29960198e2876b Mon Sep 17 00:00:00 2001 From: Allen Li Date: Thu, 5 Dec 2024 12:28:29 -0500 Subject: [PATCH 4/6] start go routines when cert-manager not exist Signed-off-by: Allen Li --- main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.go b/main.go index 252d267b9..d9a0468d8 100644 --- a/main.go +++ b/main.go @@ -181,6 +181,8 @@ func main() { } if !exist && err == nil { klog.Infof("cert-manager CRD does not exist, skip cert-manager related controllers initialization") + // start go routines + ch <- bs } else if exist && err == nil { if err = (&certmanagerv1controllers.CertificateRefreshReconciler{ From b2732d9a66c2d2eb24bff2b901558aef83d8dc6a Mon Sep 17 00:00:00 2001 From: Allen Li Date: Mon, 9 Dec 2024 14:35:04 -0500 Subject: [PATCH 5/6] start go routine earlier Signed-off-by: Allen Li --- main.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index d9a0468d8..d41992c88 100644 --- a/main.go +++ b/main.go @@ -173,6 +173,9 @@ func main() { os.Exit(1) } + // start go routines + ch <- bs + // check if cert-manager CRD does not exist, then skip cert-manager related controllers initialization exist, err := bs.CheckCRD(constant.CertManagerAPIGroupVersionV1, "Certificate") if err != nil { @@ -181,8 +184,6 @@ func main() { } if !exist && err == nil { klog.Infof("cert-manager CRD does not exist, skip cert-manager related controllers initialization") - // start go routines - ch <- bs } else if exist && err == nil { if err = (&certmanagerv1controllers.CertificateRefreshReconciler{ @@ -206,8 +207,6 @@ func main() { klog.Error(err, "unable to create controller", "controller", "V1AddLabel") os.Exit(1) } - // start go routines - ch <- bs } } else { klog.Infof("Common Service Operator goes dormant in the namespace %s", operatorNs) From 5124b2ebc80e32d554caea5583fe8e5e0f4374bc Mon Sep 17 00:00:00 2001 From: Allen Li Date: Mon, 9 Dec 2024 11:43:24 -0800 Subject: [PATCH 6/6] add logs message Signed-off-by: Allen Li --- main.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.go b/main.go index d41992c88..d3b017d75 100644 --- a/main.go +++ b/main.go @@ -164,6 +164,7 @@ func main() { // Delete Keycloak Cert go goroutines.CleanupResources(ch) + klog.Infof("Setup commonservice manager") if err = (&controllers.CommonServiceReconciler{ Bootstrap: bs, Scheme: mgr.GetScheme(), @@ -174,6 +175,7 @@ func main() { } // start go routines + klog.Infof("Start go routines") ch <- bs // check if cert-manager CRD does not exist, then skip cert-manager related controllers initialization @@ -186,6 +188,7 @@ func main() { klog.Infof("cert-manager CRD does not exist, skip cert-manager related controllers initialization") } else if exist && err == nil { + klog.Infof("Set up certmanager Manager") if err = (&certmanagerv1controllers.CertificateRefreshReconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(),