Explorar o código

Merge branch '0.8.0-api-cleanup' of https://github.com/porter-dev/porter into 0.8.0-api-cleanup

merg
Alexander Belanger %!s(int64=4) %!d(string=hai) anos
pai
achega
3b1e7b251a

+ 91 - 0
api/server/handlers/namespace/create_configmap.go

@@ -0,0 +1,91 @@
+package namespace
+
+import (
+	"fmt"
+	"net/http"
+
+	v1 "k8s.io/api/core/v1"
+
+	"github.com/porter-dev/porter/api/server/authz"
+	"github.com/porter-dev/porter/api/server/handlers"
+	"github.com/porter-dev/porter/api/server/shared"
+	"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/models"
+)
+
+type CreateConfigMapHandler struct {
+	handlers.PorterHandlerReadWriter
+	authz.KubernetesAgentGetter
+}
+
+func NewCreateConfigMapHandler(
+	config *config.Config,
+	decoderValidator shared.RequestDecoderValidator,
+	writer shared.ResultWriter,
+) *CreateConfigMapHandler {
+	return &CreateConfigMapHandler{
+		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
+		KubernetesAgentGetter:   authz.NewOutOfClusterAgentGetter(config),
+	}
+}
+
+func (c *CreateConfigMapHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	request := &types.CreateConfigMapRequest{}
+
+	if ok := c.DecodeAndValidate(w, r, request); !ok {
+		return
+	}
+
+	namespace := r.Context().Value(types.NamespaceScope).(string)
+	cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
+
+	agent, err := c.GetAgent(r, cluster)
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	configMap, err := createConfigMap(agent, namespace, request)
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	var res = types.CreateConfigMapResponse{
+		ConfigMap: configMap,
+	}
+
+	c.WriteResult(w, r, res)
+}
+
+func createConfigMap(agent *kubernetes.Agent, namespace string, request *types.CreateConfigMapRequest) (*v1.ConfigMap, error) {
+	secretData := make(map[string][]byte)
+
+	for key, rawValue := range request.SecretVariables {
+		// encodedValue := base64.StdEncoding.EncodeToString([]byte(rawValue))
+
+		// if err != nil {
+		// 	app.handleErrorInternal(err, w)
+		// 	return
+		// }
+
+		secretData[key] = []byte(rawValue)
+	}
+
+	// create secret first
+	if _, err := agent.CreateLinkedSecret(request.Name, namespace, request.Name, secretData); err != nil {
+		return nil, err
+	}
+
+	// add all secret env variables to configmap with value PORTERSECRET_${configmap_name}
+	for key := range request.SecretVariables {
+		request.Variables[key] = fmt.Sprintf("PORTERSECRET_%s", request.Name)
+	}
+
+	return agent.CreateConfigMap(request.Name, namespace, request.Variables)
+}

+ 38 - 8
api/server/router/namespace.go

@@ -53,6 +53,35 @@ func getNamespaceRoutes(
 
 	routes := make([]*Route, 0)
 
+	// GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap/list -> namespace.NewListConfigMapsHandler
+	listConfigMapsEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: relPath + "/configmap/list",
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+				types.ClusterScope,
+				types.NamespaceScope,
+			},
+		},
+	)
+
+	listConfigMapsHandler := namespace.NewListConfigMapsHandler(
+		config,
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: listConfigMapsEndpoint,
+		Handler:  listConfigMapsHandler,
+		Router:   r,
+	})
+
 	// GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap -> namespace.NewGetConfigMapHandler
 	getConfigMapEndpoint := factory.NewAPIEndpoint(
 		&types.APIRequestMetadata{
@@ -83,14 +112,14 @@ func getNamespaceRoutes(
 		Router:   r,
 	})
 
-	// GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap/list -> namespace.NewListConfigMapsHandler
-	listConfigMapsEndpoint := factory.NewAPIEndpoint(
+	// POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/configmap/create -> namespace.NewGetConfigMapHandler
+	createConfigMapEndpoint := factory.NewAPIEndpoint(
 		&types.APIRequestMetadata{
-			Verb:   types.APIVerbGet,
-			Method: types.HTTPVerbGet,
+			Verb:   types.APIVerbCreate,
+			Method: types.HTTPVerbPost,
 			Path: &types.Path{
 				Parent:       basePath,
-				RelativePath: relPath + "/configmap/list",
+				RelativePath: relPath + "/configmap/create",
 			},
 			Scopes: []types.PermissionScope{
 				types.UserScope,
@@ -101,14 +130,15 @@ func getNamespaceRoutes(
 		},
 	)
 
-	listConfigMapsHandler := namespace.NewListConfigMapsHandler(
+	createConfigMapHandler := namespace.NewCreateConfigMapHandler(
 		config,
+		factory.GetDecoderValidator(),
 		factory.GetResultWriter(),
 	)
 
 	routes = append(routes, &Route{
-		Endpoint: listConfigMapsEndpoint,
-		Handler:  listConfigMapsHandler,
+		Endpoint: createConfigMapEndpoint,
+		Handler:  createConfigMapHandler,
 		Router:   r,
 	})
 

+ 10 - 0
api/types/namespace.go

@@ -66,3 +66,13 @@ type GetConfigMapResponse struct {
 type ListConfigMapsResponse struct {
 	*v1.ConfigMapList
 }
+
+type CreateConfigMapRequest struct {
+	Name            string            `json:"name,required"`
+	Variables       map[string]string `json:"variables,required"`
+	SecretVariables map[string]string `json:"secret_variables,required"`
+}
+
+type CreateConfigMapResponse struct {
+	*v1.ConfigMap
+}

+ 1 - 1
dashboard/src/main/home/cluster-dashboard/env-groups/CreateEnvGroup.tsx

@@ -90,13 +90,13 @@ export default class CreateEnvGroup extends Component<PropsType, StateType> {
         "<token>",
         {
           name: this.state.envGroupName,
-          namespace: this.state.selectedNamespace,
           variables: apiEnvVariables,
           secret_variables: secretEnvVariables,
         },
         {
           id: this.context.currentProject.id,
           cluster_id: this.props.currentCluster.id,
+          namespace: this.state.selectedNamespace,
         }
       )
       .then((res) => {

+ 6 - 4
dashboard/src/shared/api.tsx

@@ -902,14 +902,16 @@ const getConfigMap = baseApi<
 const createConfigMap = baseApi<
   {
     name: string;
-    namespace: string;
     variables: Record<string, string>;
     secret_variables?: Record<string, string>;
   },
-  { id: number; cluster_id: number }
+  {
+    id: number;
+    cluster_id: number;
+    namespace: string;
+  }
 >("POST", (pathParams) => {
-  let { id, cluster_id } = pathParams;
-  return `/api/projects/${id}/k8s/configmap/create?cluster_id=${cluster_id}`;
+  return `/api/projects/${pathParams.id}/clusters/${pathParams.cluster_id}/namespaces/${pathParams.namespace}/configmap/create`;
 });
 
 const updateConfigMap = baseApi<

+ 12 - 12
docs/developing/backend-refactor-status.md

@@ -60,7 +60,7 @@
 | <li>- [ ] `GET /api/projects/{project_id}/helmrepos`                                                                        |             |                 |             |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/helmrepos`                                                                       |             |                 |             |                  |
 | <li>- [ ] `GET /api/projects/{project_id}/helmrepos/{helm_id}/charts`                                                       |             |                 |             |                  |
-| <li>- [x] `GET /api/projects/{project_id}/infra`                                                                            |             |                 |             | yes              |
+| <li>- [x] `GET /api/projects/{project_id}/infra`                                                                            | AS          |                 |             | yes              |
 | <li>- [ ] `POST /api/projects/{project_id}/infra/{infra_id}/docr/destroy`                                                   |             |                 |             |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/infra/{infra_id}/doks/destroy`                                                   |             |                 |             |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/infra/{infra_id}/ecr/destroy`                                                    |             |                 |             |                  |
@@ -77,26 +77,26 @@
 | <li>- [ ] `POST /api/projects/{project_id}/invites/{invite_id}`                                                             |             |                 |             |                  |
 | <li>- [ ] `DELETE /api/projects/{project_id}/invites/{invite_id}`                                                           |             |                 |             |                  |
 | <li>- [ ] `GET /api/projects/{project_id}/invites/{token}`                                                                  |             |                 |             |                  |
-| <li>- [x] `GET /api/projects/{project_id}/k8s/configmap`                                                                    |             | yes             |             | yes              |
-| <li>- [ ] `POST /api/projects/{project_id}/k8s/configmap/create`                                                            |             |                 |             |                  |
+| <li>- [x] `GET /api/projects/{project_id}/k8s/configmap`                                                                    | AS          | yes             |             | yes              |
+| <li>- [x] `POST /api/projects/{project_id}/k8s/configmap/create`                                                            | AS          | yes             |             | yes              |
 | <li>- [ ] `DELETE /api/projects/{project_id}/k8s/configmap/delete`                                                          |             |                 |             |                  |
-| <li>- [x] `GET /api/projects/{project_id}/k8s/configmap/list`                                                               |             | yes             |             | yes              |
+| <li>- [x] `GET /api/projects/{project_id}/k8s/configmap/list`                                                               | AS          | yes             |             | yes              |
 | <li>- [ ] `POST /api/projects/{project_id}/k8s/configmap/rename`                                                            |             |                 |             |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/k8s/configmap/update`                                                            |             |                 |             |                  |
 | <li>- [ ] `GET /api/projects/{project_id}/k8s/helm_releases`                                                                |             |                 |             |                  |
 | <li>- [ ] `DELETE /api/projects/{project_id}/k8s/jobs/{namespace}/{name}`                                                   |             |                 |             |                  |
 | <li>- [ ] `GET /api/projects/{project_id}/k8s/jobs/{namespace}/{name}/pods`                                                 |             |                 |             |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/k8s/jobs/{namespace}/{name}/stop`                                                |             |                 |             |                  |
-| <li>- [x] `GET /api/projects/{project_id}/k8s/kubeconfig`                                                                   |             | yes             | yes         |                  |
-| <li>- [x] `GET /api/projects/{project_id}/k8s/metrics`                                                                      |             | yes             |             | yes              |
-| <li>- [x] `GET /api/projects/{project_id}/k8s/namespaces`                                                                   |             | yes             |             | yes              |
-| <li>- [x] `POST /api/projects/{project_id}/k8s/namespaces/create`                                                           |             | yes             |             | yes              |
-| <li>- [x] `DELETE /api/projects/{project_id}/k8s/namespaces/delete`                                                         |             | yes             |             | yes              |
+| <li>- [x] `GET /api/projects/{project_id}/k8s/kubeconfig`                                                                   | AS          | yes             | yes         |                  |
+| <li>- [x] `GET /api/projects/{project_id}/k8s/metrics`                                                                      | AS          | yes             |             | yes              |
+| <li>- [x] `GET /api/projects/{project_id}/k8s/namespaces`                                                                   | AS          | yes             |             | yes              |
+| <li>- [x] `POST /api/projects/{project_id}/k8s/namespaces/create`                                                           | AS          | yes             |             | yes              |
+| <li>- [x] `DELETE /api/projects/{project_id}/k8s/namespaces/delete`                                                         | AS          | yes             |             | yes              |
 | <li>- [ ] `GET /api/projects/{project_id}/k8s/pods`                                                                         |             |                 |             |                  |
 | <li>- [ ] `DELETE /api/projects/{project_id}/k8s/pods/{namespace}/{name}`                                                   |             |                 |             |                  |
 | <li>- [ ] `GET /api/projects/{project_id}/k8s/pods/{namespace}/{name}/events/list`                                          |             |                 |             |                  |
-| <li>- [x] `GET /api/projects/{project_id}/k8s/prometheus/detect`                                                            |             | yes             |             | yes              |
-| <li>- [x] `GET /api/projects/{project_id}/k8s/prometheus/ingresses`                                                         |             | yes             |             | yes              |
+| <li>- [x] `GET /api/projects/{project_id}/k8s/prometheus/detect`                                                            | AS          | yes             |             | yes              |
+| <li>- [x] `GET /api/projects/{project_id}/k8s/prometheus/ingresses`                                                         | AS          | yes             |             | yes              |
 | <li>- [ ] `POST /api/projects/{project_id}/k8s/subdomain`                                                                   |             |                 |             |                  |
 | <li>- [ ] `GET /api/projects/{project_id}/k8s/{namespace}/ingress/{name}`                                                   |             |                 |             |                  |
 | <li>- [ ] `GET /api/projects/{project_id}/k8s/{namespace}/pod/{name}/logs`                                                  |             |                 |             |                  |
@@ -123,7 +123,7 @@
 | <li>- [ ] `GET /api/projects/{project_id}/registries/{registry_id}/repositories`                                            |             |                 |             |                  |
 | <li>- [ ] `GET /api/projects/{project_id}/registries/{registry_id}/repositories/*`                                          |             |                 |             |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/registries/{registry_id}/repository`                                             |             |                 |             |                  |
-| <li>- [x] `GET /api/projects/{project_id}/releases`                                                                         |             | yes             |             | yes              |
+| <li>- [x] `GET /api/projects/{project_id}/releases`                                                                         | AS          | yes             |             | yes              |
 | <li>- [ ] `POST /api/projects/{project_id}/releases/image/update/batch`                                                     |             |                 |             |                  |
 | <li>- [ ] `GET /api/projects/{project_id}/releases/{name}/history`                                                          |             |                 |             |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/releases/{name}/notifications`                                                   |             |                 |             |                  |