delete_deployment.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package environment
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "github.com/google/go-github/v41/github"
  8. "github.com/porter-dev/porter/api/server/authz"
  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/server/shared/requestutils"
  14. "github.com/porter-dev/porter/api/types"
  15. "github.com/porter-dev/porter/internal/models"
  16. "gorm.io/gorm"
  17. )
  18. type DeleteDeploymentHandler struct {
  19. handlers.PorterHandlerReadWriter
  20. authz.KubernetesAgentGetter
  21. }
  22. func NewDeleteDeploymentHandler(
  23. config *config.Config,
  24. decoderValidator shared.RequestDecoderValidator,
  25. writer shared.ResultWriter,
  26. ) *DeleteDeploymentHandler {
  27. return &DeleteDeploymentHandler{
  28. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  29. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  30. }
  31. }
  32. func (c *DeleteDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  33. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  34. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  35. if !project.PreviewEnvsEnabled {
  36. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(errPreviewProjectDisabled, http.StatusForbidden))
  37. return
  38. } else if !cluster.PreviewEnvsEnabled {
  39. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(errPreviewClusterDisabled, http.StatusForbidden))
  40. return
  41. }
  42. deplID, reqErr := requestutils.GetURLParamUint(r, "deployment_id")
  43. if reqErr != nil {
  44. c.HandleAPIError(w, r, apierrors.NewErrInternal(reqErr))
  45. return
  46. }
  47. // read the deployment
  48. depl, err := c.Repo().Environment().ReadDeploymentByID(project.ID, cluster.ID, deplID)
  49. if err != nil {
  50. if errors.Is(err, gorm.ErrRecordNotFound) {
  51. c.HandleAPIError(w, r, apierrors.NewErrNotFound(fmt.Errorf("deployment id not found in cluster and project")))
  52. return
  53. }
  54. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  55. return
  56. }
  57. // delete corresponding namespace
  58. agent, err := c.GetAgent(r, cluster, "")
  59. if err != nil {
  60. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  61. return
  62. }
  63. // make sure we do not delete any kubernetes "system" namespaces
  64. if !isSystemNamespace(depl.Namespace) {
  65. err = agent.DeleteNamespace(depl.Namespace)
  66. if err != nil {
  67. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error deleting preview deployment namespace: %w",
  68. err)))
  69. return
  70. }
  71. }
  72. // check that the environment belongs to the project and cluster IDs
  73. env, err := c.Repo().Environment().ReadEnvironmentByID(project.ID, cluster.ID, depl.EnvironmentID)
  74. if err != nil {
  75. if errors.Is(err, gorm.ErrRecordNotFound) {
  76. c.HandleAPIError(w, r, apierrors.NewErrForbidden(fmt.Errorf("environment id not found in cluster and project")))
  77. return
  78. }
  79. c.HandleAPIError(w, r, apierrors.NewErrInternal(reqErr))
  80. return
  81. }
  82. depl.Status = types.DeploymentStatusInactive
  83. // update the deployment to mark it inactive
  84. depl, err = c.Repo().Environment().UpdateDeployment(depl)
  85. if err != nil {
  86. if errors.Is(err, gorm.ErrRecordNotFound) {
  87. c.HandleAPIError(w, r, apierrors.NewErrNotFound(errDeploymentNotFound))
  88. return
  89. }
  90. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  91. return
  92. }
  93. client, err := getGithubClientFromEnvironment(c.Config(), env)
  94. if err != nil {
  95. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  96. return
  97. }
  98. if depl.GHDeploymentID != 0 {
  99. // set the GitHub deployment status to be inactive
  100. _, _, err := client.Repositories.CreateDeploymentStatus(
  101. context.Background(),
  102. env.GitRepoOwner,
  103. env.GitRepoName,
  104. depl.GHDeploymentID,
  105. &github.DeploymentStatusRequest{
  106. State: github.String("inactive"),
  107. },
  108. )
  109. if err != nil {
  110. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  111. fmt.Errorf("%v: %w", errGithubAPI, err), http.StatusConflict,
  112. ))
  113. return
  114. }
  115. }
  116. c.WriteResult(w, r, depl.ToDeploymentType())
  117. }