Просмотр исходного кода

Merge branch 'nico/fix-source-configs-not-found' of github.com:porter-dev/porter into dev

jnfrati 3 лет назад
Родитель
Сommit
2aa2c88cfd

+ 3 - 3
api/server/handlers/stack/create.go

@@ -269,10 +269,10 @@ func getSourceConfigModels(sourceConfigs []*types.CreateStackSourceConfigRequest
 			}
 
 			// If the source config had a source config ID then we need to copy it over
-			if len(sourceConfig.SourceConfigID) != 0 {
-				newSourceConfig.SourceConfigID = sourceConfig.SourceConfigID
+			if sourceConfig.StableSourceConfigID != "" {
+				newSourceConfig.StableSourceConfigID = sourceConfig.StableSourceConfigID
 			} else {
-				newSourceConfig.SourceConfigID = string(uid)
+				newSourceConfig.StableSourceConfigID = string(uid)
 			}
 
 			res = append(res, *newSourceConfig)

+ 6 - 6
api/server/handlers/stack/update_stack_name.go → api/server/handlers/stack/update_stack.go

@@ -12,25 +12,25 @@ import (
 	"github.com/porter-dev/porter/internal/models"
 )
 
-type StackUpdateStackName struct {
+type StackUpdateStack struct {
 	handlers.PorterHandlerReadWriter
 }
 
-func NewStackUpdateStackNameHandler(
+func NewStackUpdateStackHandler(
 	config *config.Config,
 	reader shared.RequestDecoderValidator,
 	writer shared.ResultWriter,
-) *StackUpdateStackName {
-	return &StackUpdateStackName{
+) *StackUpdateStack {
+	return &StackUpdateStack{
 		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, reader, writer),
 	}
 }
 
-func (p *StackUpdateStackName) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+func (p *StackUpdateStack) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
 	stack, _ := r.Context().Value(types.StackScope).(*models.Stack)
 
-	req := &types.UpdateStackNameRequest{}
+	req := &types.UpdateStackRequest{}
 
 	if ok := p.DecodeAndValidate(w, r, req); !ok {
 		return

+ 14 - 16
api/server/router/v1/stack.go

@@ -9,7 +9,7 @@ import (
 	"github.com/porter-dev/porter/api/types"
 )
 
-// swagger:parameters getStack deleteStack putStackSource rollbackStack listStackRevisions addApplication addEnvGroup
+// swagger:parameters getStack deleteStack putStackSource rollbackStack listStackRevisions addApplication addEnvGroup updateStack
 type stackPathParams struct {
 	// The project id
 	// in: path
@@ -820,15 +820,15 @@ func getV1StackRoutes(
 		Router:   r,
 	})
 
-	// PATCH /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/update_name -> stack.NewStackAddApplicationHandler
-	// swagger:operation PATCH /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/update_name addApplication
+	// PATCH /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id} -> stack.NewStackUpdateStackHandler
+	// swagger:operation PATCH /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id} updateStack
 	//
-	// Adds an application to an existing stack
+	// Updates a stack. Currently the only value available to update is the stack name.
 	//
 	// ---
 	// produces:
 	// - application/json
-	// summary: Add an application to a stack
+	// summary: Update Stack
 	// tags:
 	// - Stacks
 	// parameters:
@@ -837,24 +837,22 @@ func getV1StackRoutes(
 	//   - name: namespace
 	//   - name: stack_id
 	//   - in: body
-	//     name: AddApplicationToStack
-	//     description: The application to add
+	//     name: UpdateStack
+	//     description: The stack to update
 	//     schema:
-	//       $ref: '#/definitions/CreateStackAppResourceRequest'
+	//       $ref: '#/definitions/UpdateStackRequest'
 	// responses:
 	//   '200':
-	//     description: Successfully added the application to the stack
-	//   '400':
-	//     description: Stack does not have any revisions
+	//     description: Successfully updated the stack
 	//   '403':
 	//     description: Forbidden
-	updateStackNameEndpoint := factory.NewAPIEndpoint(
+	updateStackEndpoint := factory.NewAPIEndpoint(
 		&types.APIRequestMetadata{
 			Verb:   types.APIVerbUpdate,
 			Method: types.HTTPVerbPatch,
 			Path: &types.Path{
 				Parent:       basePath,
-				RelativePath: relPath + "/{stack_id}/update_name",
+				RelativePath: relPath + "/{stack_id}",
 			},
 			Scopes: []types.PermissionScope{
 				types.UserScope,
@@ -866,15 +864,15 @@ func getV1StackRoutes(
 		},
 	)
 
-	updateStackNameHandler := stack.NewStackUpdateStackNameHandler(
+	updateStackHandler := stack.NewStackUpdateStackHandler(
 		config,
 		factory.GetDecoderValidator(),
 		factory.GetResultWriter(),
 	)
 
 	routes = append(routes, &router.Route{
-		Endpoint: updateStackNameEndpoint,
-		Handler:  updateStackNameHandler,
+		Endpoint: updateStackEndpoint,
+		Handler:  updateStackHandler,
 		Router:   r,
 	})
 

+ 4 - 3
api/types/stacks.go

@@ -63,7 +63,8 @@ type CreateStackAppResourceRequest struct {
 	SourceConfigName string `json:"source_config_name" form:"required"`
 }
 
-type UpdateStackNameRequest struct {
+// swagger:model
+type UpdateStackRequest struct {
 	Name string `json:"name" form:"required"`
 }
 
@@ -227,7 +228,7 @@ type StackSourceConfig struct {
 	StackSourceConfigBuild *StackSourceConfigBuild `json:"build,omitempty"`
 
 	// Unique ID to identify between revisions
-	SourceConfigID string `json:"source_config_id"`
+	StableSourceConfigID string `json:"stable_source_config_id"`
 }
 
 // swagger:model
@@ -261,7 +262,7 @@ type CreateStackSourceConfigRequest struct {
 	// required: true
 	ImageTag string `json:"image_tag" form:"required"`
 
-	SourceConfigID string `json:"source_config_id,omitempty"`
+	StableSourceConfigID string `json:"source_config_id,omitempty"`
 
 	// If this field is empty, the resource is deployed directly from the image repo uri
 	StackSourceConfigBuild *StackSourceConfigBuild `json:"build,omitempty"`

+ 2 - 2
dashboard/src/main/home/cluster-dashboard/stacks/ExpandedStack/_SourceConfig.tsx

@@ -170,10 +170,10 @@ const SourceConfigItem = ({
         <SourceConfigStyles.ItemTitle>
           <span>{name}</span>
 
-          {sourceConfig.source_config_id && (
+          {sourceConfig.stable_source_config_id && (
             <EditButton
               onClick={toggleEditNameMode}
-              disabled={!sourceConfig.source_config_id}
+              disabled={!sourceConfig.stable_source_config_id}
             >
               <i className="material-icons-outlined">edit</i>
             </EditButton>

+ 1 - 1
dashboard/src/main/home/cluster-dashboard/stacks/ExpandedStack/components/Settings.tsx

@@ -40,7 +40,7 @@ const Settings = ({
   const handleStackNameChange = async () => {
     setButtonStatus("loading");
     try {
-      await api.updateStackName(
+      await api.updateStack(
         "<token>",
         {
           name: stackName,

+ 1 - 1
dashboard/src/main/home/cluster-dashboard/stacks/types.ts

@@ -90,7 +90,7 @@ export type SourceConfig = {
   stack_id: string;
   stack_revision_id: number;
 
-  source_config_id: string;
+  stable_source_config_id: string;
 
   build?: {
     method: "pack" | "docker";

+ 3 - 3
dashboard/src/shared/api.tsx

@@ -2162,7 +2162,7 @@ const removeStackEnvGroup = baseApi<
     `/api/v1/projects/${project_id}/clusters/${cluster_id}/namespaces/${namespace}/stacks/${stack_id}/remove_env_group/${env_group_name}`
 );
 
-const updateStackName = baseApi<
+const updateStack = baseApi<
   {
     name: string;
   },
@@ -2175,7 +2175,7 @@ const updateStackName = baseApi<
 >(
   "PATCH",
   ({ project_id, cluster_id, namespace, stack_id }) =>
-    `/api/v1/projects/${project_id}/clusters/${cluster_id}/namespaces/${namespace}/stacks/${stack_id}/update_name`
+    `/api/v1/projects/${project_id}/clusters/${cluster_id}/namespaces/${namespace}/stacks/${stack_id}`
 );
 
 const getGithubStatus = baseApi<{}, {}>("GET", ({}) => `/api/status/github`);
@@ -2380,7 +2380,7 @@ export default {
   removeStackAppResource,
   addStackEnvGroup,
   removeStackEnvGroup,
-  updateStackName,
+  updateStack,
 
   // STATUS
   getGithubStatus,

+ 12 - 10
internal/models/stack.go

@@ -160,7 +160,9 @@ func (s StackResource) ToStackResource(stackID string, stackRevisionID uint, sou
 type StackSourceConfig struct {
 	gorm.Model
 
-	SourceConfigID string
+	// A unique identifier for this source config, this will allow us identify a same source config
+	// across multiple revisions and updates. This is not the same as the UID or ID which are updated over revisions.
+	StableSourceConfigID string
 
 	StackRevisionID uint
 
@@ -177,15 +179,15 @@ type StackSourceConfig struct {
 
 func (s StackSourceConfig) ToStackSourceConfigType(stackID string, stackRevisionID uint) *types.StackSourceConfig {
 	return &types.StackSourceConfig{
-		CreatedAt:       s.CreatedAt,
-		UpdatedAt:       s.UpdatedAt,
-		StackID:         stackID,
-		StackRevisionID: stackRevisionID,
-		Name:            s.Name,
-		ID:              s.UID,
-		ImageRepoURI:    s.ImageRepoURI,
-		ImageTag:        s.ImageTag,
-		SourceConfigID:  s.SourceConfigID,
+		CreatedAt:            s.CreatedAt,
+		UpdatedAt:            s.UpdatedAt,
+		StackID:              stackID,
+		StackRevisionID:      stackRevisionID,
+		Name:                 s.Name,
+		ID:                   s.UID,
+		ImageRepoURI:         s.ImageRepoURI,
+		ImageTag:             s.ImageTag,
+		StableSourceConfigID: s.StableSourceConfigID,
 	}
 }
 

+ 13 - 3
internal/stacks/helpers.go

@@ -52,10 +52,20 @@ func CloneAppResources(
 			if prevSourceConfig.UID == appResource.StackSourceConfigUID {
 				// find the corresponding new source config
 				for _, newSourceConfig := range newSourceConfigs {
-					fmt.Println(newSourceConfig.SourceConfigID, prevSourceConfig.SourceConfigID)
-					if newSourceConfig.SourceConfigID == prevSourceConfig.SourceConfigID {
-						linkedSourceConfigUID = newSourceConfig.UID
+
+					// If the source config was created previous to the implemntation of StableSourceConfigID
+					// it means that we should check by the name and not the StableSourceConfigID.
+					// This will happen only once for old source configs.
+					if prevSourceConfig.StableSourceConfigID != "" {
+						if newSourceConfig.StableSourceConfigID == prevSourceConfig.StableSourceConfigID {
+							linkedSourceConfigUID = newSourceConfig.UID
+						}
+					} else {
+						if newSourceConfig.Name == prevSourceConfig.Name {
+							linkedSourceConfigUID = newSourceConfig.UID
+						}
 					}
+
 				}
 			}
 		}