瀏覽代碼

app resource works

Mohammed Nafees 3 年之前
父節點
當前提交
d520b47409

+ 0 - 2
cli/cmd/apply.go

@@ -139,8 +139,6 @@ func apply(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string
 		if err != nil {
 			return err
 		}
-
-		return nil
 	} else if previewVersion.Version == "v1" {
 		if _, ok := os.LookupEnv("PORTER_VALIDATE_YAML"); ok {
 			err := applyValidate()

+ 82 - 0
cli/cmd/preview/v2beta1/app_resource.go

@@ -0,0 +1,82 @@
+package v2beta1
+
+import (
+	"fmt"
+	"strings"
+
+	"github.com/mitchellh/mapstructure"
+	apiTypes "github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/internal/integrations/preview"
+	"github.com/porter-dev/switchboard/pkg/types"
+)
+
+func (a *AppResource) GetName() string {
+	if a == nil || a.Name == nil {
+		return ""
+	}
+
+	return *a.Name
+}
+
+func (a *AppResource) GetDependsOn() []string {
+	var dependsOn []string
+
+	if a == nil || a.DependsOn == nil {
+		return dependsOn
+	}
+
+	for _, d := range a.DependsOn {
+		if d == nil {
+			continue
+		}
+
+		dependsOn = append(dependsOn, *d)
+	}
+
+	return dependsOn
+}
+
+func (a *AppResource) GetBuildRef() string {
+	if a == nil || a.BuildRef == nil {
+		return ""
+	}
+
+	return *a.BuildRef
+}
+
+func (a *AppResource) getV1Resource(b *Build) (*types.Resource, error) {
+	config := &preview.ApplicationConfig{}
+
+	config.Build.Method = "registry"
+	config.Build.Image = fmt.Sprintf("\"{ .%s.image }\"", b.GetName())
+	config.Build.Env = b.GetRawEnv()
+	config.Values = a.HelmValues
+
+	for _, eg := range b.GetEnvGroups() {
+		ns, name, _ := strings.Cut(eg, "/")
+
+		config.EnvGroups = append(config.EnvGroups, apiTypes.EnvGroupMeta{
+			Name:      name,
+			Namespace: ns,
+		})
+	}
+
+	rawConfig := make(map[string]any)
+
+	err := mapstructure.Decode(config, &rawConfig)
+
+	if err != nil {
+		return nil, err
+	}
+
+	return &types.Resource{
+		Name:      a.GetName(),
+		DependsOn: append([]string{b.GetName()}, a.GetDependsOn()...),
+		Source: map[string]any{
+			"name":    a.Chart.GetName(),
+			"repo":    a.Chart.GetURL(),
+			"version": a.Chart.GetVersion(),
+		},
+		Config: rawConfig,
+	}, nil
+}

+ 21 - 5
cli/cmd/preview/v2beta1/apply.go

@@ -152,11 +152,15 @@ func (a *PreviewApplier) DowngradeToV1() (*types.ResourceGroup, error) {
 		Version: "v1",
 	}
 
+	buildRefs := make(map[string]*Build)
+
 	for _, b := range a.parsed.Builds {
 		if b == nil {
 			continue
 		}
 
+		buildRefs[b.GetName()] = b
+
 		bi, err := b.getV1BuildImage()
 
 		if err != nil {
@@ -172,13 +176,25 @@ func (a *PreviewApplier) DowngradeToV1() (*types.ResourceGroup, error) {
 		v1File.Resources = append(v1File.Resources, bi, pi)
 	}
 
-	// fileBytes, err := yaml.Marshal(v1File)
+	for _, app := range a.parsed.Apps {
+		if app == nil {
+			continue
+		}
 
-	// if err != nil {
-	// 	return nil, err
-	// }
+		if _, ok := buildRefs[app.GetBuildRef()]; !ok {
+			errMsg := composePreviewMessage(fmt.Sprintf("build_ref '%s' referenced by app '%s' does not exist",
+				app.GetBuildRef(), app.GetName()), Error)
+			return nil, fmt.Errorf("%s: %w", errMsg, err)
+		}
+
+		ai, err := app.getV1Resource(buildRefs[app.GetBuildRef()])
 
-	// fmt.Println(string(fileBytes))
+		if err != nil {
+			return nil, err
+		}
+
+		v1File.Resources = append(v1File.Resources, ai)
+	}
 
 	return v1File, nil
 }

+ 25 - 0
cli/cmd/preview/v2beta1/helm_chart.go

@@ -0,0 +1,25 @@
+package v2beta1
+
+func (c *HelmChart) GetName() string {
+	if c == nil || c.Name == nil {
+		return ""
+	}
+
+	return *c.Name
+}
+
+func (c *HelmChart) GetURL() string {
+	if c == nil || c.URL == nil {
+		return ""
+	}
+
+	return *c.URL
+}
+
+func (c *HelmChart) GetVersion() string {
+	if c == nil || c.Version == nil {
+		return ""
+	}
+
+	return *c.Version
+}

+ 23 - 11
cli/cmd/preview/v2beta1/types.go

@@ -30,21 +30,33 @@ type Build struct {
 	// UseCache   *bool     `yaml:"use_cache"`
 }
 
-type Resource struct {
-	Name      *string          `yaml:"name" validate:"required,unique"`
-	DependsOn []*string        `yaml:"depends_on"`
-	Type      *string          `yaml:"type" validate:"required"`
-	ChartURL  *string          `yaml:"chart_url" validate:"url"`
-	Version   *string          `yaml:"version"`
-	Build     map[*string]*any `yaml:"build"`
-	Deploy    map[*string]*any `yaml:"deploy"`
+type HelmChart struct {
+	URL     *string `yaml:"url" validate:"url"`
+	Name    *string `yaml:"name" validate:"required"`
+	Version *string `yaml:"version"`
+}
+
+type AppResource struct {
+	Name      *string    `yaml:"name" validate:"required"`
+	DependsOn []*string  `yaml:"depends_on"`
+	Chart     *HelmChart `yaml:"helm_chart"`
+	BuildRef  *string    `yaml:"build_ref"`
+	// Deploy     map[*string]*any `yaml:"deploy"`
+	HelmValues map[string]any `yaml:"helm_values"`
+}
+
+type AddonResource struct {
+	Name       *string        `yaml:"name" validate:"required"`
+	DependsOn  []*string      `yaml:"depends_on"`
+	Chart      *HelmChart     `yaml:"helm_chart" validate:"required"`
+	HelmValues map[string]any `yaml:"helm_values"`
 }
 
 type PorterYAML struct {
 	Version *string `yaml:"version"`
 	// Variables []*Variable `yaml:"variables"`
 	// EnvGroups []*EnvGroup `yaml:"env_groups"`
-	Builds []*Build    `yaml:"builds"`
-	Apps   []*Resource `yaml:"apps"`
-	Addons []*Resource `yaml:"addons"`
+	Builds []*Build         `yaml:"builds"`
+	Apps   []*AppResource   `yaml:"apps"`
+	Addons []*AddonResource `yaml:"addons"`
 }