From fea217048591823280a888b6c26f68558e51dded Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期五, 12 一月 2024 15:17:53 +0800 Subject: [PATCH] 增加admin_grpc环境变量 --- src/util/util.go | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/util/util.go b/src/util/util.go index 3da9a85..9e0170e 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -4,6 +4,8 @@ "context" "crypto/tls" "fmt" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/clientcmd" "net/http" "os" "time" @@ -12,14 +14,14 @@ ) // homeDir 鑾峰彇褰撳墠鐢ㄦ埛鐨勫鐩綍璺緞 -func homeDir() string { +func HomeDir() string { if h := os.Getenv("HOME"); h != "" { return h } return os.Getenv("USERPROFILE") // Windows 鐜涓嬭幏鍙栫敤鎴风洰褰� } -func sshExec(nodeIP, sshUsername, sshPassword, remoteSSHCommand string, sshPort int) (string, error) { +func SSHExec(nodeIP, sshUsername, sshPassword, remoteSSHCommand string, sshPort int) (string, error) { // SSH 杩炴帴閰嶇疆 config := &ssh.ClientConfig{ User: sshUsername, @@ -97,20 +99,20 @@ } // 瀹夎Docker -func installDocker(nodeIP, sshUsername, sshPassword string, sshPort int) error { +func InstallDocker(nodeIP, sshUsername, sshPassword string, sshPort int) error { // 妫�鏌ocker鏄惁宸插畨瑁� checkCommand := "which docker" - _, err := sshExec(nodeIP, sshUsername, sshPassword, checkCommand, sshPort) + _, err := SSHExec(nodeIP, sshUsername, sshPassword, checkCommand, sshPort) if err == nil { fmt.Println("Docker is already installed on the remote server.") return nil } // 瀹夎Docker - installCommand := "sudo curl -fsSL https://get.docker.com -o get-docker.sh && sudo sh get-docker.sh" - _, err = sshExec(nodeIP, sshUsername, sshPassword, installCommand, sshPort) + installCommand := "sudo curl -fsSL https://get.docker.com -o get-docker.sh && sudo sh get-docker.sh && sudo systemctl start docker && sudo systemctl enable docker" + _, err = SSHExec(nodeIP, sshUsername, sshPassword, installCommand, sshPort) if err != nil { - return fmt.Errorf("failed to install Docker on the remote server: %v", err) + return fmt.Errorf("failed to install Docker on the remote server:%v %v", nodeIP, err) } fmt.Println("Docker has been installed on the remote server.") @@ -118,7 +120,7 @@ } // Create an HTTP client with insecure TLS configuration -func createHTTPClient() *http.Client { +func CreateHTTPClient() *http.Client { transport := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } @@ -126,10 +128,10 @@ } // 瀹夎kubectl -func installKubectl(nodeIP, sshUsername, sshPassword string, sshPort int) error { +func InstallKubectl(nodeIP, sshUsername, sshPassword string, sshPort int) error { // 妫�鏌ubectl鏄惁宸插畨瑁� checkCommand := "which kubectl" - _, err := sshExec(nodeIP, sshUsername, sshPassword, checkCommand, sshPort) + _, err := SSHExec(nodeIP, sshUsername, sshPassword, checkCommand, sshPort) if err == nil { fmt.Println("kubectl is already installed on the remote server.") return nil @@ -137,7 +139,7 @@ // 瀹夎kubectl installCommand := "sudo curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && sudo chmod +x kubectl && sudo mv kubectl /usr/local/bin/" - _, err = sshExec(nodeIP, sshUsername, sshPassword, installCommand, sshPort) + _, err = SSHExec(nodeIP, sshUsername, sshPassword, installCommand, sshPort) if err != nil { return fmt.Errorf("failed to install kubectl on the remote server: %v", err) } @@ -145,3 +147,28 @@ fmt.Println("kubectl has been installed on the remote server.") return nil } + +var kubeConfig string + +func GetClient(cluster string) (*kubernetes.Clientset, error) { + // 閰嶇疆 Kubernetes 闆嗙兢鐨� kubeconfig 璺緞 + //if kubeConfig == nil { + // kubeConfig = flag.String("kubeconfig", filepath.Join(HomeDir(), ".kube", cluster, "config"), "kubeconfig file") + // flag.Parse() + //} + + kubeConfig = fmt.Sprintf("%s/.kube/%s/config", HomeDir(), cluster) + + // 鍒涘缓 Kubernetes 瀹㈡埛绔� + config, err := clientcmd.BuildConfigFromFlags("", kubeConfig) + if err != nil { + panic(err.Error()) + } + + clientset, err := kubernetes.NewForConfig(config) + if err != nil { + panic(err.Error()) + } + + return clientset, nil +} -- Gitblit v1.8.0