2
0

create_deployment_by_cluster.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package environment
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "github.com/porter-dev/porter/api/server/authz"
  8. "github.com/porter-dev/porter/api/server/handlers"
  9. "github.com/porter-dev/porter/api/server/shared"
  10. "github.com/porter-dev/porter/api/server/shared/apierrors"
  11. "github.com/porter-dev/porter/api/server/shared/config"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/models"
  14. "gorm.io/gorm"
  15. )
  16. type CreateDeploymentByClusterHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. authz.KubernetesAgentGetter
  19. }
  20. func NewCreateDeploymentByClusterHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *CreateDeploymentByClusterHandler {
  25. return &CreateDeploymentByClusterHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  28. }
  29. }
  30. func (c *CreateDeploymentByClusterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  31. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  32. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  33. request := &types.CreateDeploymentRequest{}
  34. if ok := c.DecodeAndValidate(w, r, request); !ok {
  35. return
  36. }
  37. // read the environment to get the environment id
  38. env, err := c.Repo().Environment().ReadEnvironmentByOwnerRepoName(
  39. project.ID, cluster.ID, request.RepoOwner, request.RepoName,
  40. )
  41. if err != nil {
  42. if errors.Is(err, gorm.ErrRecordNotFound) {
  43. c.HandleAPIError(w, r, apierrors.NewErrNotFound(
  44. fmt.Errorf("error creating deployment: %w", errEnvironmentNotFound)),
  45. )
  46. return
  47. }
  48. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  49. return
  50. }
  51. // create deployment on GitHub API
  52. client, err := getGithubClientFromEnvironment(c.Config(), env)
  53. if err != nil {
  54. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  55. return
  56. }
  57. // add a check for Github PR status
  58. prClosed, err := isGithubPRClosed(client, request.RepoOwner, request.RepoName, int(request.PullRequestID))
  59. if err != nil {
  60. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusConflict))
  61. return
  62. }
  63. if prClosed {
  64. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  65. fmt.Errorf("attempting to create deployment for a closed github PR"), http.StatusConflict,
  66. ))
  67. return
  68. }
  69. ghDeployment, err := createGithubDeployment(client, env, request.PRBranchFrom, request.ActionID)
  70. if err != nil {
  71. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusConflict))
  72. return
  73. }
  74. // create the deployment
  75. depl, err := c.Repo().Environment().CreateDeployment(&models.Deployment{
  76. EnvironmentID: env.ID,
  77. Namespace: request.Namespace,
  78. Status: types.DeploymentStatusCreating,
  79. PullRequestID: request.PullRequestID,
  80. GHDeploymentID: ghDeployment.GetID(),
  81. RepoOwner: request.GitHubMetadata.RepoOwner,
  82. RepoName: request.GitHubMetadata.RepoName,
  83. PRName: request.GitHubMetadata.PRName,
  84. CommitSHA: request.GitHubMetadata.CommitSHA,
  85. PRBranchFrom: request.GitHubMetadata.PRBranchFrom,
  86. PRBranchInto: request.GitHubMetadata.PRBranchInto,
  87. })
  88. if err != nil {
  89. // try to delete the GitHub deployment
  90. _, err = client.Repositories.DeleteDeployment(
  91. context.Background(),
  92. env.GitRepoOwner,
  93. env.GitRepoName,
  94. ghDeployment.GetID(),
  95. )
  96. if err != nil {
  97. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("%v: %w", errGithubAPI, err),
  98. http.StatusConflict))
  99. return
  100. }
  101. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error creating deployment: %w", err)))
  102. return
  103. }
  104. c.WriteResult(w, r, depl.ToDeploymentType())
  105. }