manifests.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package porter_app
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/authz"
  5. "github.com/porter-dev/porter/api/server/shared/requestutils"
  6. "connectrpc.com/connect"
  7. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  8. "github.com/porter-dev/porter/internal/telemetry"
  9. "github.com/porter-dev/porter/api/server/handlers"
  10. "github.com/porter-dev/porter/api/server/shared"
  11. "github.com/porter-dev/porter/api/server/shared/apierrors"
  12. "github.com/porter-dev/porter/api/server/shared/config"
  13. "github.com/porter-dev/porter/api/types"
  14. "github.com/porter-dev/porter/internal/models"
  15. )
  16. // AppManifestsHandler handles requests to the /apps/{porter_app_name}/manifests endpoint
  17. type AppManifestsHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. authz.KubernetesAgentGetter
  20. }
  21. // NewAppManifestsHandler returns a new AppManifestsHandler
  22. func NewAppManifestsHandler(
  23. config *config.Config,
  24. decoderValidator shared.RequestDecoderValidator,
  25. writer shared.ResultWriter,
  26. ) *AppManifestsHandler {
  27. return &AppManifestsHandler{
  28. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  29. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  30. }
  31. }
  32. // AppManifestsRequest is the request object for the /apps/{porter_app_name}/manifests endpoint
  33. type AppManifestsRequest struct {
  34. DeploymentTargetID string `schema:"deployment_target_id"`
  35. }
  36. // AppManifestsResponse is the response object for the /apps/{porter_app_name}/manifests endpoint
  37. type AppManifestsResponse struct {
  38. // Base64Manifests is the base64 encoded manifests
  39. Base64Manifests string `json:"base64_manifests"`
  40. }
  41. // ServeHTTP translates the request into a TemplateAppManifests grpc request, forwards to the cluster control plane, and returns the response.
  42. func (c *AppManifestsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  43. ctx, span := telemetry.NewSpan(r.Context(), "serve-app-manifests")
  44. defer span.End()
  45. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  46. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  47. appName, reqErr := requestutils.GetURLParamString(r, types.URLParamPorterAppName)
  48. if reqErr != nil {
  49. e := telemetry.Error(ctx, span, reqErr, "error parsing stack name from url")
  50. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest))
  51. return
  52. }
  53. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: appName})
  54. request := &AppManifestsRequest{}
  55. if ok := c.DecodeAndValidate(w, r, request); !ok {
  56. err := telemetry.Error(ctx, span, nil, "error decoding request")
  57. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  58. return
  59. }
  60. // optional deployment target id - if not provided, use the cluster's default
  61. deploymentTargetID := request.DeploymentTargetID
  62. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "deployment-target-id", Value: deploymentTargetID})
  63. var deploymentTargetIdentifer *porterv1.DeploymentTargetIdentifier
  64. if deploymentTargetID != "" {
  65. deploymentTargetIdentifer = &porterv1.DeploymentTargetIdentifier{
  66. Id: deploymentTargetID,
  67. }
  68. }
  69. appManifestsReq := connect.NewRequest(&porterv1.TemplateAppManifestsRequest{
  70. ProjectId: int64(project.ID),
  71. ClusterId: int64(cluster.ID),
  72. AppName: appName,
  73. DeploymentTargetIdentifier: deploymentTargetIdentifer,
  74. })
  75. appManifestsRes, err := c.Config().ClusterControlPlaneClient.TemplateAppManifests(ctx, appManifestsReq)
  76. if err != nil {
  77. err := telemetry.Error(ctx, span, err, "error getting current app manifests from cluster control plane client")
  78. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  79. return
  80. }
  81. if appManifestsRes == nil || appManifestsRes.Msg == nil {
  82. err := telemetry.Error(ctx, span, err, "current app manifests resp is nil")
  83. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  84. return
  85. }
  86. response := AppManifestsResponse{
  87. Base64Manifests: appManifestsRes.Msg.Base64Manifests,
  88. }
  89. c.WriteResult(w, r, response)
  90. }