Sfoglia il codice sorgente

env group clone fix

Mohammed Nafees 4 anni fa
parent
commit
f32c779076

+ 21 - 0
api/client/k8s.go

@@ -91,6 +91,27 @@ func (c *Client) GetEnvGroup(
 	return resp, err
 }
 
+func (c *Client) CloneEnvGroup(
+	ctx context.Context,
+	projectID, clusterID uint,
+	namespace string,
+	req *types.CloneEnvGroupRequest,
+) (*types.EnvGroup, error) {
+	resp := &types.EnvGroup{}
+
+	err := c.postRequest(
+		fmt.Sprintf(
+			"/projects/%d/clusters/%d/namespaces/%s/envgroup/clone",
+			projectID, clusterID,
+			namespace,
+		),
+		req,
+		resp,
+	)
+
+	return resp, err
+}
+
 func (c *Client) GetRelease(
 	ctx context.Context,
 	projectID, clusterID uint,

+ 2 - 2
api/server/handlers/namespace/clone_env_group.go

@@ -46,7 +46,7 @@ func (c *CloneEnvGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 		return
 	}
 
-	envGroup, err := envgroup.GetEnvGroup(agent, request.Name, request.Namespace, request.Version)
+	envGroup, err := envgroup.GetEnvGroup(agent, request.Name, namespace, request.Version)
 
 	if err != nil {
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
@@ -59,7 +59,7 @@ func (c *CloneEnvGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 
 	configMap, err := envgroup.CreateEnvGroup(agent, types.ConfigMapInput{
 		Name:      request.CloneName,
-		Namespace: namespace,
+		Namespace: request.Namespace,
 		Variables: envGroup.Variables,
 	})
 

+ 9 - 9
api/server/handlers/namespace/get_env_group.go

@@ -1,9 +1,9 @@
 package namespace
 
 import (
-	"errors"
 	"fmt"
 	"net/http"
+	"strings"
 
 	"github.com/porter-dev/porter/api/server/authz"
 	"github.com/porter-dev/porter/api/server/handlers"
@@ -11,7 +11,6 @@ import (
 	"github.com/porter-dev/porter/api/server/shared/apierrors"
 	"github.com/porter-dev/porter/api/server/shared/config"
 	"github.com/porter-dev/porter/api/types"
-	"github.com/porter-dev/porter/internal/kubernetes"
 	"github.com/porter-dev/porter/internal/kubernetes/envgroup"
 	"github.com/porter-dev/porter/internal/models"
 )
@@ -51,13 +50,14 @@ func (c *GetEnvGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 
 	envGroup, err := envgroup.GetEnvGroup(agent, request.Name, namespace, request.Version)
 
-	if err != nil && errors.Is(err, kubernetes.IsNotFoundError) {
-		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
-			fmt.Errorf("env group not found"),
-			http.StatusNotFound,
-		))
-		return
-	} else if err != nil {
+	if err != nil {
+		if strings.Contains(err.Error(), "not found") {
+			c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
+				fmt.Errorf("env group not found"),
+				http.StatusNotFound),
+			)
+			return
+		}
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 		return
 	}

+ 6 - 6
api/server/router/namespace.go

@@ -87,14 +87,14 @@ func getNamespaceRoutes(
 		Router:   r,
 	})
 
-	// GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/envgroups/list -> namespace.NewListEnvGroupsHandler
+	// GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/envgroup/list -> namespace.NewListEnvGroupsHandler
 	listEnvGroupsEndpoint := factory.NewAPIEndpoint(
 		&types.APIRequestMetadata{
 			Verb:   types.APIVerbGet,
 			Method: types.HTTPVerbGet,
 			Path: &types.Path{
 				Parent:       basePath,
-				RelativePath: relPath + "/envgroups/list",
+				RelativePath: relPath + "/envgroup/list",
 			},
 			Scopes: []types.PermissionScope{
 				types.UserScope,
@@ -116,14 +116,14 @@ func getNamespaceRoutes(
 		Router:   r,
 	})
 
-	// GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/envgroups/clone -> namespace.NewCloneEnvGroupHandler
+	// POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/envgroup/clone -> namespace.NewCloneEnvGroupHandler
 	cloneEnvGroupEndpoint := factory.NewAPIEndpoint(
 		&types.APIRequestMetadata{
-			Verb:   types.APIVerbGet,
-			Method: types.HTTPVerbGet,
+			Verb:   types.APIVerbCreate,
+			Method: types.HTTPVerbPost,
 			Path: &types.Path{
 				Parent:       basePath,
-				RelativePath: relPath + "/envgroups/clone",
+				RelativePath: relPath + "/envgroup/clone",
 			},
 			Scopes: []types.PermissionScope{
 				types.UserScope,

+ 1 - 1
cli/cmd/apply.go

@@ -178,7 +178,7 @@ type ApplicationConfig struct {
 		Buildpacks []string
 	}
 
-	EnvGroups []string
+	EnvGroups []types.EnvGroupMeta
 
 	Values map[string]interface{}
 }

+ 31 - 33
cli/cmd/deploy/shared.go

@@ -2,9 +2,9 @@ package deploy
 
 import (
 	"context"
-	"strconv"
-	"strings"
+	"fmt"
 
+	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
 	"github.com/porter-dev/porter/api/types"
 )
@@ -19,43 +19,19 @@ type SharedOpts struct {
 	OverrideTag     string
 	Method          DeployBuildType
 	AdditionalEnv   map[string]string
-	EnvGroups       []string
-}
-
-func getEnvGroupNameVersion(group string) (string, uint, error) {
-	if !strings.Contains(group, "@") {
-		return group, 0, nil
-	}
-
-	envGroupSpl := strings.Split(group, "@")
-	envGroupName := envGroupSpl[0]
-	envGroupVersion := uint64(0)
-
-	envGroupVersion, err := strconv.ParseUint(envGroupSpl[1], 10, 32)
-
-	if err != nil {
-		return "", 0, err
-	}
-
-	return envGroupName, uint(envGroupVersion), nil
+	EnvGroups       []types.EnvGroupMeta
 }
 
 func coalesceEnvGroups(
 	client *api.Client,
 	projectID, clusterID uint,
 	namespace string,
-	envGroups []string,
+	envGroups []types.EnvGroupMeta,
 	config map[string]interface{},
 ) error {
 	for _, group := range envGroups {
-		if group == "" {
-			continue
-		}
-
-		envGroupName, envGroupVersion, err := getEnvGroupNameVersion(group)
-
-		if err != nil {
-			return err
+		if group.Name == "" {
+			return fmt.Errorf("env group name cannot be empty")
 		}
 
 		envGroup, err := client.GetEnvGroup(
@@ -64,12 +40,34 @@ func coalesceEnvGroups(
 			clusterID,
 			namespace,
 			&types.GetEnvGroupRequest{
-				Name:    envGroupName,
-				Version: envGroupVersion,
+				Name:    group.Name,
+				Version: group.Version,
 			},
 		)
 
-		if err != nil {
+		if err != nil && err.Error() == "env group not found" {
+			if group.Namespace == "" {
+				return fmt.Errorf("env group namespace cannot be empty")
+			}
+
+			color.New(color.FgBlue, color.Bold).
+				Printf("Env group '%s' does not exist in the target namespace '%s'\n", group.Name, namespace)
+			color.New(color.FgBlue, color.Bold).
+				Printf("Cloning env group '%s' from namespace '%s' to target namespace '%s'\n",
+					group.Name, group.Namespace, namespace)
+
+			envGroup, err = client.CloneEnvGroup(
+				context.Background(), projectID, clusterID, group.Namespace,
+				&types.CloneEnvGroupRequest{
+					Name:      group.Name,
+					Namespace: namespace,
+				},
+			)
+
+			if err != nil {
+				return err
+			}
+		} else if err != nil {
 			return err
 		}