From a2667454aa39ea72b0e3660dca7dc4f468d712a2 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期二, 26 十二月 2023 14:41:15 +0800 Subject: [PATCH] 更改环境变量,增加NODE_ID --- src/k8s/create.go | 106 +++++++++++++++++++++++++++++++---------------------- 1 files changed, 62 insertions(+), 44 deletions(-) diff --git a/src/k8s/create.go b/src/k8s/create.go index fc089ee..ae36b1c 100644 --- a/src/k8s/create.go +++ b/src/k8s/create.go @@ -31,6 +31,7 @@ Client *kubernetes.Clientset // Kubernetes 瀹㈡埛绔� Image string // 闀滃儚鍚嶇О DBHost string // 鏁版嵁搴撳湴鍧� + DBName string //鏁版嵁搴撳悕绉� HttpPort int32 // HTTP 绔彛 RpcPort int32 // RPC 绔彛 NameSpace string // Namespace @@ -38,6 +39,7 @@ ServiceName string // Service 鍚嶇О ALHost string // 绠楁硶Host Host string // 鏈嶅姟Host + NodeID string // Nsq鑺傜偣ID } func create_test() { @@ -136,12 +138,6 @@ return err } - // 鍒涘缓 Namespace - err = createNamespace(config.Client, config.NameSpace) - if err != nil { - return err - } - config.HttpPort, config.RpcPort, err = getTwoNodePort(config.Client) if err != nil { return err @@ -163,8 +159,8 @@ 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{ @@ -188,6 +184,61 @@ func createDeployment(config Config) error { fmt.Println("\033[1;37;40mCreating Deployment:", config.DeploymentName, "\033[0m") + + 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", + }, + }, + }, + { + Name: "HOST", + ValueFrom: &apiv1.EnvVarSource{ + FieldRef: &apiv1.ObjectFieldSelector{ + APIVersion: "v1", + FieldPath: "status.hostIP", + }, + }, + }, + { + Name: "DB_NAME", + Value: config.DBName, + }, + { + Name: "DB_PORT", + Value: strconv.Itoa(6446), + }, + { + Name: "DB_USER", + Value: config.NameSpace, + }, + { + Name: "DB_PASSWD", + Value: config.NameSpace + "@Basic2023", + }, + } + + 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, + }) + } deployment := &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ @@ -221,42 +272,9 @@ }, Containers: []apiv1.Container{ { - Name: config.NameSpace, - Image: config.Image, - Env: []apiv1.EnvVar{ - { - Name: "GRPC_PORT", - Value: fmt.Sprint(config.RpcPort), - }, - { - Name: "DB_HOST", - Value: config.DBHost, - }, - { - Name: "HOST", - Value: config.Host, - }, - { - Name: "AL_HOST", - Value: config.ALHost, - }, - { - 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", - }, - }, + Name: config.NameSpace, + Image: config.Image, + Env: envs, ImagePullPolicy: apiv1.PullAlways, // 璁剧疆闀滃儚鎷夊彇绛栫暐涓� Always }, }, -- Gitblit v1.8.0