From 794b44835f98df719f2b1186b2ad90a11f9a35b0 Mon Sep 17 00:00:00 2001
From: wangpengfei <274878379@qq.com>
Date: 星期四, 08 六月 2023 20:28:11 +0800
Subject: [PATCH] fix
---
src/main/main.go | 119 ++++++++++++++++++++++++++++++++---------------------------
1 files changed, 64 insertions(+), 55 deletions(-)
diff --git a/src/main/main.go b/src/main/main.go
index dce6324..cb4f545 100644
--- a/src/main/main.go
+++ b/src/main/main.go
@@ -26,17 +26,8 @@
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{
+func rancher_install_test(node rancher.Node) {
+ /* nodes := []Node{
{
Roles: []string{"etcd", "controlplane", "worker"},
IP: "192.168.20.189",
@@ -59,10 +50,10 @@
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)
+ err := rancher.InstallDockerAndRancher(node.IP, node.SSHUsername, node.SSHPassword, node.SSHPort)
if err != nil {
log.Fatalf("Failed to install Rancher: %v", err)
}
@@ -70,7 +61,7 @@
os.Exit(0)
}
-func cluster_test(clusterName string,nodes []Node,rancherConfig rancher.RancherConfig) {
+func cluster_test(clusterName string, rancherClusterConfig rancher.RancherClusterConfig) {
// Create the cluster
// Rancher configuration
@@ -79,16 +70,19 @@
BearerToken: "token-nnrsc:w68zdt8s47fnpjd5xqdl5hhzpz4j2d56kt5nx49nsswcbpdzc28kh5",
}*/
+ fmt.Println(rancherClusterConfig)
+ fmt.Println(rancherClusterConfig.RancherURL)
+ fmt.Println(rancherClusterConfig.BearerToken)
// Deploy clusterId
- clusterID, err := cluster.GetClusterID(rancherConfig.RancherURL, rancherConfig.BearerToken, clusterName)
+ clusterID, err := cluster.GetClusterID(rancherClusterConfig.RancherURL, rancherClusterConfig.BearerToken, clusterName)
if err != nil {
- log.Fatal(err)
- err = cluster.CreateCluster(rancherConfig, clusterName)
+ fmt.Printf("Cluster not found: %s %v\n", clusterName, err)
+ _, err = cluster.CreateCluster(rancherClusterConfig, 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)
+ clusterID, err = cluster.GetClusterID(rancherClusterConfig.RancherURL, rancherClusterConfig.BearerToken, clusterName)
if err != nil {
log.Fatal(err)
}
@@ -96,13 +90,13 @@
fmt.Println(clusterID)
// Deploy nodeCommand
- nodeCommand, err := cluster.GetNodeCommand(rancherConfig.RancherURL, rancherConfig.BearerToken, clusterID)
+ nodeCommand, err := cluster.GetNodeCommand(rancherClusterConfig.RancherURL, rancherClusterConfig.BearerToken, clusterID)
if err != nil {
log.Fatal(err)
}
fmt.Println(nodeCommand)
- for _, node := range nodes {
+ for _, node := range rancherClusterConfig.Nodes {
//Deploy Docker on each node
err = util.InstallDocker(node.IP, node.SSHUsername, node.SSHPassword, node.SSHPort)
if err != nil {
@@ -161,21 +155,53 @@
}
if rancherInstallCmd.Parsed() {
- rancher_install_test()
+ //// 鍒濆鍖栭厤缃В鏋愬簱
+ //viper.SetConfigName("config")
+ //viper.SetConfigType("yaml")
+ //viper.AddConfigPath("./config")
+ //
+ //// 璇诲彇閰嶇疆鏂囦欢
+ //err := viper.ReadInConfig()
+ //if err != nil {
+ // log.Fatalf("Failed to read config file: %v", err)
+ //}
+
+ // 瑙f瀽閰嶇疆鏂囦欢涓殑瀛楁
+ var rancherClusterConfig rancher.RancherClusterConfig
+ //err = viper.Unmarshal(&rancherClusterConfig)
+ //if err != nil {
+ // log.Fatalf("Failed to unmarshal config: %v", err)
+ //}
+
+ rancherClusterConfig.Nodes = make([]rancher.Node, 3)
+ rancherClusterConfig.Nodes[0] = rancher.Node{
+ Roles: []string{"worker"},
+ IP: "192.168.20.189",
+ SSHUsername: "basic",
+ SSHPassword: "123",
+ SSHPort: 22,
+ }
+
+ rancher_install_test(rancherClusterConfig.Nodes[0])
os.Exit(1)
}
// 鎵ц create 鍛戒护
if createCmd.Parsed() {
+
// 閰嶇疆 Kubernetes 闆嗙兢鐨� kubeconfig 璺緞
kubeconfig := flag.String("kubeconfig", filepath.Join(util.HomeDir(), ".kube", "config"), "kubeconfig file")
flag.Parse()
+
+ fmt.Println(&kubeconfig)
// 鍒涘缓 Kubernetes 瀹㈡埛绔�
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
+
+ fmt.Println(&config)
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
@@ -188,7 +214,12 @@
os.Exit(1)
}
- err = k8s.CreateDeploymentAndService(clientset, *createNamespace, *createDeployment, *createService)
+ err = k8s.CreateDeploymentAndService(k8s.Config{
+ Client: clientset,
+ NameSpace: *createNamespace,
+ DeploymentName: *createDeployment,
+ ServiceName: *createService,
+ })
if err != nil {
panic(err)
}
@@ -256,7 +287,12 @@
for _, ns := range namespaces {
deploymentName := ns
serviceName := ns
- err := k8s.CreateDeploymentAndService(clientset, ns, deploymentName, serviceName)
+ err := k8s.CreateDeploymentAndService(k8s.Config{
+ Client: clientset,
+ NameSpace: ns,
+ DeploymentName: deploymentName,
+ ServiceName: serviceName,
+ })
if err != nil {
log.Printf("\033[97;41mFailed to create resources in namespace %s: %v\033[0m\n", ns, err)
} else {
@@ -300,41 +336,14 @@
}
// 瑙f瀽閰嶇疆鏂囦欢涓殑瀛楁
- var rancherConfig rancher.RancherConfig
- err = viper.Unmarshal(&rancherConfig)
+ var rancherClusterConfig rancher.RancherClusterConfig
+ err = viper.Unmarshal(&rancherClusterConfig)
if err != nil {
log.Fatalf("Failed to unmarshal config: %v", err)
}
- clusterName := "kubernetus"
- nodes := []Node{
- {
- ClusterName: clusterName,
- Roles: []string{"worker"},
- IP: "192.168.20.189",
- SSHUsername: "basic",
- SSHPassword: "123",
- SSHPort: 22,
- },
- {
- ClusterName: clusterName,
- Roles: []string{"etcd", "worker"},
- IP: "192.168.20.10",
- SSHUsername: "basic",
- SSHPassword: "123",
- SSHPort: 22,
- },
- {
- ClusterName: clusterName,
- Roles: []string{"etcd", "controlplane","worker"},
- IP: "192.168.20.115",
- SSHUsername: "basic",
- SSHPassword: "alien123",
- SSHPort: 22,
- },
- // Add more nodes here if needed
- }
-
- cluster_test(clusterName, nodes, rancherConfig)
+ clusterName := "kubernetus"
+ fmt.Println(rancherClusterConfig)
+ cluster_test(clusterName, rancherClusterConfig)
}
}
--
Gitblit v1.8.0