From 7af5aa38ad8f685f642007f95716443382f2a0be Mon Sep 17 00:00:00 2001 From: qiuwei Date: Mon, 29 Apr 2024 10:40:29 +0800 Subject: [PATCH] fix: change api server service ip to clusterip,port to clusterport Signed-off-by: qiuwei --- pkg/kubenest/constants/constant.go | 1 + pkg/kubenest/tasks/upload.go | 17 +++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/pkg/kubenest/constants/constant.go b/pkg/kubenest/constants/constant.go index c427fbcae..2770f2265 100644 --- a/pkg/kubenest/constants/constant.go +++ b/pkg/kubenest/constants/constant.go @@ -44,6 +44,7 @@ const ( ApiServerServiceType = "NodePort" // APICallRetryInterval defines how long kubeadm should wait before retrying a failed API operation ApiServerCallRetryInterval = 100 * time.Millisecond + APIServerSVCPortName = "client" //controlplane etcd Etcd = "etcd" diff --git a/pkg/kubenest/tasks/upload.go b/pkg/kubenest/tasks/upload.go index 8404afaa4..068687b15 100644 --- a/pkg/kubenest/tasks/upload.go +++ b/pkg/kubenest/tasks/upload.go @@ -170,9 +170,10 @@ func runUploadAdminKubeconfig(r workflow.RunData) error { if err != nil { return err } - nodePort := getNodePortFromAPIServerService(service) - endpoint = fmt.Sprintf("https://%s:%d", data.ControlplaneAddress(), nodePort) - + clusterPort := getClusterPortFromAPIServerService(service) + clusterIP := service.Spec.ClusterIP + fmt.Printf("UploadAdminKubeconfig get Cluster IP is : %s\n", clusterIP) + endpoint = fmt.Sprintf("https://%s:%d", clusterIP, clusterPort) kubeconfig, err := buildKubeConfigFromSpec(data, endpoint) if err != nil { return err @@ -199,18 +200,18 @@ func runUploadAdminKubeconfig(r workflow.RunData) error { return nil } -func getNodePortFromAPIServerService(service *corev1.Service) int32 { - var nodePort int32 +func getClusterPortFromAPIServerService(service *corev1.Service) int32 { + var clusterPort int32 if service.Spec.Type == corev1.ServiceTypeNodePort { for _, port := range service.Spec.Ports { - if port.Name != "client" { + if port.Name != constants.APIServerSVCPortName { continue } - nodePort = port.NodePort + clusterPort = port.Port } } - return nodePort + return clusterPort } func buildKubeConfigFromSpec(data InitData, serverURL string) (*clientcmdapi.Config, error) {