apply.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package porter_app
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. "connectrpc.com/connect"
  9. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  10. "github.com/porter-dev/api-contracts/generated/go/helpers"
  11. "github.com/porter-dev/porter/internal/porter_app"
  12. "github.com/porter-dev/porter/internal/telemetry"
  13. "github.com/porter-dev/porter/api/server/authz"
  14. "github.com/porter-dev/porter/api/server/handlers"
  15. "github.com/porter-dev/porter/api/server/shared"
  16. "github.com/porter-dev/porter/api/server/shared/apierrors"
  17. "github.com/porter-dev/porter/api/server/shared/config"
  18. "github.com/porter-dev/porter/api/types"
  19. "github.com/porter-dev/porter/internal/models"
  20. )
  21. // ApplyPorterAppHandler is the handler for the /apps/parse endpoint
  22. type ApplyPorterAppHandler struct {
  23. handlers.PorterHandlerReadWriter
  24. authz.KubernetesAgentGetter
  25. }
  26. // NewApplyPorterAppHandler handles POST requests to the endpoint /apps/apply
  27. func NewApplyPorterAppHandler(
  28. config *config.Config,
  29. decoderValidator shared.RequestDecoderValidator,
  30. writer shared.ResultWriter,
  31. ) *ApplyPorterAppHandler {
  32. return &ApplyPorterAppHandler{
  33. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  34. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  35. }
  36. }
  37. // ApplyPorterAppRequest is the request object for the /apps/apply endpoint
  38. type ApplyPorterAppRequest struct {
  39. Base64AppProto string `json:"b64_app_proto"`
  40. DeploymentTargetId string `json:"deployment_target_id"`
  41. AppRevisionID string `json:"app_revision_id"`
  42. }
  43. // ApplyPorterAppResponse is the response object for the /apps/apply endpoint
  44. type ApplyPorterAppResponse struct {
  45. AppRevisionId string `json:"app_revision_id"`
  46. CLIAction porterv1.EnumCLIAction `json:"cli_action"`
  47. }
  48. // ServeHTTP translates the request into a ApplyPorterApp request, forwards to the cluster control plane, and returns the response
  49. func (c *ApplyPorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  50. ctx, span := telemetry.NewSpan(r.Context(), "serve-apply-porter-app")
  51. defer span.End()
  52. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  53. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  54. telemetry.WithAttributes(span,
  55. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  56. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  57. )
  58. if !project.GetFeatureFlag(models.ValidateApplyV2, c.Config().LaunchDarklyClient) {
  59. err := telemetry.Error(ctx, span, nil, "project does not have validate apply v2 enabled")
  60. c.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  61. return
  62. }
  63. request := &ApplyPorterAppRequest{}
  64. if ok := c.DecodeAndValidate(w, r, request); !ok {
  65. err := telemetry.Error(ctx, span, nil, "error decoding request")
  66. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  67. return
  68. }
  69. var appRevisionID string
  70. var appProto *porterv1.PorterApp
  71. var deploymentTargetID string
  72. if request.AppRevisionID != "" {
  73. appRevisionID = request.AppRevisionID
  74. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-revision-id", Value: request.AppRevisionID})
  75. } else {
  76. if request.Base64AppProto == "" {
  77. err := telemetry.Error(ctx, span, nil, "b64 yaml is empty")
  78. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  79. return
  80. }
  81. decoded, err := base64.StdEncoding.DecodeString(request.Base64AppProto)
  82. if err != nil {
  83. err := telemetry.Error(ctx, span, err, "error decoding base yaml")
  84. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  85. return
  86. }
  87. appProto = &porterv1.PorterApp{}
  88. err = helpers.UnmarshalContractObject(decoded, appProto)
  89. if err != nil {
  90. err := telemetry.Error(ctx, span, err, "error unmarshalling app proto")
  91. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  92. return
  93. }
  94. if request.DeploymentTargetId == "" {
  95. err := telemetry.Error(ctx, span, err, "deployment target id is empty")
  96. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  97. return
  98. }
  99. deploymentTargetID = request.DeploymentTargetId
  100. telemetry.WithAttributes(span,
  101. telemetry.AttributeKV{Key: "app-name", Value: appProto.Name},
  102. telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetId},
  103. )
  104. agent, err := c.GetAgent(r, cluster, "")
  105. if err != nil {
  106. err := telemetry.Error(ctx, span, err, "error getting kubernetes agent")
  107. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  108. return
  109. }
  110. subdomainCreateInput := porter_app.CreatePorterSubdomainInput{
  111. AppName: appProto.Name,
  112. RootDomain: c.Config().ServerConf.AppRootDomain,
  113. DNSClient: c.Config().DNSClient,
  114. DNSRecordRepository: c.Repo().DNSRecord(),
  115. KubernetesAgent: agent,
  116. }
  117. appProto, err = addPorterSubdomainsIfNecessary(ctx, appProto, subdomainCreateInput)
  118. if err != nil {
  119. err := telemetry.Error(ctx, span, err, "error adding porter subdomains")
  120. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  121. return
  122. }
  123. }
  124. applyReq := connect.NewRequest(&porterv1.ApplyPorterAppRequest{
  125. ProjectId: int64(project.ID),
  126. DeploymentTargetId: deploymentTargetID,
  127. App: appProto,
  128. PorterAppRevisionId: appRevisionID,
  129. })
  130. ccpResp, err := c.Config().ClusterControlPlaneClient.ApplyPorterApp(ctx, applyReq)
  131. if err != nil {
  132. err := telemetry.Error(ctx, span, err, "error calling ccp apply porter app")
  133. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  134. return
  135. }
  136. if ccpResp == nil {
  137. err := telemetry.Error(ctx, span, err, "ccp resp is nil")
  138. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  139. return
  140. }
  141. if ccpResp.Msg == nil {
  142. err := telemetry.Error(ctx, span, err, "ccp resp msg is nil")
  143. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  144. return
  145. }
  146. if ccpResp.Msg.PorterAppRevisionId == "" {
  147. err := telemetry.Error(ctx, span, err, "ccp resp app revision id is nil")
  148. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  149. return
  150. }
  151. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "resp-app-revision-id", Value: ccpResp.Msg.PorterAppRevisionId})
  152. if ccpResp.Msg.CliAction == porterv1.EnumCLIAction_ENUM_CLI_ACTION_UNSPECIFIED {
  153. err := telemetry.Error(ctx, span, err, "ccp resp cli action is nil")
  154. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  155. return
  156. }
  157. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "cli-action", Value: ccpResp.Msg.CliAction.String()})
  158. response := &ApplyPorterAppResponse{
  159. AppRevisionId: ccpResp.Msg.PorterAppRevisionId,
  160. CLIAction: ccpResp.Msg.CliAction,
  161. }
  162. c.WriteResult(w, r, response)
  163. }
  164. // addPorterSubdomainsIfNecessary adds porter subdomains to the app proto if a web service is changed to private and has no domains
  165. func addPorterSubdomainsIfNecessary(ctx context.Context, app *porterv1.PorterApp, createSubdomainInput porter_app.CreatePorterSubdomainInput) (*porterv1.PorterApp, error) {
  166. for serviceName, service := range app.Services {
  167. if service.Type == porterv1.ServiceType_SERVICE_TYPE_WEB {
  168. if service.GetWebConfig() == nil {
  169. return app, fmt.Errorf("web service %s does not contain web config", serviceName)
  170. }
  171. webConfig := service.GetWebConfig()
  172. if !webConfig.Private && len(webConfig.Domains) == 0 {
  173. subdomain, err := porter_app.CreatePorterSubdomain(ctx, createSubdomainInput)
  174. if err != nil {
  175. return app, fmt.Errorf("error creating subdomain: %w", err)
  176. }
  177. if subdomain == "" {
  178. return app, errors.New("response subdomain is empty")
  179. }
  180. webConfig.Domains = []*porterv1.Domain{
  181. {Name: subdomain},
  182. }
  183. service.Config = &porterv1.Service_WebConfig{WebConfig: webConfig}
  184. }
  185. }
  186. }
  187. return app, nil
  188. }