provision_eks.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package provision
  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. "gorm.io/gorm"
  15. )
  16. type ProvisionEKSHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. }
  19. func NewProvisionEKSHandler(
  20. config *config.Config,
  21. decoderValidator shared.RequestDecoderValidator,
  22. writer shared.ResultWriter,
  23. ) *ProvisionEKSHandler {
  24. return &ProvisionEKSHandler{
  25. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  26. }
  27. }
  28. func (c *ProvisionEKSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  29. // read the user and project from context
  30. user, _ := r.Context().Value(types.UserScope).(*models.User)
  31. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  32. request := &types.CreateEKSInfraRequest{
  33. ProjectID: proj.ID,
  34. }
  35. if ok := c.DecodeAndValidate(w, r, request); !ok {
  36. return
  37. }
  38. // get the AWS integration
  39. awsInt, err := c.Repo().AWSIntegration().ReadAWSIntegration(proj.ID, request.AWSIntegrationID)
  40. if err != nil {
  41. if err == gorm.ErrRecordNotFound {
  42. c.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  43. } else {
  44. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  45. }
  46. return
  47. }
  48. suffix, err := repository.GenerateRandomBytes(6)
  49. if err != nil {
  50. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  51. return
  52. }
  53. infra := &models.Infra{
  54. Kind: types.InfraEKS,
  55. ProjectID: proj.ID,
  56. Suffix: suffix,
  57. Status: types.StatusCreating,
  58. AWSIntegrationID: request.AWSIntegrationID,
  59. }
  60. // handle write to the database
  61. infra, err = c.Repo().Infra().CreateInfra(infra)
  62. if err != nil {
  63. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  64. return
  65. }
  66. // launch provisioning pod
  67. _, err = c.Config().ProvisionerAgent.ProvisionEKS(
  68. &kubernetes.SharedProvisionOpts{
  69. ProjectID: proj.ID,
  70. Repo: c.Repo(),
  71. Infra: infra,
  72. Operation: provisioner.Apply,
  73. PGConf: c.Config().DBConf,
  74. RedisConf: c.Config().RedisConf,
  75. ProvImageTag: c.Config().ServerConf.ProvisionerImageTag,
  76. ProvImagePullSecret: c.Config().ServerConf.ProvisionerImagePullSecret,
  77. },
  78. awsInt,
  79. request.EKSName,
  80. request.MachineType,
  81. )
  82. if err != nil {
  83. infra.Status = types.StatusError
  84. infra, _ = c.Repo().Infra().UpdateInfra(infra)
  85. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  86. return
  87. }
  88. c.Config().AnalyticsClient.Track(analytics.ClusterProvisioningStartTrack(
  89. &analytics.ClusterProvisioningStartTrackOpts{
  90. ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(user.ID, proj.ID),
  91. ClusterType: types.InfraEKS,
  92. InfraID: infra.ID,
  93. },
  94. ))
  95. c.WriteResult(w, r, infra.ToInfraType())
  96. }