cheliequan
2023-05-24 ab70148fc0213a4523d976099a18e438cb2ed067
src/main/main.go
@@ -8,14 +8,13 @@
   "path/filepath"
   "time"
   "../create"
   "../delete"
   "basic.com/aps/aps_deploy.git/src/cluster"
   "basic.com/aps/aps_deploy.git/src/rancher"
   "basic.com/aps/aps_deploy.git/src/util"
   "k8s.io/client-go/kubernetes"
   "k8s.io/client-go/tools/clientcmd"
   "basic.com/aps/aps_deploy.git/src/create"
   "basic.com/aps/aps_deploy.git/src/delete"
   "basic.com/aps/aps_deploy.git/src/util"
   "basic.com/aps/aps_deploy.git/src/k8s"
)
var (
@@ -25,10 +24,143 @@
   namespaces          = []string{"guangsheng", "geruimi", "tongsheng"}
)
type Node struct {
   ClusterName string   `json:"clusterName"`
   Roles       []string `json:"roles"`
   IP          string   `json:"ip"`
   SSHUsername string   `json:"sshUsername"`
   SSHPassword string   `json:"sshPassword"`
   SSHPort     int      `json:"sshPort"`
}
func rancher_install_test() {
   nodes := []Node{
      {
         Roles:       []string{"etcd", "controlplane", "worker"},
         IP:          "192.168.20.189",
         SSHUsername: "basic",
         SSHPassword: "123",
         SSHPort:     22,
      },
      {
         Roles:       []string{"worker"},
         IP:          "192.168.20.10",
         SSHUsername: "basic",
         SSHPassword: "123",
         SSHPort:     22,
      },
      {
         Roles:       []string{"worker"},
         IP:          "192.168.20.115",
         SSHUsername: "basic",
         SSHPassword: "alien123",
         SSHPort:     22,
      },
      // Add more nodes here if needed
   }
   //install rancher on master node
   err := rancher.InstallDockerAndRancher(nodes[0].IP, nodes[0].SSHUsername, nodes[0].SSHPassword, nodes[0].SSHPort)
   if err != nil {
      log.Fatalf("Failed to install Rancher: %v", err)
   }
   os.Exit(0)
}
func cluster_test() {
   clusterName := "kubernetus"
   nodes := []Node{
      {
         ClusterName: clusterName,
         Roles:       []string{"etcd", "controlplane", "worker"},
         IP:          "192.168.20.189",
         SSHUsername: "basic",
         SSHPassword: "123",
         SSHPort:     22,
      },
      {
         ClusterName: clusterName,
         Roles:       []string{"worker"},
         IP:          "192.168.20.10",
         SSHUsername: "basic",
         SSHPassword: "123",
         SSHPort:     22,
      },
      {
         ClusterName: clusterName,
         Roles:       []string{"worker"},
         IP:          "192.168.20.115",
         SSHUsername: "basic",
         SSHPassword: "alien123",
         SSHPort:     22,
      },
      // Add more nodes here if needed
   }
   // Create the cluster
   // Rancher configuration
   /*rancherConfig := RancherConfig{
      RancherURL:  "https://192.168.20.119:8443",
      BearerToken: "token-nnrsc:w68zdt8s47fnpjd5xqdl5hhzpz4j2d56kt5nx49nsswcbpdzc28kh5",
   }*/
   rancherConfig := rancher.RancherConfig{
      RancherURL:  "https://192.168.20.189:8443",
      BearerToken: "token-t4cdf:h7zhmbvbzdvd9mmjw8zmt8rh4m7rl5gtqpqljlhl9tlr2z26j9lf4l",
   }
   //   Deploy clusterId
   clusterID, err := cluster.GetClusterID(rancherConfig.RancherURL, rancherConfig.BearerToken, clusterName)
   if err != nil {
      log.Fatal(err)
      err = cluster.CreateCluster(rancherConfig, clusterName)
      if err != nil {
         log.Fatalf("Failed to create cluster: %v", err)
      }
      fmt.Printf("Cluster created: %s\n", clusterName)
      clusterID, err = cluster.GetClusterID(rancherConfig.RancherURL, rancherConfig.BearerToken, clusterName)
      if err != nil {
         log.Fatal(err)
      }
   }
   fmt.Println(clusterID)
   //   Deploy nodeCommand
   nodeCommand, err := cluster.GetNodeCommand(rancherConfig.RancherURL, rancherConfig.BearerToken, clusterID)
   if err != nil {
      log.Fatal(err)
   }
   fmt.Println(nodeCommand)
   for _, node := range nodes {
      //Deploy Docker on each node
      err = util.InstallDocker(node.IP, node.SSHUsername, node.SSHPassword, node.SSHPort)
      if err != nil {
         log.Fatal(err)
      }
      // Deploy Kubectl on each node
      err = util.InstallKubectl(node.IP, node.SSHUsername, node.SSHPassword, node.SSHPort)
      if err != nil {
         log.Fatal(err)
      }
      // Deploy Kubernetes roles on each node
      err = cluster.Deployk8sRolesOnNode(node.IP, node.SSHUsername, node.SSHPassword, nodeCommand, node.SSHPort, node.Roles)
      if err != nil {
         log.Fatal(err)
      }
   }
   os.Exit(0)
}
func main() {
   createCmd := flag.NewFlagSet("create", flag.ExitOnError)
   deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError)
   testCmd := flag.NewFlagSet("test", flag.ExitOnError)
   clusterCmd := flag.NewFlagSet("cluster", flag.ExitOnError)
   rancherInstallCmd := flag.NewFlagSet("rancher_install", flag.ExitOnError)
   createNamespace := createCmd.String("ns", "", "Namespace name")
   createDeployment := createCmd.String("deployment", "", "Deployment name")
@@ -39,7 +171,7 @@
   deleteService := deleteCmd.String("service", "", "Service name")
   if len(os.Args) < 2 {
      fmt.Println("create/delete/test command is required")
      fmt.Println("create/delete/test/cluster/rancher_install command is required")
      os.Exit(1)
   }
@@ -50,13 +182,17 @@
      deleteCmd.Parse(os.Args[2:])
   case "test":
      testCmd.Parse(os.Args[2:])
   case "cluster":
      clusterCmd.Parse(os.Args[2:])
   case "rancher_install":
      rancherInstallCmd.Parse(os.Args[2:])
   default:
      flag.PrintDefaults()
      os.Exit(1)
   }
   // 配置 Kubernetes 集群的 kubeconfig 路径
   kubeconfig := flag.String("kubeconfig", filepath.Join(util.homeDir(), ".kube", "config"), "kubeconfig file")
   kubeconfig := flag.String("kubeconfig", filepath.Join(util.HomeDir(), ".kube", "config"), "kubeconfig file")
   flag.Parse()
   // 创建 Kubernetes 客户端
@@ -78,12 +214,12 @@
         os.Exit(1)
      }
      err := create.CreateDeploymentAndService(clientset, *createNamespace, *createDeployment, *createService)
      err := k8s.CreateDeploymentAndService(clientset, *createNamespace, *createDeployment, *createService)
      if err != nil {
         panic(err)
      }
      nodeport, err := create.GetServiceNodePort(clientset, *createNamespace, *createService)
      nodeport, err := k8s.GetServiceNodePort(clientset, *createNamespace, *createService)
      if err != nil {
         panic(err)
      }
@@ -99,7 +235,7 @@
         os.Exit(1)
      }
      err := delete.DeleteResources(clientset, *deleteNamespace, *deleteDeployment, *deleteService)
      err := k8s.DeleteResources(clientset, *deleteNamespace, *deleteDeployment, *deleteService)
      if err != nil {
         panic(err)
      }
@@ -116,7 +252,7 @@
      for _, ns := range namespaces {
         deploymentName := ns
         serviceName := ns
         err := create.CreateDeploymentAndService(clientset, ns, deploymentName, serviceName)
         err := k8s.CreateDeploymentAndService(clientset, ns, deploymentName, serviceName)
         if err != nil {
            log.Printf("\033[97;41mFailed to create resources in namespace %s: %v\033[0m\n", ns, err)
         } else {
@@ -138,7 +274,7 @@
      // 删除多个 Namespace 下的相同名称的 Deployment 和 Service
      for _, ns := range namespaces {
         err = delete.DeleteResources(clientset, ns, ns, ns)
         err = k8s.DeleteResources(clientset, ns, ns, ns)
         if err != nil {
            panic(err)
         }
@@ -147,4 +283,12 @@
      fmt.Println("Resources deleted.")
   }
   if clusterCmd.Parsed() {
      cluster_test()
   }
   if rancherInstallCmd.Parsed() {
      rancher_install_test()
   }
}