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_srm.go | 89 +++++++++++++++++++++++++++-----------------
1 files changed, 54 insertions(+), 35 deletions(-)
diff --git a/src/k8s/create_srm.go b/src/k8s/create_srm.go
index 851b27e..ff8f417 100644
--- a/src/k8s/create_srm.go
+++ b/src/k8s/create_srm.go
@@ -23,7 +23,7 @@
return err
}
- config.HttpPort, err = getRandomNodePort(config.Client)
+ config.HttpNodePort, err = getRandomNodePort(config.Client)
if err != nil {
return err
}
@@ -47,13 +47,8 @@
func (c *SrmCreate) CreateEnv(config Config, pairs map[string]string) []apiv1.EnvVar {
envs := []apiv1.EnvVar{
{
- Name: "DB_HOST",
- ValueFrom: &apiv1.EnvVarSource{
- FieldRef: &apiv1.ObjectFieldSelector{
- APIVersion: "v1",
- FieldPath: "status.hostIP",
- },
- },
+ Name: "DB_HOST",
+ Value: config.DBHost,
},
{
Name: "DB_NAME",
@@ -71,30 +66,21 @@
Name: "DB_PASSWD",
Value: config.NameSpace + "@Basic2023",
},
+ {
+ Name: "HOST",
+ ValueFrom: &apiv1.EnvVarSource{
+ FieldRef: &apiv1.ObjectFieldSelector{
+ APIVersion: "v1",
+ FieldPath: "status.hostIP",
+ },
+ },
+ },
+ {
+ Name: "ADMIN_GRPC",
+ Value: "$(HOST):50051",
+ },
}
- if config.ALHost != "" {
- envs = append(envs, apiv1.EnvVar{
- Name: "AL_HOST",
- Value: config.ALHost,
- })
- }
-
- if config.NodeID != "" {
- envs = append(envs, apiv1.EnvVar{
- Name: "NODE_ID",
- Value: config.NodeID,
- })
- }
-
- if len(pairs) > 0 {
- for name, value := range pairs {
- envs = append(envs, apiv1.EnvVar{
- Name: name,
- Value: value,
- })
- }
- }
for name, value := range pairs {
envs = append(envs, apiv1.EnvVar{
Name: name,
@@ -165,11 +151,12 @@
// createService 鍒涘缓鎸囧畾鐨� Service
func (c *SrmCreate) CreateService(config Config) error {
- fmt.Println("\033[1;37;40mCreating Service:", config.ServiceName, "\033[0m")
+ httpServiceName := config.ServiceName + "-http"
+ fmt.Println("\033[1;37;40mCreating Service:", httpServiceName, "\033[0m")
service := &apiv1.Service{
ObjectMeta: metav1.ObjectMeta{
- Name: config.ServiceName,
+ Name: httpServiceName,
},
Spec: apiv1.ServiceSpec{
Selector: map[string]string{
@@ -180,9 +167,9 @@
{
Name: "http",
Protocol: apiv1.ProtocolTCP,
- Port: port, // 闆嗙兢鍐呴儴璁块棶绔彛
- TargetPort: intstr.FromInt(int(port)), // 瀹瑰櫒瀵瑰绔彛
- NodePort: config.HttpPort, // 澶栭儴璁块棶绔彛
+ Port: config.HttpPort, // 闆嗙兢鍐呴儴璁块棶绔彛
+ TargetPort: intstr.FromInt(int(config.HttpPort)), // 瀹瑰櫒瀵瑰绔彛
+ NodePort: config.HttpNodePort, // 澶栭儴璁块棶绔彛
},
},
SessionAffinity: apiv1.ServiceAffinityClientIP,
@@ -195,6 +182,38 @@
if !errors.IsAlreadyExists(err) {
return fmt.Errorf("failed to create Service: %v", err)
}
+ log.Printf("Service %s already exists in Namespace %s\n", httpServiceName, config.NameSpace)
+ } else {
+ log.Printf("Service %s created in Namespace %s\n", httpServiceName, config.NameSpace)
+ }
+
+ clusterIPService := &apiv1.Service{
+ ObjectMeta: metav1.ObjectMeta{
+ Name: config.ServiceName,
+ },
+ Spec: apiv1.ServiceSpec{
+ Selector: map[string]string{
+ "cid": c.GetCid(config),
+ },
+ Type: apiv1.ServiceTypeClusterIP,
+ Ports: []apiv1.ServicePort{
+ {
+ Name: "grpc",
+ Protocol: apiv1.ProtocolTCP,
+ Port: config.RpcPort, // 闆嗙兢鍐呴儴璁块棶绔彛
+ TargetPort: intstr.FromInt(int(config.RpcPort)), // 瀹瑰櫒瀵瑰绔彛
+ },
+ },
+ SessionAffinity: apiv1.ServiceAffinityClientIP,
+ },
+ }
+
+ _, err = config.Client.CoreV1().Services(config.NameSpace).Create(context.TODO(), clusterIPService, metav1.CreateOptions{})
+
+ if err != nil {
+ if !errors.IsAlreadyExists(err) {
+ return fmt.Errorf("failed to create clusterIP 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)
--
Gitblit v1.8.0