From 0ade888a16e243f6d22351a65013229e538a54a9 Mon Sep 17 00:00:00 2001
From: wangpengfei <274878379@qq.com>
Date: 星期日, 04 六月 2023 20:19:08 +0800
Subject: [PATCH] tmp

---
 src/k8s/create.go |   39 +++++++++++++++++++++++++++------------
 1 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/src/k8s/create.go b/src/k8s/create.go
index 12b995c..0e6a08e 100644
--- a/src/k8s/create.go
+++ b/src/k8s/create.go
@@ -22,6 +22,7 @@
 var (
 	replicas      int32 = 3
 	port          int32 = 9081
+	rpcPort       int32 = 9082
 	namespaces          = []string{"guangsheng", "geruimi", "tongsheng"}
 	usedNodePorts       = make(map[int32]bool)
 )
@@ -134,6 +135,7 @@
 		return err
 	}
 
+	log.Printf("Waiting for Deployment %s to be ready...\n", deploymentName)
 	// 鍒涘缓 Service
 	err = createService(clientset, namespace, serviceName)
 	if err != nil {
@@ -202,7 +204,7 @@
 					Containers: []apiv1.Container{
 						{
 							Name:  namespace,
-							Image: "192.168.20.119/apsserver/apsserver:v0.2",
+							Image: "192.168.20.119/apsserver/apsserver:v0.5",
 							Env: []apiv1.EnvVar{
 								{
 									Name:  "NODE_ID",
@@ -253,10 +255,10 @@
 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
-	}
+	//nodePort, err := getRandomNodePort(clientset)
+	//if err != nil {
+	//	return err
+	//}
 
 	service := &apiv1.Service{
 		ObjectMeta: metav1.ObjectMeta{
@@ -271,22 +273,30 @@
 				{
 					Name:       "http",
 					Protocol:   apiv1.ProtocolTCP,
-					Port:       port,
-					TargetPort: intstr.FromInt(int(port)),
-					NodePort:   nodePort,
+					Port:       port,                      // 闆嗙兢鍐呴儴璁块棶绔彛
+					TargetPort: intstr.FromInt(int(port)), // 瀹瑰櫒瀵瑰绔彛
+					NodePort:   0,                         // 澶栭儴璁块棶绔彛
+				},
+				{
+					Name:       "tcp",
+					Protocol:   apiv1.ProtocolTCP,
+					Port:       rpcPort,
+					TargetPort: intstr.FromInt(int(rpcPort)),
+					NodePort:   0,
 				},
 			},
 		},
 	}
 
-	_, err = clientset.CoreV1().Services(namespace).Create(context.TODO(), service, metav1.CreateOptions{})
+	_, 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)
 		}
-		fmt.Printf("Service %s already exists in Namespace %s\n", serviceName, namespace)
+		log.Printf("Service %s already exists in Namespace %s\n", serviceName, namespace)
 	} else {
-		fmt.Printf("Service %s created in Namespace %s\n", serviceName, namespace)
+		log.Printf("Service %s created in Namespace %s\n", serviceName, namespace)
 	}
 
 	return nil
@@ -357,7 +367,12 @@
 
 	// 鑾峰彇绗竴涓鍙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 == "tcp" {
+				return p.NodePort, nil
+			}
+		}
 	}
 
 	return 0, fmt.Errorf("no ports defined for Service %s", serviceName)

--
Gitblit v1.8.0