Kaynağa Gözat

move parse yaml back to update endpoint (#4422)

ianedwards 2 yıl önce
ebeveyn
işleme
195f5ed159

+ 2 - 0
api/client/porter_app.go

@@ -221,6 +221,7 @@ type UpdateAppInput struct {
 	Variables            map[string]string
 	Secrets              map[string]string
 	Deletions            porter_app.Deletions
+	PatchOperations      []v2.PatchOperation
 }
 
 // UpdateApp updates a porter app
@@ -246,6 +247,7 @@ func (c *Client) UpdateApp(
 		Variables:            inp.Variables,
 		Secrets:              inp.Secrets,
 		Deletions:            inp.Deletions,
+		PatchOperations:      inp.PatchOperations,
 	}
 
 	err := c.postRequest(

+ 10 - 1
api/server/handlers/porter_app/update_app.go

@@ -16,6 +16,7 @@ import (
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/internal/models"
 	"github.com/porter-dev/porter/internal/porter_app"
+	v2 "github.com/porter-dev/porter/internal/porter_app/v2"
 	"github.com/porter-dev/porter/internal/telemetry"
 )
 
@@ -91,6 +92,8 @@ type UpdateAppRequest struct {
 	Base64AddonProtos []string `json:"b64_addon_protos"`
 	// Base64PorterYAML is a base64 encoded porter yaml to apply representing a potentially partial porter app contract
 	Base64PorterYAML string `json:"b64_porter_yaml"`
+	// PatchOperations is a set of patch operations to apply to the porter.yaml if specified
+	PatchOperations []v2.PatchOperation `json:"patch_operations"`
 	// IsEnvOverride is used to remove any variables that are not specified in the request.  If false, the request will only update the variables specified in the request,
 	// and leave all other variables untouched.
 	IsEnvOverride bool `json:"is_env_override"`
@@ -209,7 +212,13 @@ func (c *UpdateAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
 			return
 		}
-		appProto = appFromYaml.AppProto
+
+		appProto, err = v2.PatchApp(ctx, appFromYaml.AppProto, request.PatchOperations)
+		if err != nil {
+			err := telemetry.Error(ctx, span, err, "error patching app proto")
+			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
+			return
+		}
 
 		// only public variables can be defined in porter.yaml
 		envVariables = mergeEnvVariables(request.Variables, appFromYaml.EnvVariables)

+ 2 - 29
cli/cmd/v2/apply.go

@@ -15,9 +15,7 @@ import (
 
 	"github.com/fatih/color"
 	app_api "github.com/porter-dev/porter/api/server/handlers/porter_app"
-	"github.com/porter-dev/porter/internal/porter_app"
 	v2 "github.com/porter-dev/porter/internal/porter_app/v2"
-	"gopkg.in/yaml.v3"
 
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/internal/models"
@@ -121,36 +119,12 @@ func Apply(ctx context.Context, inp ApplyInput) error {
 		color.New(color.FgGreen).Printf("Using Porter YAML at path: %s\n", inp.PorterYamlPath) // nolint:errcheck,gosec
 	}
 
-	if b64YAML == "" {
-		color.New(color.FgGreen).Printf("No Porter YAML found, using default configuration...\n") // nolint:errcheck,gosec
-		if inp.AppName == "" {
-			return errors.New("no porter yaml found and app name not specified")
-		}
-
-		app := v2.PorterApp{
-			Version: string(porter_app.PorterYamlVersion_V2),
-			Name:    inp.AppName,
-		}
-
-		by, err := yaml.Marshal(app)
-		if err != nil {
-			return fmt.Errorf("error marshaling default porter yaml: %w", err)
-		}
-
-		b64YAML = base64.StdEncoding.EncodeToString(by)
-	}
-
 	commitSHA := commitSHAFromEnv()
 	gitSource, err := gitSourceFromEnv()
 	if err != nil {
 		return fmt.Errorf("error getting git source from env: %w", err)
 	}
 
-	parseRes, err := client.ParseYAML(ctx, cliConf.Project, cliConf.Cluster, b64YAML, inp.AppName, inp.PatchOperations)
-	if err != nil {
-		return fmt.Errorf("error parsing porter yaml: %w", err)
-	}
-
 	updateInput := api.UpdateAppInput{
 		ProjectID:          cliConf.Project,
 		ClusterID:          cliConf.Cluster,
@@ -159,11 +133,10 @@ func Apply(ctx context.Context, inp ApplyInput) error {
 		GitSource:          gitSource,
 		DeploymentTargetId: deploymentTargetID,
 		CommitSHA:          commitSHA,
-		Base64AppProto:     parseRes.B64AppProto,
-		Variables:          parseRes.EnvVariables,
-		Secrets:            parseRes.EnvSecrets,
+		Base64PorterYAML:   b64YAML,
 		WithPredeploy:      inp.WithPredeploy,
 		Exact:              inp.Exact,
+		PatchOperations:    inp.PatchOperations,
 	}
 
 	updateResp, err := client.UpdateApp(ctx, updateInput)