瀏覽代碼

save base deployment target to app template (#3871)

ianedwards 2 年之前
父節點
當前提交
d94d1b95d0

+ 17 - 3
api/server/handlers/porter_app/create_app_template.go

@@ -43,9 +43,10 @@ func NewCreateAppTemplateHandler(
 
 // CreateAppTemplateRequest is the request object for the /app-template POST endpoint
 type CreateAppTemplateRequest struct {
-	B64AppProto string            `json:"b64_app_proto"`
-	Variables   map[string]string `json:"variables"`
-	Secrets     map[string]string `json:"secrets"`
+	B64AppProto            string            `json:"b64_app_proto"`
+	Variables              map[string]string `json:"variables"`
+	Secrets                map[string]string `json:"secrets"`
+	BaseDeploymentTargetID string            `json:"base_deployment_target_id"`
 }
 
 // CreateAppTemplateResponse is the response object for the /app-template POST endpoint
@@ -88,6 +89,18 @@ func (c *CreateAppTemplateHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 		return
 	}
 
+	baseDeploymentTarget, err := uuid.Parse(request.BaseDeploymentTargetID)
+	if err != nil {
+		err := telemetry.Error(ctx, span, err, "error parsing base deployment target id")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
+		return
+	}
+	if baseDeploymentTarget == uuid.Nil {
+		err := telemetry.Error(ctx, span, err, "base deployment target id is nil")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
+		return
+	}
+
 	agent, err := c.GetAgent(r, cluster, "")
 	if err != nil {
 		err := telemetry.Error(ctx, span, err, "error getting agent")
@@ -152,6 +165,7 @@ func (c *CreateAppTemplateHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 	}
 
 	appTemplate.Base64App = protoWithoutDefaultAppEnvGroups
+	appTemplate.BaseDeploymentTargetID = baseDeploymentTarget
 
 	updatedAppTemplate, err := c.Repo().AppTemplate().CreateAppTemplate(appTemplate)
 	if err != nil {

+ 0 - 18
api/server/handlers/porter_app/validate.go

@@ -6,7 +6,6 @@ import (
 
 	"connectrpc.com/connect"
 
-	"github.com/google/uuid"
 	porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
 
 	"github.com/porter-dev/api-contracts/generated/go/helpers"
@@ -132,7 +131,6 @@ func (c *ValidatePorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 	)
 
 	var overrides *porterv1.PorterApp
-	var baseDeploymentTargetID string
 
 	if request.Base64AppOverrides != "" {
 		decoded, err := base64.StdEncoding.DecodeString(request.Base64AppOverrides)
@@ -151,21 +149,6 @@ func (c *ValidatePorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 		}
 
 		telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "validated-with-overrides", Value: true})
-
-		// once we support many deployment targets, this will need to be updated to use whatever deployment target has been selected as the base
-		defaultDeploymentTarget, err := c.Repo().DeploymentTarget().DeploymentTargetBySelectorAndSelectorType(project.ID, cluster.ID, DeploymentTargetSelector_Default, DeploymentTargetSelectorType_Default)
-		if err != nil {
-			err := telemetry.Error(ctx, span, err, "error getting default deployment target from repo")
-			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
-			return
-		}
-		if defaultDeploymentTarget.ID == uuid.Nil {
-			err := telemetry.Error(ctx, span, err, "default deployment target not found")
-			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
-			return
-		}
-
-		baseDeploymentTargetID = defaultDeploymentTarget.ID.String()
 	}
 
 	var serviceDeletions map[string]*porterv1.ServiceDeletions
@@ -192,7 +175,6 @@ func (c *ValidatePorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 			EnvGroupNames:    request.Deletions.EnvGroupNames,
 			ServiceDeletions: serviceDeletions,
 		},
-		BaseDeploymentTargetId: baseDeploymentTargetID,
 	})
 	ccpResp, err := c.Config().ClusterControlPlaneClient.ValidatePorterApp(ctx, validateReq)
 	if err != nil {

+ 7 - 1
dashboard/src/main/home/cluster-dashboard/preview-environments/v2/setup-app/AppTemplateForm.tsx

@@ -226,6 +226,7 @@ const AppTemplateForm: React.FC<Props> = ({ existingTemplate }) => {
             b64_app_proto: btoa(app.toJsonString()),
             variables,
             secrets,
+            base_deployment_target_id: deploymentTarget.id,
           },
           {
             project_id: projectId,
@@ -280,7 +281,9 @@ const AppTemplateForm: React.FC<Props> = ({ existingTemplate }) => {
                 fieldArrayName={"app.services"}
                 maxCPU={currentClusterResources.maxCPU}
                 maxRAM={currentClusterResources.maxRAM}
-                clusterContainsGPUNodes={currentClusterResources.clusterContainsGPUNodes}
+                clusterContainsGPUNodes={
+                  currentClusterResources.clusterContainsGPUNodes
+                }
                 internalNetworkingDetails={{
                   namespace: deploymentTarget.namespace,
                   appName: porterApp.name,
@@ -321,6 +324,9 @@ const AppTemplateForm: React.FC<Props> = ({ existingTemplate }) => {
                 fieldArrayName={"app.predeploy"}
                 maxCPU={currentClusterResources.maxCPU}
                 maxRAM={currentClusterResources.maxRAM}
+                clusterContainsGPUNodes={
+                  currentClusterResources.clusterContainsGPUNodes
+                }
               />
             </>,
             <Button

+ 1 - 0
dashboard/src/shared/api.tsx

@@ -988,6 +988,7 @@ const createAppTemplate = baseApi<
     b64_app_proto: string;
     variables: Record<string, string>;
     secrets: Record<string, string>;
+    base_deployment_target_id: string;
   },
   {
     project_id: number;

+ 1 - 1
go.mod

@@ -83,7 +83,7 @@ require (
 	github.com/matryer/is v1.4.0
 	github.com/nats-io/nats.go v1.24.0
 	github.com/open-policy-agent/opa v0.44.0
-	github.com/porter-dev/api-contracts v0.2.27
+	github.com/porter-dev/api-contracts v0.2.29
 	github.com/riandyrn/otelchi v0.5.1
 	github.com/santhosh-tekuri/jsonschema/v5 v5.0.1
 	github.com/stefanmcshane/helm v0.0.0-20221213002717-88a4a2c6e77d

+ 2 - 2
go.sum

@@ -1520,8 +1520,8 @@ github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/polyfloyd/go-errorlint v0.0.0-20210722154253-910bb7978349/go.mod h1:wi9BfjxjF/bwiZ701TzmfKu6UKC357IOAtNr0Td0Lvw=
-github.com/porter-dev/api-contracts v0.2.27 h1:NZTWmbiqQF082Kl0vUtXev5gcI8lTY6bofjS4Sjwej4=
-github.com/porter-dev/api-contracts v0.2.27/go.mod h1:fX6JmP5QuzxDLvqP3evFOTXjI4dHxsG0+VKNTjImZU8=
+github.com/porter-dev/api-contracts v0.2.29 h1:K5NtjgAsVgPgJLk8/rv+UOlDHWzgHvaCqvT8gMjeFVM=
+github.com/porter-dev/api-contracts v0.2.29/go.mod h1:fX6JmP5QuzxDLvqP3evFOTXjI4dHxsG0+VKNTjImZU8=
 github.com/porter-dev/switchboard v0.0.3 h1:dBuYkiVLa5Ce7059d6qTe9a1C2XEORFEanhbtV92R+M=
 github.com/porter-dev/switchboard v0.0.3/go.mod h1:xSPzqSFMQ6OSbp42fhCi4AbGbQbsm6nRvOkrblFeXU4=
 github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=

+ 4 - 0
internal/models/app_template.go

@@ -20,4 +20,8 @@ type AppTemplate struct {
 
 	// PorterAppID is the ID of the PorterApp that the template belongs to.
 	PorterAppID int `json:"porter_app_id"`
+
+	// BaseDeploymentTargetID is the ID of the deployment target that this template is based on
+	// This is used to look up the latest app revision in the base, which will hydrate the template on apply.
+	BaseDeploymentTargetID uuid.UUID `json:"base_deployment_target_id" gorm:"type:uuid;default:00000000-0000-0000-0000-000000000000"`
 }