Răsfoiți Sursa

process variables, os-env, and env groups for porter.yaml v2

Mohammed Nafees 3 ani în urmă
părinte
comite
acaff7c25a

+ 8 - 8
api/server/handlers/namespace/clone_env_group.go

@@ -50,12 +50,12 @@ func (c *CloneEnvGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 		return
 	}
 
-	cm, _, err := agent.GetLatestVersionedConfigMap(request.Name, namespace)
+	cm, _, err := agent.GetLatestVersionedConfigMap(request.SourceName, namespace)
 
 	if err != nil {
 		if errors.Is(err, kubernetes.IsNotFoundError) {
 			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
-				fmt.Errorf("error cloning env group: envgroup %s in namespace %s not found", request.Name, namespace), http.StatusNotFound,
+				fmt.Errorf("error cloning env group: envgroup %s in namespace %s not found", request.SourceName, namespace), http.StatusNotFound,
 				"no config map found for envgroup",
 			))
 			return
@@ -65,12 +65,12 @@ func (c *CloneEnvGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 		return
 	}
 
-	secret, _, err := agent.GetLatestVersionedSecret(request.Name, namespace)
+	secret, _, err := agent.GetLatestVersionedSecret(request.SourceName, namespace)
 
 	if err != nil {
 		if errors.Is(err, kubernetes.IsNotFoundError) {
 			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
-				fmt.Errorf("error cloning env group: envgroup %s in namespace %s not found", request.Name, namespace), http.StatusNotFound,
+				fmt.Errorf("error cloning env group: envgroup %s in namespace %s not found", request.SourceName, namespace), http.StatusNotFound,
 				"no k8s secret found for envgroup",
 			))
 			return
@@ -80,8 +80,8 @@ func (c *CloneEnvGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 		return
 	}
 
-	if request.CloneName == "" {
-		request.CloneName = request.Name
+	if request.TargetName == "" {
+		request.TargetName = request.SourceName
 	}
 
 	vars := make(map[string]string)
@@ -98,8 +98,8 @@ func (c *CloneEnvGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 	}
 
 	configMap, err := envgroup.CreateEnvGroup(agent, types.ConfigMapInput{
-		Name:            request.CloneName,
-		Namespace:       request.Namespace,
+		Name:            request.TargetName,
+		Namespace:       request.TargetNamespace,
 		Variables:       vars,
 		SecretVariables: secretVars,
 	})

+ 4 - 4
api/types/namespace.go

@@ -134,10 +134,10 @@ type GetEnvGroupRequest struct {
 }
 
 type CloneEnvGroupRequest struct {
-	Namespace string `json:"namespace" form:"required"`
-	Name      string `json:"name" form:"required,dns1123"`
-	CloneName string `json:"clone_name,dns1123"`
-	Version   uint   `json:"version"`
+	TargetNamespace string `json:"namespace" form:"required"`
+	SourceName      string `json:"name" form:"required,dns1123"`
+	TargetName      string `json:"clone_name" form:"dns1123"`
+	SourceVersion   uint   `json:"version"`
 }
 
 type GetEnvGroupAllRequest struct {

+ 2 - 2
cli/cmd/apply.go

@@ -1129,8 +1129,8 @@ func (t *CloneEnvGroupHook) PreApply() error {
 					_, err = t.client.CloneEnvGroup(
 						context.Background(), target.Project, target.Cluster, group.Namespace,
 						&types.CloneEnvGroupRequest{
-							Name:      group.Name,
-							Namespace: target.Namespace,
+							SourceName:      group.Name,
+							TargetNamespace: target.Namespace,
 						},
 					)
 

+ 99 - 1
cli/cmd/preview/v2/apply.go

@@ -3,6 +3,8 @@ package v2
 import (
 	"context"
 	"fmt"
+	"os"
+	"strings"
 
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
@@ -24,6 +26,10 @@ type PreviewApplier struct {
 	rawBytes  []byte
 	namespace string
 	parsed    *types.ParsedPorterYAML
+
+	variablesMap map[string]string
+	osEnv        map[string]string
+	envGroups    map[string]*apiTypes.EnvGroup
 }
 
 func NewApplier(client *api.Client, raw []byte, namespace string) (*PreviewApplier, error) {
@@ -48,12 +54,102 @@ func NewApplier(client *api.Client, raw []byte, namespace string) (*PreviewAppli
 }
 
 func (a *PreviewApplier) Apply() error {
-	err := a.processVariables()
+	err := a.readOSEnv()
+
+	if err != nil {
+		return err
+	}
+
+	err = a.processVariables()
 
 	if err != nil {
 		return err
 	}
 
+	err = a.processEnvGroups()
+
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func (a *PreviewApplier) readOSEnv() error {
+	color.New(color.FgBlue).Println("[porter.yaml] Reading OS environment variables") // FIXME: use a scoped logger
+
+	env := os.Environ()
+	osEnv := make(map[string]string)
+
+	for _, e := range env {
+		k, v, _ := strings.Cut(e, "=")
+
+		if k != "" && v != "" {
+			// we only read in env variables that start with PORTER_APPLY_
+			k = strings.ReplaceAll(k, "PORTER_APPLY_", "")
+
+			osEnv[k] = v
+		}
+	}
+
+	a.osEnv = osEnv
+
+	return nil
+}
+
+func (a *PreviewApplier) processEnvGroups() error {
+	for _, eg := range a.parsed.PorterYAML.EnvGroups.GetValue() {
+		envGroup, err := a.apiClient.GetEnvGroup(
+			context.Background(),
+			config.GetCLIConfig().Project,
+			config.GetCLIConfig().Cluster,
+			a.namespace,
+			&apiTypes.GetEnvGroupRequest{
+				Name: eg.Name.GetValue(),
+			},
+		)
+
+		if err != nil && strings.Contains(err.Error(), "env group not found") {
+			cloneFrom := strings.Split(eg.CloneFrom.GetValue(), "/")
+
+			if len(cloneFrom) != 2 {
+				// this should not happen
+				return fmt.Errorf("internal error: please let the Porter team know about this and quote the following "+
+					"error:\n-----\nERROR: invalid env group clone_from format: %s", eg.CloneFrom.GetValue())
+			}
+
+			// clone the env group
+			envGroup, err := a.apiClient.CloneEnvGroup(
+				context.Background(),
+				config.GetCLIConfig().Project,
+				config.GetCLIConfig().Cluster,
+				cloneFrom[0],
+				&apiTypes.CloneEnvGroupRequest{
+					SourceName:      cloneFrom[1],
+					TargetNamespace: a.namespace,
+					TargetName:      eg.Name.GetValue(),
+				},
+			)
+
+			if err != nil {
+				return fmt.Errorf("error cloning env group '%s' from '%s': %w", eg.Name.GetValue(),
+					eg.CloneFrom.GetValue(), err)
+			}
+
+			a.envGroups[eg.Name.GetValue()] = &apiTypes.EnvGroup{
+				Name:      envGroup.Name,
+				Variables: envGroup.Variables,
+			}
+		} else if err != nil {
+			return fmt.Errorf("error checking for env group '%s': %w", eg.Name.GetValue(), err)
+		} else {
+			a.envGroups[eg.Name.GetValue()] = &apiTypes.EnvGroup{
+				Name:      envGroup.Name,
+				Variables: envGroup.Variables,
+			}
+		}
+	}
+
 	return nil
 }
 
@@ -125,6 +221,8 @@ func (a *PreviewApplier) processVariables() error {
 		}
 	}
 
+	a.variablesMap = variablesMap
+
 	return nil
 }