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/k8s/create.go |  419 ++++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 286 insertions(+), 133 deletions(-)

diff --git a/src/k8s/create.go b/src/k8s/create.go
index 3be6767..b08e0d9 100644
--- a/src/k8s/create.go
+++ b/src/k8s/create.go
@@ -7,6 +7,7 @@
 	"log"
 	"math/rand"
 	"path/filepath"
+	"strconv"
 
 	"basic.com/aps/aps_deploy.git/src/util"
 	appsv1 "k8s.io/api/apps/v1"
@@ -19,11 +20,37 @@
 )
 
 var (
-	replicas      int32 = 3
+	replicas      int32 = 1
 	port          int32 = 9081
-	namespaces          = []string{"guangsheng", "geruimi", "tongsheng"}
+	rpcPort       int32 = 9091
+	namespaces          = []string{"testFactory1", "testFactory2", "testFactory3"}
 	usedNodePorts       = make(map[int32]bool)
+	Projects            = []string{ProjectAps, ProjectCrm, ProjectSrm, ProjectWms}
 )
+
+const (
+	ProjectAps = "aps"
+	ProjectCrm = "crm"
+	ProjectSrm = "srm"
+	ProjectWms = "wms"
+)
+
+type Config struct {
+	Client         *kubernetes.Clientset // Kubernetes 瀹㈡埛绔�
+	Image          string                // 闀滃儚鍚嶇О
+	DBHost         string                // 鏁版嵁搴撳湴鍧�
+	DBName         string                //鏁版嵁搴撳悕绉�
+	HttpPort       int32                 // HTTP 绔彛
+	RpcPort        int32                 // RPC 绔彛
+	NameSpace      string                // Namespace
+	DeploymentName string                // Deployment 鍚嶇О
+	ServiceName    string                // Service 鍚嶇О
+	ALHost         string                // 绠楁硶Host
+	Host           string                // 鏈嶅姟Host
+	NodeID         string                // Nsq鑺傜偣ID
+	HttpNodePort   int32                 // HTTP nodePort绔彛
+	RpcNodePort    int32                 // RPC nodePort绔彛
+}
 
 func create_test() {
 	// 閰嶇疆 Kubernetes 闆嗙兢鐨� kubeconfig 璺緞
@@ -43,9 +70,16 @@
 
 	// 鍒涘缓澶氫釜 Namespace 涓嬬殑鐩稿悓鍚嶇О鐨� Deployment 鍜� Service
 	for _, ns := range namespaces {
-		err = CreateDeploymentAndService(clientset, ns, ns, ns)
+		err = CreateNamespace(clientset, ns)
 		if err != nil {
 			panic(err)
+		}
+		for _, proj := range Projects {
+			obj := GetCreateObj(proj)
+			err = obj.CreateDeploymentAndService(Config{Client: clientset, NameSpace: ns, DeploymentName: proj, ServiceName: proj}, nil)
+			if err != nil {
+				panic(err)
+			}
 		}
 
 		nodeport, err := GetServiceNodePort(clientset, ns, ns)
@@ -54,7 +88,22 @@
 		}
 
 		log.Printf("Service NodePort: %d\n", nodeport)
+
 	}
+}
+
+func GetCreateObj(proj string) Create {
+	switch proj {
+	case ProjectAps:
+		return &ApsCreate{}
+	case ProjectCrm:
+		return &CrmCreate{}
+	case ProjectSrm:
+		return &SrmCreate{}
+	case ProjectWms:
+		return &WmsCreate{}
+	}
+	return nil
 }
 
 // CheckAndDeleteResources 妫�娴嬪苟鍒犻櫎鎸囧畾鐨� Namespace銆丏eployment 鍜� Service
@@ -112,38 +161,8 @@
 	return nil
 }
 
-func CreateDeploymentAndService(clientset *kubernetes.Clientset, namespace, deploymentName, serviceName string) error {
-	fmt.Println("\033[1;37;40mCreating resources in Namespace:", namespace, "\033[0m")
-
-	// 妫�娴嬪苟鍒犻櫎宸插瓨鍦ㄧ殑 Deployment 鍜� Service
-	err := CheckAndDeleteResources(clientset, namespace, deploymentName, serviceName)
-	if err != nil {
-		return err
-	}
-
-	// 鍒涘缓 Namespace
-	err = createNamespace(clientset, namespace)
-	if err != nil {
-		return err
-	}
-
-	// 鍒涘缓 Deployment
-	err = createDeployment(clientset, namespace, deploymentName)
-	if err != nil {
-		return err
-	}
-
-	// 鍒涘缓 Service
-	err = createService(clientset, namespace, serviceName)
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-// createNamespace 鍒涘缓鎸囧畾鐨� Namespace
-func createNamespace(clientset *kubernetes.Clientset, namespace string) error {
+// CreateNamespace 鍒涘缓鎸囧畾鐨� Namespace
+func CreateNamespace(clientset *kubernetes.Clientset, namespace string) error {
 	log.Printf("Creating Namespace: %s\n", namespace)
 
 	ns := &apiv1.Namespace{
@@ -165,110 +184,25 @@
 	return nil
 }
 
-func createDeployment(clientset *kubernetes.Clientset, namespace, deploymentName string) error {
-	fmt.Println("\033[1;37;40mCreating Deployment:", deploymentName, "\033[0m")
-
-	deployment := &appsv1.Deployment{
-		ObjectMeta: metav1.ObjectMeta{
-			Name: deploymentName,
-		},
-		Spec: appsv1.DeploymentSpec{
-			Replicas: &replicas,
-			Selector: &metav1.LabelSelector{
-				MatchLabels: map[string]string{
-					"cid": namespace,
-				},
-			},
-			Template: apiv1.PodTemplateSpec{
-				ObjectMeta: metav1.ObjectMeta{
-					Labels: map[string]string{
-						"cid": namespace,
-					},
-				},
-				Spec: apiv1.PodSpec{
-					TopologySpreadConstraints: []apiv1.TopologySpreadConstraint{
-						{
-							MaxSkew:           1,
-							TopologyKey:       "kubernetes.io/hostname",
-							WhenUnsatisfiable: apiv1.DoNotSchedule,
-							LabelSelector: &metav1.LabelSelector{
-								MatchLabels: map[string]string{
-									"cid": namespace,
-								},
-							},
-						},
-					},
-					Containers: []apiv1.Container{
-						{
-							Name:  namespace,
-							Image: "192.168.20.119/apsserver/apsserver:v0.2",
-							Env: []apiv1.EnvVar{
-								{
-									Name:  "NODE_ID",
-									Value: namespace,
-								},
-							},
-						},
-					},
-				},
-			},
-		},
+func getTwoNodePort(client *kubernetes.Clientset) (nodePort1, nodePort2 int32, err error) {
+	nodePort1, err = getRandomNodePort(client)
+	if err != nil {
+		return 0, 0, err
 	}
 
-	_, err := clientset.AppsV1().Deployments(namespace).Create(context.TODO(), deployment, metav1.CreateOptions{})
-	if err != nil {
-		if !errors.IsAlreadyExists(err) {
-			return fmt.Errorf("failed to create Deployment: %v", err)
+	for {
+		nodePort2, err = getRandomNodePort(client)
+		if err != nil {
+			return 0, 0, err
 		}
-		fmt.Printf("Deployment %s already exists in Namespace %s\n", deploymentName, namespace)
-	} else {
-		fmt.Printf("Deployment %s created in Namespace %s\n", deploymentName, namespace)
-	}
 
-	return nil
-}
-
-// createService 鍒涘缓鎸囧畾鐨� Service
-func createService(clientset *kubernetes.Clientset, namespace, serviceName string) error {
-	fmt.Println("\033[1;37;40mCreating Service:", serviceName, "\033[0m")
-
-	nodePort, err := getRandomNodePort(clientset)
-	if err != nil {
-		return err
-	}
-
-	service := &apiv1.Service{
-		ObjectMeta: metav1.ObjectMeta{
-			Name: serviceName,
-		},
-		Spec: apiv1.ServiceSpec{
-			Selector: map[string]string{
-				"cid": namespace,
-			},
-			Type: apiv1.ServiceTypeNodePort,
-			Ports: []apiv1.ServicePort{
-				{
-					Name:       "http",
-					Protocol:   apiv1.ProtocolTCP,
-					Port:       port,
-					TargetPort: intstr.FromInt(int(port)),
-					NodePort:   nodePort,
-				},
-			},
-		},
-	}
-
-	_, err = clientset.CoreV1().Services(namespace).Create(context.TODO(), service, metav1.CreateOptions{})
-	if err != nil {
-		if !errors.IsAlreadyExists(err) {
-			return fmt.Errorf("failed to create Service: %v", err)
+		if nodePort2 != nodePort1 {
+			break
 		}
-		fmt.Printf("Service %s already exists in Namespace %s\n", serviceName, namespace)
-	} else {
-		fmt.Printf("Service %s created in Namespace %s\n", serviceName, namespace)
+
 	}
 
-	return nil
+	return nodePort1, nodePort2, nil
 }
 
 // getRandomNodePort 鑾峰彇涓�涓湭浣跨敤鐨勯殢鏈� NodePort
@@ -336,8 +270,227 @@
 
 	// 鑾峰彇绗竴涓鍙g殑 NodePort
 	if len(svc.Spec.Ports) > 0 {
-		return svc.Spec.Ports[0].NodePort, nil
+		for _, p := range svc.Spec.Ports {
+			// return tcp port
+			if p.Name == "http" {
+				return p.NodePort, nil
+			}
+		}
 	}
 
 	return 0, fmt.Errorf("no ports defined for Service %s", serviceName)
 }
+
+type Create interface {
+	CreateDeploymentAndService(config Config, extendEnv map[string]string) error
+	CreateEnv(config Config, pairs map[string]string) []apiv1.EnvVar
+	CreateDeployment(config Config, extendEnv map[string]string) error
+	CreateService(config Config) error
+	GetCid(config Config) string
+}
+
+type ApsCreate struct{}
+
+func (c *ApsCreate) CreateDeploymentAndService(config Config, extendEnv map[string]string) error {
+	fmt.Println("\033[1;37;40mCreating resources in Namespace:", config.NameSpace, "\033[0m")
+
+	// 妫�娴嬪苟鍒犻櫎宸插瓨鍦ㄧ殑 Deployment 鍜� Service
+	err := CheckAndDeleteResources(config.Client, config.NameSpace, config.DeploymentName, config.ServiceName)
+	if err != nil {
+		return err
+	}
+
+	config.HttpNodePort, config.RpcNodePort, err = getTwoNodePort(config.Client)
+	if err != nil {
+		return err
+	}
+
+	// 鍒涘缓 Deployment
+	err = c.CreateDeployment(config, extendEnv)
+	if err != nil {
+		return err
+	}
+
+	log.Printf("Waiting for Deployment %s to be ready...\n", config.DeploymentName)
+	// 鍒涘缓 Service
+	err = c.CreateService(config)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func (c *ApsCreate) CreateEnv(config Config, pairs map[string]string) []apiv1.EnvVar {
+	envs := []apiv1.EnvVar{
+		{
+			Name:  "GRPC_PORT",
+			Value: fmt.Sprint(config.RpcPort),
+		},
+		{
+			Name: "DB_HOST",
+			//ValueFrom: &apiv1.EnvVarSource{
+			//	FieldRef: &apiv1.ObjectFieldSelector{
+			//		APIVersion: "v1",
+			//		FieldPath:  "status.hostIP",
+			//	},
+			//},
+			Value: config.DBHost,
+		},
+		{
+			Name: "HOST",
+			ValueFrom: &apiv1.EnvVarSource{
+				FieldRef: &apiv1.ObjectFieldSelector{
+					APIVersion: "v1",
+					FieldPath:  "status.hostIP",
+				},
+			},
+		},
+		{
+			Name:  "AL_HOST",
+			Value: config.ALHost,
+		},
+		{
+			Name:  "GRPC_NODE_PORT",
+			Value: strconv.Itoa(int(config.RpcNodePort)),
+		},
+		{
+			Name:  "NODE_ID",
+			Value: config.NodeID,
+		},
+		{
+			Name:  "DB_NAME",
+			Value: config.NameSpace,
+		},
+		{
+			Name:  "DB_PORT",
+			Value: strconv.Itoa(3306),
+		},
+		{
+			Name:  "DB_USER",
+			Value: config.NameSpace,
+		},
+		{
+			Name:  "DB_PASSWD",
+			Value: config.NameSpace + "@Basic2023",
+		},
+	}
+
+	for name, value := range pairs {
+		envs = append(envs, apiv1.EnvVar{
+			Name:  name,
+			Value: value,
+		})
+	}
+	return envs
+}
+func (c *ApsCreate) CreateDeployment(config Config, extendEnv map[string]string) error {
+	fmt.Println("\033[1;37;40mCreating Deployment:", config.DeploymentName, "\033[0m")
+	log.Printf("Aps CreateDeployment config:%+v\n", config)
+	envs := c.CreateEnv(config, extendEnv)
+	deployment := &appsv1.Deployment{
+		ObjectMeta: metav1.ObjectMeta{
+			Name: config.DeploymentName,
+		},
+		Spec: appsv1.DeploymentSpec{
+			Replicas: &replicas,
+			Selector: &metav1.LabelSelector{
+				MatchLabels: map[string]string{
+					"cid": c.GetCid(config),
+				},
+			},
+			Template: apiv1.PodTemplateSpec{
+				ObjectMeta: metav1.ObjectMeta{
+					Labels: map[string]string{
+						"cid": c.GetCid(config),
+					},
+				},
+				Spec: apiv1.PodSpec{
+					TopologySpreadConstraints: []apiv1.TopologySpreadConstraint{
+						{
+							MaxSkew:           1,
+							TopologyKey:       "kubernetes.io/hostname",
+							WhenUnsatisfiable: apiv1.DoNotSchedule,
+							LabelSelector: &metav1.LabelSelector{
+								MatchLabels: map[string]string{
+									"cid": c.GetCid(config),
+								},
+							},
+						},
+					},
+					Containers: []apiv1.Container{
+						{
+							Name:            config.NameSpace,
+							Image:           config.Image,
+							Env:             envs,
+							ImagePullPolicy: apiv1.PullIfNotPresent, // 璁剧疆闀滃儚鎷夊彇绛栫暐涓� Always
+						},
+					},
+				},
+			},
+		},
+	}
+
+	_, err := config.Client.AppsV1().Deployments(config.NameSpace).Create(context.TODO(), deployment, metav1.CreateOptions{})
+	if err != nil {
+		if !errors.IsAlreadyExists(err) {
+			return fmt.Errorf("failed to create Deployment: %v", err)
+		}
+		fmt.Printf("Deployment %s already exists in Namespace %s\n", config.DeploymentName, config.NameSpace)
+	} else {
+		fmt.Printf("Deployment %s created in Namespace %s\n", config.DeploymentName, config.NameSpace)
+	}
+
+	return nil
+}
+
+// createService 鍒涘缓鎸囧畾鐨� Service
+func (c *ApsCreate) CreateService(config Config) error {
+	fmt.Println("\033[1;37;40mCreating Service:", config.ServiceName, "\033[0m")
+
+	service := &apiv1.Service{
+		ObjectMeta: metav1.ObjectMeta{
+			Name: config.ServiceName,
+		},
+		Spec: apiv1.ServiceSpec{
+			Selector: map[string]string{
+				"cid": c.GetCid(config),
+			},
+			Type: apiv1.ServiceTypeNodePort,
+			Ports: []apiv1.ServicePort{
+				{
+					Name:       "http",
+					Protocol:   apiv1.ProtocolTCP,
+					Port:       config.HttpPort,                      // 闆嗙兢鍐呴儴璁块棶绔彛
+					TargetPort: intstr.FromInt(int(config.HttpPort)), // 瀹瑰櫒瀵瑰绔彛
+					NodePort:   config.HttpNodePort,                  // 澶栭儴璁块棶绔彛
+				},
+				{
+					Name:       "tcp",
+					Protocol:   apiv1.ProtocolTCP,
+					Port:       config.RpcPort,
+					TargetPort: intstr.FromInt(int(config.RpcPort)),
+					NodePort:   config.RpcNodePort,
+				},
+			},
+			SessionAffinity: apiv1.ServiceAffinityClientIP,
+		},
+	}
+
+	_, err := config.Client.CoreV1().Services(config.NameSpace).Create(context.TODO(), service, metav1.CreateOptions{})
+
+	if err != nil {
+		if !errors.IsAlreadyExists(err) {
+			return fmt.Errorf("failed to create Service: %v", err)
+		}
+		log.Printf("Service %s already exists in Namespace %s\n", config.ServiceName, config.NameSpace)
+	} else {
+		log.Printf("Service %s created in Namespace %s\n", config.ServiceName, config.NameSpace)
+	}
+
+	return nil
+}
+
+func (c *ApsCreate) GetCid(config Config) string {
+	return fmt.Sprintf("%v-%v", config.NameSpace, "aps")
+}

--
Gitblit v1.8.0