From 064c0874e5fd041c4641ef873d1bf72ac98a184d Mon Sep 17 00:00:00 2001 From: wangpengfei <274878379@qq.com> Date: 星期五, 02 六月 2023 16:26:09 +0800 Subject: [PATCH] Merge branch 'master' into fly_develop --- src/util/util.go | 35 +++++++++++++++++++++++++++++------ 1 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/util/util.go b/src/util/util.go index 08caedd..1831dae 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -3,9 +3,13 @@ import ( "context" "crypto/tls" + "flag" "fmt" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/clientcmd" "net/http" "os" + "path/filepath" "time" "golang.org/x/crypto/ssh" @@ -100,17 +104,17 @@ 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.") @@ -129,7 +133,7 @@ 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 +141,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 +149,22 @@ fmt.Println("kubectl has been installed on the remote server.") return nil } + +func GetClient() (*kubernetes.Clientset, error) { + // 閰嶇疆 Kubernetes 闆嗙兢鐨� kubeconfig 璺緞 + kubeconfig := flag.String("kubeconfig", filepath.Join(HomeDir(), ".kube", "config"), "kubeconfig file") + flag.Parse() + + // 鍒涘缓 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