From f727dad8ba83680ee79125455eb06b6c6d776e23 Mon Sep 17 00:00:00 2001 From: wangdepeng Date: Thu, 30 May 2024 15:50:57 +0800 Subject: [PATCH] fix: connect to apiserver connection refused Signed-off-by: wangdepeng --- pkg/kubenest/constants/constant.go | 1 + pkg/kubenest/init.go | 2 +- .../apiserver/mainfests_deployment.go | 2 ++ pkg/kubenest/tasks/anp.go | 20 ++++++++++++++++++- pkg/kubenest/tasks/check.go | 1 + 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/pkg/kubenest/constants/constant.go b/pkg/kubenest/constants/constant.go index f33f7cc95..49267f16e 100644 --- a/pkg/kubenest/constants/constant.go +++ b/pkg/kubenest/constants/constant.go @@ -41,6 +41,7 @@ const ( //controlplane apiserver ApiServer = "apiserver" + ApiServerAnp = "apiserver-anp" ApiServerReplicas = 1 ApiServerServiceSubnet = "10.237.6.0/18" ApiServerEtcdListenClientPort = 2379 diff --git a/pkg/kubenest/init.go b/pkg/kubenest/init.go index 727af0b27..b67099239 100644 --- a/pkg/kubenest/init.go +++ b/pkg/kubenest/init.go @@ -60,9 +60,9 @@ func NewInitPhase(opts *InitOptions) *workflow.Phase { initPhase.AppendTask(tasks.NewVirtualClusterApiserverTask()) initPhase.AppendTask(tasks.NewUploadKubeconfigTask()) initPhase.AppendTask(tasks.NewCheckApiserverHealthTask()) - initPhase.AppendTask(tasks.NewAnpTask()) initPhase.AppendTask(tasks.NewComponentTask()) initPhase.AppendTask(tasks.NewCheckControlPlaneTask()) + initPhase.AppendTask(tasks.NewAnpTask()) // create core-dns initPhase.AppendTask(tasks.NewCoreDNSTask()) // add server diff --git a/pkg/kubenest/manifest/controlplane/apiserver/mainfests_deployment.go b/pkg/kubenest/manifest/controlplane/apiserver/mainfests_deployment.go index 03ba71885..bd76bd505 100644 --- a/pkg/kubenest/manifest/controlplane/apiserver/mainfests_deployment.go +++ b/pkg/kubenest/manifest/controlplane/apiserver/mainfests_deployment.go @@ -136,6 +136,7 @@ kind: Deployment metadata: labels: virtualCluster-app: apiserver + virtualCluster-anp: apiserver-anp app.kubernetes.io/managed-by: virtual-cluster-controller name: {{ .DeploymentName }} namespace: {{ .Namespace }} @@ -148,6 +149,7 @@ spec: metadata: labels: virtualCluster-app: apiserver + virtualCluster-anp: apiserver-anp spec: automountServiceAccountToken: false hostNetwork: true diff --git a/pkg/kubenest/tasks/anp.go b/pkg/kubenest/tasks/anp.go index 69f74bd46..5c780ca26 100644 --- a/pkg/kubenest/tasks/anp.go +++ b/pkg/kubenest/tasks/anp.go @@ -3,6 +3,7 @@ package tasks import ( "context" "fmt" + apiclient "github.com/kosmos.io/kosmos/pkg/kubenest/util/api-client" "strings" "github.com/pkg/errors" @@ -43,7 +44,7 @@ func NewAnpTask() workflow.Task { }, { Name: "check-anp-health", - Run: runCheckVirtualClusterAPIServer, + Run: runCheckVirtualClusterAnp, }, }, } @@ -350,3 +351,20 @@ func runUploadProxyAgentCert(r workflow.RunData) error { klog.V(2).InfoS("[Upload-ProxyAgentCert] Successfully uploaded virtual cluster agent certs to secret", "virtual cluster", klog.KObj(data)) return nil } + +func runCheckVirtualClusterAnp(r workflow.RunData) error { + data, ok := r.(InitData) + if !ok { + return errors.New("check-VirtualClusterAnp task invoked with an invalid data struct") + } + + checker := apiclient.NewVirtualClusterChecker(data.RemoteClient(), constants.ComponentBeReadyTimeout) + + err := checker.WaitForSomePods(virtualClusterAnpLabels.String(), data.GetNamespace(), 1) + if err != nil { + return fmt.Errorf("checking for virtual cluster anp to ready timeout, err: %w", err) + } + + klog.V(2).InfoS("[check-VirtualClusterAPIServer] the virtual cluster anp is ready", "virtual cluster", klog.KObj(data)) + return nil +} diff --git a/pkg/kubenest/tasks/check.go b/pkg/kubenest/tasks/check.go index 93b9041af..d87182ea2 100644 --- a/pkg/kubenest/tasks/check.go +++ b/pkg/kubenest/tasks/check.go @@ -17,6 +17,7 @@ var ( virtualClusterManagerLabels = labels.Set{"virtualCluster-app": constants.VirtualClusterScheduler} virtualClusterApiserverLabels = labels.Set{"virtualCluster-app": constants.ApiServer} virtualClusterEtcdLabels = labels.Set{"virtualCluster-app": constants.Etcd} + virtualClusterAnpLabels = labels.Set{"virtualCluster-anp": constants.ApiServerAnp} ) func NewCheckApiserverHealthTask() workflow.Task {