delete.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package infra
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/handlers"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/apierrors"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/analytics"
  10. "github.com/porter-dev/porter/internal/kubernetes"
  11. "github.com/porter-dev/porter/internal/kubernetes/provisioner"
  12. "github.com/porter-dev/porter/internal/models"
  13. "github.com/porter-dev/porter/internal/repository"
  14. )
  15. type InfraDeleteHandler struct {
  16. handlers.PorterHandlerReadWriter
  17. }
  18. func NewInfraDeleteHandler(
  19. config *config.Config,
  20. decoderValidator shared.RequestDecoderValidator,
  21. writer shared.ResultWriter,
  22. ) *InfraDeleteHandler {
  23. return &InfraDeleteHandler{
  24. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  25. }
  26. }
  27. func (c *InfraDeleteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. infra, _ := r.Context().Value(types.InfraScope).(*models.Infra)
  29. request := &types.DeleteInfraRequest{}
  30. if ok := c.DecodeAndValidate(w, r, request); !ok {
  31. return
  32. }
  33. if infra.Kind == types.InfraDOKS || infra.Kind == types.InfraGKE || infra.Kind == types.InfraEKS {
  34. c.Config().AnalyticsClient.Track(analytics.ClusterDestroyingStartTrack(
  35. &analytics.ClusterDestroyingStartTrackOpts{
  36. ClusterScopedTrackOpts: analytics.GetClusterScopedTrackOpts(infra.CreatedByUserID, infra.ProjectID, 0),
  37. ClusterType: infra.Kind,
  38. InfraID: infra.ID,
  39. },
  40. ))
  41. }
  42. infra.Status = types.StatusDestroying
  43. infra, err := c.Repo().Infra().UpdateInfra(infra)
  44. if err != nil {
  45. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  46. return
  47. }
  48. switch infra.Kind {
  49. case types.InfraECR:
  50. err = destroyECR(c.Repo(), c.Config(), infra, request.Name)
  51. case types.InfraEKS:
  52. err = destroyEKS(c.Repo(), c.Config(), infra, request.Name)
  53. case types.InfraDOCR:
  54. err = destroyDOCR(c.Repo(), c.Config(), infra, request.Name)
  55. case types.InfraDOKS:
  56. err = destroyDOKS(c.Repo(), c.Config(), infra, request.Name)
  57. case types.InfraGKE:
  58. err = destroyGKE(c.Repo(), c.Config(), infra, request.Name)
  59. }
  60. if err != nil {
  61. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  62. return
  63. }
  64. }
  65. func destroyECR(repo repository.Repository, conf *config.Config, infra *models.Infra, name string) error {
  66. awsInt, err := repo.AWSIntegration().ReadAWSIntegration(infra.ProjectID, infra.AWSIntegrationID)
  67. if err != nil {
  68. return err
  69. }
  70. _, err = conf.ProvisionerAgent.ProvisionECR(
  71. &kubernetes.SharedProvisionOpts{
  72. ProjectID: infra.ProjectID,
  73. Repo: repo,
  74. Infra: infra,
  75. Operation: provisioner.Destroy,
  76. PGConf: conf.DBConf,
  77. RedisConf: conf.RedisConf,
  78. ProvImageTag: conf.ServerConf.ProvisionerImageTag,
  79. ProvImagePullSecret: conf.ServerConf.ProvisionerImagePullSecret,
  80. },
  81. awsInt,
  82. name,
  83. )
  84. return err
  85. }
  86. func destroyEKS(repo repository.Repository, conf *config.Config, infra *models.Infra, name string) error {
  87. awsInt, err := repo.AWSIntegration().ReadAWSIntegration(infra.ProjectID, infra.AWSIntegrationID)
  88. if err != nil {
  89. return err
  90. }
  91. _, err = conf.ProvisionerAgent.ProvisionEKS(
  92. &kubernetes.SharedProvisionOpts{
  93. ProjectID: infra.ProjectID,
  94. Repo: repo,
  95. Infra: infra,
  96. Operation: provisioner.Destroy,
  97. PGConf: conf.DBConf,
  98. RedisConf: conf.RedisConf,
  99. ProvImageTag: conf.ServerConf.ProvisionerImageTag,
  100. ProvImagePullSecret: conf.ServerConf.ProvisionerImagePullSecret,
  101. },
  102. awsInt,
  103. name,
  104. "",
  105. )
  106. return err
  107. }
  108. func destroyDOCR(repo repository.Repository, conf *config.Config, infra *models.Infra, name string) error {
  109. doInt, err := repo.OAuthIntegration().ReadOAuthIntegration(infra.ProjectID, infra.DOIntegrationID)
  110. if err != nil {
  111. return err
  112. }
  113. _, err = conf.ProvisionerAgent.ProvisionDOCR(
  114. &kubernetes.SharedProvisionOpts{
  115. ProjectID: infra.ProjectID,
  116. Repo: repo,
  117. Infra: infra,
  118. Operation: provisioner.Destroy,
  119. PGConf: conf.DBConf,
  120. RedisConf: conf.RedisConf,
  121. ProvImageTag: conf.ServerConf.ProvisionerImageTag,
  122. ProvImagePullSecret: conf.ServerConf.ProvisionerImagePullSecret,
  123. },
  124. doInt,
  125. conf.DOConf,
  126. name,
  127. "",
  128. )
  129. return err
  130. }
  131. func destroyDOKS(repo repository.Repository, conf *config.Config, infra *models.Infra, name string) error {
  132. doInt, err := repo.OAuthIntegration().ReadOAuthIntegration(infra.ProjectID, infra.DOIntegrationID)
  133. if err != nil {
  134. return err
  135. }
  136. _, err = conf.ProvisionerAgent.ProvisionDOKS(
  137. &kubernetes.SharedProvisionOpts{
  138. ProjectID: infra.ProjectID,
  139. Repo: repo,
  140. Infra: infra,
  141. Operation: provisioner.Destroy,
  142. PGConf: conf.DBConf,
  143. RedisConf: conf.RedisConf,
  144. ProvImageTag: conf.ServerConf.ProvisionerImageTag,
  145. ProvImagePullSecret: conf.ServerConf.ProvisionerImagePullSecret,
  146. },
  147. doInt,
  148. conf.DOConf,
  149. "",
  150. name,
  151. )
  152. return err
  153. }
  154. func destroyGKE(repo repository.Repository, conf *config.Config, infra *models.Infra, name string) error {
  155. gcpInt, err := repo.GCPIntegration().ReadGCPIntegration(infra.ProjectID, infra.GCPIntegrationID)
  156. if err != nil {
  157. return err
  158. }
  159. _, err = conf.ProvisionerAgent.ProvisionGKE(
  160. &kubernetes.SharedProvisionOpts{
  161. ProjectID: infra.ProjectID,
  162. Repo: repo,
  163. Infra: infra,
  164. Operation: provisioner.Destroy,
  165. PGConf: conf.DBConf,
  166. RedisConf: conf.RedisConf,
  167. ProvImageTag: conf.ServerConf.ProvisionerImageTag,
  168. ProvImagePullSecret: conf.ServerConf.ProvisionerImagePullSecret,
  169. },
  170. gcpInt,
  171. name,
  172. )
  173. return err
  174. }