run_app_job.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // RunAppJobHandler handles requests to the /apps/{porter_app_name}/run endpoint
  17. type RunAppJobHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. authz.KubernetesAgentGetter
  20. }
  21. // NewRunAppJobHandler returns a new AppJobRunHandler
  22. func NewRunAppJobHandler(
  23. config *config.Config,
  24. decoderValidator shared.RequestDecoderValidator,
  25. writer shared.ResultWriter,
  26. ) *RunAppJobHandler {
  27. return &RunAppJobHandler{
  28. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  29. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  30. }
  31. }
  32. // RunAppJobRequest is the request object for the /apps/{porter_app_name}/run endpoint
  33. type RunAppJobRequest struct {
  34. ServiceName string `json:"service_name"`
  35. // DeploymentTargetID is the id of the deployment target the job should be run against. One of DeploymentTargetID or DeploymentTargetName is required
  36. DeploymentTargetID string `json:"deployment_target_id"`
  37. // DeploymentTargetName is the name of the deployment target the job should be run against. One of DeploymentTargetID or DeploymentTargetName is required
  38. DeploymentTargetName string `json:"deployment_target_name"`
  39. // Optional field to override the default run command for the job
  40. RunCommand string `json:"run_command"`
  41. // Image is an optional field to override the image used for the job
  42. Image Image `json:"image,omitempty"`
  43. }
  44. // RunAppJobResponse is the response object for the /apps/{porter_app_name}/run endpoint
  45. type RunAppJobResponse struct {
  46. JobRunID string `json:"job_run_id"`
  47. }
  48. // ServeHTTP runs a one-off command in the same environment as the provided service, app and deployment target
  49. func (c *RunAppJobHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  50. ctx, span := telemetry.NewSpan(r.Context(), "serve-app-job-run")
  51. defer span.End()
  52. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  53. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  54. appName, reqErr := requestutils.GetURLParamString(r, types.URLParamPorterAppName)
  55. if reqErr != nil {
  56. e := telemetry.Error(ctx, span, reqErr, "error parsing app name from url")
  57. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest))
  58. return
  59. }
  60. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: appName})
  61. request := &RunAppJobRequest{}
  62. if ok := c.DecodeAndValidate(w, r, request); !ok {
  63. err := telemetry.Error(ctx, span, nil, "error decoding request")
  64. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  65. return
  66. }
  67. if request.ServiceName == "" {
  68. err := telemetry.Error(ctx, span, nil, "service name is required")
  69. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  70. return
  71. }
  72. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "service-name", Value: request.ServiceName})
  73. deploymentTargetName := request.DeploymentTargetName
  74. if request.DeploymentTargetName == "" && request.DeploymentTargetID == "" {
  75. defaultDeploymentTarget, err := defaultDeploymentTarget(ctx, defaultDeploymentTargetInput{
  76. ProjectID: project.ID,
  77. ClusterID: cluster.ID,
  78. ClusterControlPlaneClient: c.Config().ClusterControlPlaneClient,
  79. })
  80. if err != nil {
  81. err := telemetry.Error(ctx, span, err, "error getting default deployment target")
  82. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  83. return
  84. }
  85. deploymentTargetName = defaultDeploymentTarget.Name
  86. }
  87. telemetry.WithAttributes(span,
  88. telemetry.AttributeKV{Key: "deployment-target-name", Value: deploymentTargetName},
  89. telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID},
  90. )
  91. var commandOptional *string
  92. if request.RunCommand != "" {
  93. commandOptional = &request.RunCommand
  94. }
  95. var imageOverrideOptional *porterv1.AppImage
  96. if request.Image.Tag != "" {
  97. telemetry.WithAttributes(span,
  98. telemetry.AttributeKV{Key: "image-override-repo", Value: request.Image.Repository},
  99. telemetry.AttributeKV{Key: "image-override-tag", Value: request.Image.Tag},
  100. )
  101. imageOverrideOptional = &porterv1.AppImage{
  102. Repository: request.Image.Repository,
  103. Tag: request.Image.Tag,
  104. }
  105. }
  106. manualServiceRunReq := connect.NewRequest(&porterv1.ManualServiceRunRequest{
  107. ProjectId: int64(project.ID),
  108. AppName: appName,
  109. ServiceName: request.ServiceName,
  110. Command: commandOptional,
  111. Image: imageOverrideOptional,
  112. DeploymentTargetIdentifier: &porterv1.DeploymentTargetIdentifier{
  113. Id: request.DeploymentTargetID,
  114. Name: deploymentTargetName,
  115. },
  116. })
  117. serviceResp, err := c.Config().ClusterControlPlaneClient.ManualServiceRun(ctx, manualServiceRunReq)
  118. if err != nil {
  119. err := telemetry.Error(ctx, span, err, "error getting app helm values from cluster control plane client")
  120. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  121. return
  122. }
  123. if serviceResp == nil || serviceResp.Msg == nil {
  124. err := telemetry.Error(ctx, span, err, "app helm values resp is nil")
  125. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  126. return
  127. }
  128. response := RunAppJobResponse{
  129. JobRunID: serviceResp.Msg.JobRunId,
  130. }
  131. c.WriteResult(w, r, response)
  132. }