wangpengfei
2023-06-02 064c0874e5fd041c4641ef873d1bf72ac98a184d
src/util/util.go
@@ -3,23 +3,27 @@
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"
)
// 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 +101,20 @@
}
// 安装Docker
func installDocker(nodeIP, sshUsername, sshPassword string, sshPort int) error {
func InstallDocker(nodeIP, sshUsername, sshPassword string, sshPort int) error {
   // 检查Docker是否已安装
   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 +122,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 +130,10 @@
}
// 安装kubectl
func installKubectl(nodeIP, sshUsername, sshPassword string, sshPort int) error {
func InstallKubectl(nodeIP, sshUsername, sshPassword string, sshPort int) error {
   // 检查kubectl是否已安装
   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
}