From 9192e78c6fee124083952777616870d33790cdab Mon Sep 17 00:00:00 2001 From: Min Min Date: Wed, 25 Dec 2024 14:19:53 +0800 Subject: [PATCH 1/4] fix connection improvement bugs Signed-off-by: Min Min --- pkg/tool/clientmanager/kube.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/tool/clientmanager/kube.go b/pkg/tool/clientmanager/kube.go index b38258a1af..e8fd8bee98 100644 --- a/pkg/tool/clientmanager/kube.go +++ b/pkg/tool/clientmanager/kube.go @@ -467,6 +467,10 @@ func (cm *KubeClientManager) getControllerRuntimeCluster(clusterID string) (cont return nil, fmt.Errorf("cluster %s not found", clusterID) } + if clusterInfo.Status != setting.Normal { + return nil, fmt.Errorf("cluster %s status: %s, cannot create client", clusterID, clusterInfo.Status) + } + var cfg *rest.Config switch clusterInfo.Type { From 60905b7405b7d910550ba306c4315afa3010a919 Mon Sep 17 00:00:00 2001 From: Min Min Date: Wed, 25 Dec 2024 15:06:58 +0800 Subject: [PATCH 2/4] fix panic Signed-off-by: Min Min --- pkg/tool/clientmanager/kube.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/tool/clientmanager/kube.go b/pkg/tool/clientmanager/kube.go index e8fd8bee98..8d4acb0cd1 100644 --- a/pkg/tool/clientmanager/kube.go +++ b/pkg/tool/clientmanager/kube.go @@ -487,11 +487,10 @@ func (cm *KubeClientManager) getControllerRuntimeCluster(clusterID string) (cont controllerClient, err := createControllerRuntimeCluster(cfg) if err == nil { - go func() { - if err := controllerClient.Start(ctrl.SetupSignalHandler()); err != nil { - log.Errorf("failed to start controller runtime cluster, error: %s", err) - } - }() + if err := controllerClient.Start(ctrl.SetupSignalHandler()); err != nil { + log.Errorf("failed to start controller runtime cluster, error: %s", err) + } + if !controllerClient.GetCache().WaitForCacheSync(context.Background()) { return nil, fmt.Errorf("failed to wait for controller runtime cluster to sync") } From c285393fdf2da35885ba810472c2430314c88b26 Mon Sep 17 00:00:00 2001 From: Min Min Date: Wed, 25 Dec 2024 15:25:28 +0800 Subject: [PATCH 3/4] bugfix for panic Signed-off-by: Min Min --- pkg/tool/clientmanager/kube.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/tool/clientmanager/kube.go b/pkg/tool/clientmanager/kube.go index 8d4acb0cd1..f5775934c1 100644 --- a/pkg/tool/clientmanager/kube.go +++ b/pkg/tool/clientmanager/kube.go @@ -50,6 +50,8 @@ import ( var kubeClientManagerInstance *KubeClientManager var once sync.Once +var stopContext = ctrl.SetupSignalHandler() + // TODO: Implement a Zadig-Kubernetes client interface, forbid business code to access these clients directly type KubeClientManager struct { @@ -447,7 +449,7 @@ func (cm *KubeClientManager) getControllerRuntimeCluster(clusterID string) (cont controllerClient, err := createControllerRuntimeCluster(ctrl.GetConfigOrDie()) if err == nil { go func() { - if err := controllerClient.Start(ctrl.SetupSignalHandler()); err != nil { + if err := controllerClient.Start(stopContext); err != nil { log.Errorf("failed to start controller runtime cluster, error: %s", err) } }() @@ -487,9 +489,11 @@ func (cm *KubeClientManager) getControllerRuntimeCluster(clusterID string) (cont controllerClient, err := createControllerRuntimeCluster(cfg) if err == nil { - if err := controllerClient.Start(ctrl.SetupSignalHandler()); err != nil { - log.Errorf("failed to start controller runtime cluster, error: %s", err) - } + go func() { + if err := controllerClient.Start(stopContext); err != nil { + log.Errorf("failed to start controller runtime cluster, error: %s", err) + } + }() if !controllerClient.GetCache().WaitForCacheSync(context.Background()) { return nil, fmt.Errorf("failed to wait for controller runtime cluster to sync") From def3335bdd81e51f6071201c1ac5277df87f7024 Mon Sep 17 00:00:00 2001 From: Min Min Date: Wed, 25 Dec 2024 15:28:22 +0800 Subject: [PATCH 4/4] update error message Signed-off-by: Min Min --- pkg/tool/clientmanager/kube.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/tool/clientmanager/kube.go b/pkg/tool/clientmanager/kube.go index f5775934c1..7c3f0bfdf9 100644 --- a/pkg/tool/clientmanager/kube.go +++ b/pkg/tool/clientmanager/kube.go @@ -470,7 +470,7 @@ func (cm *KubeClientManager) getControllerRuntimeCluster(clusterID string) (cont } if clusterInfo.Status != setting.Normal { - return nil, fmt.Errorf("cluster %s status: %s, cannot create client", clusterID, clusterInfo.Status) + return nil, fmt.Errorf("unable to connect to cluster: %s, status: %s", clusterInfo.Name, clusterInfo.Status) } var cfg *rest.Config