Browse Source

add env groups support to preview env

Mohammed Nafees 4 năm trước cách đây
mục cha
commit
0950929f24
4 tập tin đã thay đổi với 94 bổ sung1 xóa
  1. 3 0
      cli/cmd/apply.go
  2. 7 0
      cli/cmd/deploy/create.go
  3. 4 1
      cli/cmd/deploy/deploy.go
  4. 80 0
      cli/cmd/deploy/shared.go

+ 3 - 0
cli/cmd/apply.go

@@ -143,6 +143,8 @@ type ApplicationConfig struct {
 		Buildpacks []string
 	}
 
+	EnvGroups []string
+
 	Values map[string]interface{}
 }
 
@@ -310,6 +312,7 @@ func (d *Driver) applyApplication(resource *models.Resource, client *api.Client,
 		LocalDockerfile: appConfig.Build.Dockerfile,
 		OverrideTag:     tag,
 		Method:          deploy.DeployBuildType(method),
+		EnvGroups:       appConfig.EnvGroups,
 	}
 
 	if shouldCreate {

+ 7 - 0
cli/cmd/deploy/create.go

@@ -478,6 +478,13 @@ func (c *CreateAgent) getMergedValues(overrideValues map[string]interface{}) (st
 		return "", nil, err
 	}
 
+	err = coalesceEnvGroups(c.Client, c.CreateOpts.ProjectID, c.CreateOpts.ClusterID,
+		c.CreateOpts.Namespace, c.CreateOpts.EnvGroups, values)
+
+	if err != nil {
+		return "", nil, err
+	}
+
 	// merge existing values with overriding values
 	mergedValues := utils.CoalesceValues(values, overrideValues)
 

+ 4 - 1
cli/cmd/deploy/deploy.go

@@ -137,7 +137,10 @@ func NewDeployAgent(client *client.Client, app string, opts *DeployOpts) (*Deplo
 
 	deployAgent.tag = opts.OverrideTag
 
-	return deployAgent, nil
+	err = coalesceEnvGroups(deployAgent.client, deployAgent.opts.ProjectID, deployAgent.opts.ClusterID,
+		deployAgent.opts.Namespace, deployAgent.opts.EnvGroups, deployAgent.release.Config)
+
+	return deployAgent, err
 }
 
 type GetBuildEnvOpts struct {

+ 80 - 0
cli/cmd/deploy/shared.go

@@ -1,5 +1,14 @@
 package deploy
 
+import (
+	"context"
+	"strconv"
+	"strings"
+
+	api "github.com/porter-dev/porter/api/client"
+	"github.com/porter-dev/porter/api/types"
+)
+
 // SharedOpts are common options for build, create, and deploy agents
 type SharedOpts struct {
 	ProjectID       uint
@@ -10,4 +19,75 @@ type SharedOpts struct {
 	OverrideTag     string
 	Method          DeployBuildType
 	AdditionalEnv   map[string]string
+	EnvGroups       []string
+}
+
+func getEnvGroupNameVersion(group string) (string, uint, error) {
+	if !strings.Contains(group, "@") {
+		return group, 0, nil
+	}
+
+	envGroupSpl := strings.Split(group, "@")
+	envGroupName := envGroupSpl[0]
+	envGroupVersion := uint64(0)
+
+	envGroupVersion, err := strconv.ParseUint(envGroupSpl[1], 10, 32)
+
+	if err != nil {
+		return "", 0, err
+	}
+
+	return envGroupName, uint(envGroupVersion), nil
+}
+
+func coalesceEnvGroups(
+	client *api.Client,
+	projectID, clusterID uint,
+	namespace string,
+	envGroups []string,
+	config map[string]interface{},
+) error {
+	for _, group := range envGroups {
+		if group == "" {
+			continue
+		}
+
+		envGroupName, envGroupVersion, err := getEnvGroupNameVersion(group)
+
+		if err != nil {
+			return err
+		}
+
+		envGroup, err := client.GetEnvGroup(
+			context.Background(),
+			projectID,
+			clusterID,
+			namespace,
+			&types.GetEnvGroupRequest{
+				Name:    envGroupName,
+				Version: envGroupVersion,
+			},
+		)
+
+		if err != nil {
+			return err
+		}
+
+		envConfig, err := getNestedMap(config, "container", "env", "normal")
+
+		if err != nil || envConfig == nil {
+			envConfig = make(map[string]interface{})
+		}
+
+		for k, v := range envGroup.Variables {
+			envConfig[k] = v
+		}
+
+		containerMap, _ := config["container"].(map[string]interface{})
+		envMap, _ := containerMap["env"].(map[string]interface{})
+
+		envMap["normal"] = envConfig
+	}
+
+	return nil
 }