zhangqian
2023-12-26 0e22b2dfe7a24c528fb8ac58ece089bd032f5478
src/k8s/create.go
@@ -39,6 +39,7 @@
   ServiceName    string                // Service 名称
   ALHost         string                // 算法Host
   Host           string                // 服务Host
   NodeID         string                // Nsq节点ID
}
func create_test() {
@@ -59,7 +60,7 @@
   // 创建多个 Namespace 下的相同名称的 Deployment 和 Service
   for _, ns := range namespaces {
      err = CreateDeploymentAndService(Config{Client: clientset, NameSpace: ns, DeploymentName: ns, ServiceName: ns})
      err = CreateDeploymentAndService(Config{Client: clientset, NameSpace: ns, DeploymentName: ns, ServiceName: ns}, nil)
      if err != nil {
         panic(err)
      }
@@ -128,7 +129,7 @@
   return nil
}
func CreateDeploymentAndService(config Config) error {
func CreateDeploymentAndService(config Config, extendEnvs map[string]string) error {
   fmt.Println("\033[1;37;40mCreating resources in Namespace:", config.NameSpace, "\033[0m")
   // 检测并删除已存在的 Deployment 和 Service
@@ -143,7 +144,7 @@
   }
   // 创建 Deployment
   err = createDeployment(config)
   err = createDeployment(config, extendEnvs)
   if err != nil {
      return err
   }
@@ -180,10 +181,82 @@
   return nil
}
func 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",
            },
         },
      },
      {
         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",
      },
   }
func createDeployment(config Config) error {
   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,
         Value: value,
      })
   }
   return envs
}
func createDeployment(config Config, extendEnvs map[string]string) error {
   fmt.Println("\033[1;37;40mCreating Deployment:", config.DeploymentName, "\033[0m")
   envs := createEnv(config, extendEnvs)
   deployment := &appsv1.Deployment{
      ObjectMeta: metav1.ObjectMeta{
         Name: config.DeploymentName,
@@ -216,52 +289,9 @@
               },
               Containers: []apiv1.Container{
                  {
                     Name:  config.NameSpace,
                     Image: config.Image,
                     Env: []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:  "AL_HOST",
                           Value: config.ALHost,
                        },
                        {
                           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",
                        },
                     },
                     Name:            config.NameSpace,
                     Image:           config.Image,
                     Env:             envs,
                     ImagePullPolicy: apiv1.PullAlways, // 设置镜像拉取策略为 Always
                  },
               },