delete_deployment.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. deplID, reqErr := requestutils.GetURLParamUint(r, "deployment_id")
  36. if reqErr != nil {
  37. c.HandleAPIError(w, r, apierrors.NewErrInternal(reqErr))
  38. return
  39. }
  40. // read the deployment
  41. depl, err := c.Repo().Environment().ReadDeploymentByID(project.ID, cluster.ID, deplID)
  42. if err != nil {
  43. if errors.Is(err, gorm.ErrRecordNotFound) {
  44. c.HandleAPIError(w, r, apierrors.NewErrNotFound(errDeploymentNotFound))
  45. return
  46. }
  47. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  48. return
  49. }
  50. // delete corresponding namespace
  51. agent, err := c.GetAgent(r, cluster, "")
  52. if err != nil {
  53. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  54. return
  55. }
  56. // make sure we do not delete any kubernetes "system" namespaces
  57. if !isSystemNamespace(depl.Namespace) {
  58. agent.DeleteNamespace(depl.Namespace)
  59. }
  60. // check that the environment belongs to the project and cluster IDs
  61. env, err := c.Repo().Environment().ReadEnvironmentByID(project.ID, cluster.ID, depl.EnvironmentID)
  62. if err != nil {
  63. if errors.Is(err, gorm.ErrRecordNotFound) {
  64. c.HandleAPIError(w, r, apierrors.NewErrNotFound(errEnvironmentNotFound))
  65. return
  66. }
  67. c.HandleAPIError(w, r, apierrors.NewErrInternal(reqErr))
  68. return
  69. }
  70. _, err = c.Repo().Environment().DeleteDeployment(depl)
  71. if err != nil {
  72. if errors.Is(err, gorm.ErrRecordNotFound) {
  73. c.HandleAPIError(w, r, apierrors.NewErrNotFound(errDeploymentNotFound))
  74. return
  75. }
  76. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  77. return
  78. }
  79. client, err := getGithubClientFromEnvironment(c.Config(), env)
  80. if err != nil {
  81. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  82. return
  83. }
  84. if depl.GHDeploymentID != 0 {
  85. // set the GitHub deployment status to be inactive
  86. _, _, err := client.Repositories.CreateDeploymentStatus(
  87. context.Background(),
  88. env.GitRepoOwner,
  89. env.GitRepoName,
  90. depl.GHDeploymentID,
  91. &github.DeploymentStatusRequest{
  92. State: github.String("inactive"),
  93. },
  94. )
  95. if err != nil {
  96. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  97. fmt.Errorf("%v: %w", errGithubAPI, err), http.StatusConflict,
  98. ))
  99. return
  100. }
  101. }
  102. c.WriteResult(w, r, depl.ToDeploymentType())
  103. }