From e4317aefa8672590b8d98db13d691c4ad0244e18 Mon Sep 17 00:00:00 2001 From: chantu Date: Thu, 21 Dec 2023 18:48:11 +0800 Subject: [PATCH] exec k8s config as input --- pkg/lorry/client/client.go | 6 +++--- pkg/lorry/client/k8sexec_client.go | 9 ++------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/pkg/lorry/client/client.go b/pkg/lorry/client/client.go index fe0d70dbcf0..5804b0bec22 100644 --- a/pkg/lorry/client/client.go +++ b/pkg/lorry/client/client.go @@ -20,7 +20,7 @@ along with this program. If not, see . package client import ( - context "context" + "context" "errors" "net/http" @@ -53,12 +53,12 @@ func NewClient(pod corev1.Pod) (Client, error) { return mockClient, mockClientError } - _, err := rest.InClusterConfig() + config, err := rest.InClusterConfig() if err != nil { // As the service does not run as a pod in the Kubernetes cluster, // it is unable to call the lorry service running as a pod using the pod's IP address. // In this scenario, it is recommended to use an k8s exec client instead. - execClient, err := NewK8sExecClientWithPod(&pod) + execClient, err := NewK8sExecClientWithPod(config, &pod) if err != nil { return nil, err } diff --git a/pkg/lorry/client/k8sexec_client.go b/pkg/lorry/client/k8sexec_client.go index 9ff90c21f05..d4c97848939 100644 --- a/pkg/lorry/client/k8sexec_client.go +++ b/pkg/lorry/client/k8sexec_client.go @@ -21,7 +21,7 @@ package client import ( "bytes" - context "context" + "context" "encoding/json" "fmt" "io" @@ -56,7 +56,7 @@ type K8sExecClient struct { } // NewK8sExecClientWithPod create a new OperationHTTPClient with lorry container -func NewK8sExecClientWithPod(pod *corev1.Pod) (*K8sExecClient, error) { +func NewK8sExecClientWithPod(restConfig *rest.Config, pod *corev1.Pod) (*K8sExecClient, error) { var ( err error ) @@ -82,11 +82,6 @@ func NewK8sExecClientWithPod(pod *corev1.Pod) (*K8sExecClient, error) { Namespace: pod.Namespace, } - restConfig, err := ctrl.GetConfig() - if err != nil { - return nil, errors.Wrap(err, "get k8s config failed") - } - restConfig.GroupVersion = &schema.GroupVersion{Group: "", Version: "v1"} restConfig.APIPath = "/api" restConfig.NegotiatedSerializer = scheme.Codecs.WithoutConversion()