Răsfoiți Sursa

Avoiding panics in deploy stack flow (#2962)

Feroze Mohideen 3 ani în urmă
părinte
comite
692e8616c1
3 a modificat fișierele cu 34 adăugiri și 13 ștergeri
  1. 24 7
      api/server/handlers/stacks/parse.go
  2. 1 1
      cli/cmd/apply.go
  3. 9 5
      cli/cmd/stack/hooks.go

+ 24 - 7
api/server/handlers/stacks/parse.go

@@ -2,6 +2,7 @@ package stacks
 
 import (
 	"fmt"
+	"strings"
 
 	"github.com/porter-dev/porter/api/server/shared/config"
 	"github.com/porter-dev/porter/api/types"
@@ -60,7 +61,8 @@ func buildStackValues(parsed *PorterStackYAML, imageInfo *types.ImageInfo) (map[
 	values := make(map[string]interface{})
 
 	for name, app := range parsed.Apps {
-		defaultValues := getDefaultValues(app, parsed.Env)
+		appType := getType(name, app)
+		defaultValues := getDefaultValues(app, parsed.Env, appType)
 		helm_values := utils.CoalesceValues(defaultValues, app.Config)
 		values[name] = helm_values
 		if imageInfo != nil {
@@ -76,15 +78,29 @@ func buildStackValues(parsed *PorterStackYAML, imageInfo *types.ImageInfo) (map[
 	return values, nil
 }
 
-func getDefaultValues(app *App, env map[string]string) map[string]interface{} {
+func getType(name string, app *App) string {
+	if app.Type != nil {
+		return *app.Type
+	}
+	if strings.Contains(name, "web") {
+		return "web"
+	}
+	return "worker"
+}
+
+func getDefaultValues(app *App, env map[string]string, appType string) map[string]interface{} {
 	var defaultValues map[string]interface{}
-	if *app.Type == "web" {
+	var runCommand string
+	if app.Run != nil {
+		runCommand = *app.Run
+	}
+	if appType == "web" {
 		defaultValues = map[string]interface{}{
 			"ingress": map[string]interface{}{
 				"enabled": false,
 			},
 			"container": map[string]interface{}{
-				"command": *app.Run,
+				"command": runCommand,
 				"env": map[string]interface{}{
 					"normal": CopyEnv(env),
 				},
@@ -93,7 +109,7 @@ func getDefaultValues(app *App, env map[string]string) map[string]interface{} {
 	} else {
 		defaultValues = map[string]interface{}{
 			"container": map[string]interface{}{
-				"command": *app.Run,
+				"command": runCommand,
 				"env": map[string]interface{}{
 					"normal": CopyEnv(env),
 				},
@@ -107,13 +123,14 @@ func buildStackChart(parsed *PorterStackYAML, config *config.Config, projectID u
 	deps := make([]*chart.Dependency, 0)
 
 	for alias, app := range parsed.Apps {
+		appType := getType(alias, app)
 		selectedRepo := config.ServerConf.DefaultApplicationHelmRepoURL
-		selectedVersion, err := getLatestTemplateVersion(*app.Type, config, projectID)
+		selectedVersion, err := getLatestTemplateVersion(appType, config, projectID)
 		if err != nil {
 			return nil, err
 		}
 		deps = append(deps, &chart.Dependency{
-			Name:       *app.Type,
+			Name:       appType,
 			Alias:      alias,
 			Version:    selectedVersion,
 			Repository: selectedRepo,

+ 1 - 1
cli/cmd/apply.go

@@ -151,7 +151,7 @@ func apply(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string
 		if err != nil {
 			return fmt.Errorf("error parsing porter.yaml: %w", err)
 		}
-	} else if previewVersion.Version == "v1stack" {
+	} else if previewVersion.Version == "v1stack" || previewVersion.Version == "" {
 		stackName := os.Getenv("PORTER_STACK_NAME")
 		if stackName == "" {
 			return fmt.Errorf("environment variable PORTER_STACK_NAME must be set")

+ 9 - 5
cli/cmd/stack/hooks.go

@@ -63,12 +63,16 @@ func (t *DeployStackHook) PostApply(driverOutput map[string]interface{}) error {
 func (t *DeployStackHook) applyStack(client *api.Client, shouldCreate bool, driverOutput map[string]interface{}) error {
 	var imageInfo types.ImageInfo
 	image, ok := driverOutput["image"].(string)
-	if ok && image != "" {
-		// split image into image-path:tag format
+	// if it contains a $, then it means the query didn't resolve to anything
+	if ok && !strings.Contains(image, "$") {
 		imageSpl := strings.Split(image, ":")
-		imageInfo = types.ImageInfo{
-			Repository: imageSpl[0],
-			Tag:        imageSpl[1],
+		if len(imageSpl) == 2 {
+			imageInfo = types.ImageInfo{
+				Repository: imageSpl[0],
+				Tag:        imageSpl[1],
+			}
+		} else {
+			return fmt.Errorf("could not parse image info %s", image)
 		}
 	}