provision_eks.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/kubernetes"
  10. "github.com/porter-dev/porter/internal/kubernetes/provisioner"
  11. "github.com/porter-dev/porter/internal/models"
  12. "github.com/porter-dev/porter/internal/repository"
  13. "gorm.io/gorm"
  14. )
  15. type ProvisionEKSHandler struct {
  16. handlers.PorterHandlerReadWriter
  17. }
  18. func NewProvisionEKSHandler(
  19. config *config.Config,
  20. decoderValidator shared.RequestDecoderValidator,
  21. writer shared.ResultWriter,
  22. ) *ProvisionEKSHandler {
  23. return &ProvisionEKSHandler{
  24. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  25. }
  26. }
  27. func (c *ProvisionEKSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. // read the project from context
  29. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  30. request := &types.CreateEKSInfraRequest{}
  31. if ok := c.DecodeAndValidate(w, r, request); !ok {
  32. return
  33. }
  34. // get the AWS integration
  35. awsInt, err := c.Repo().AWSIntegration().ReadAWSIntegration(proj.ID, request.AWSIntegrationID)
  36. if err != nil {
  37. if err == gorm.ErrRecordNotFound {
  38. c.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  39. } else {
  40. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  41. }
  42. return
  43. }
  44. suffix, err := repository.GenerateRandomBytes(6)
  45. if err != nil {
  46. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  47. return
  48. }
  49. infra := &models.Infra{
  50. Kind: types.InfraEKS,
  51. ProjectID: proj.ID,
  52. Suffix: suffix,
  53. Status: types.StatusCreating,
  54. AWSIntegrationID: request.AWSIntegrationID,
  55. }
  56. // handle write to the database
  57. infra, err = c.Repo().Infra().CreateInfra(infra)
  58. if err != nil {
  59. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  60. return
  61. }
  62. // launch provisioning pod
  63. _, err = c.Config().ProvisionerAgent.ProvisionEKS(
  64. &kubernetes.SharedProvisionOpts{
  65. ProjectID: proj.ID,
  66. Repo: c.Repo(),
  67. Infra: infra,
  68. Operation: provisioner.Apply,
  69. PGConf: c.Config().DBConf,
  70. RedisConf: c.Config().RedisConf,
  71. ProvImageTag: c.Config().ServerConf.ProvisionerImageTag,
  72. ProvImagePullSecret: c.Config().ServerConf.ProvisionerImagePullSecret,
  73. },
  74. awsInt,
  75. request.EKSName,
  76. request.MachineType,
  77. )
  78. if err != nil {
  79. infra.Status = types.StatusError
  80. infra, _ = c.Repo().Infra().UpdateInfra(infra)
  81. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  82. return
  83. }
  84. c.WriteResult(w, r, infra.ToInfraType())
  85. }
  86. // // HandleProvisionAWSEKSInfra provisions a new aws EKS instance for a project
  87. // func (app *App) HandleProvisionAWSEKSInfra(w http.ResponseWriter, r *http.Request) {
  88. // projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  89. // userID, err := app.getUserIDFromRequest(r)
  90. // if err != nil || projID == 0 {
  91. // app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  92. // return
  93. // }
  94. // form := &forms.CreateEKSInfra{
  95. // ProjectID: uint(projID),
  96. // }
  97. // // decode from JSON to form value
  98. // if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  99. // app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  100. // return
  101. // }
  102. // // validate the form
  103. // if err := app.validator.Struct(form); err != nil {
  104. // app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  105. // return
  106. // }
  107. // // convert the form to an aws infra instance
  108. // infra, err := form.ToInfra()
  109. // if err != nil {
  110. // app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  111. // return
  112. // }
  113. // // handle write to the database
  114. // infra, err = app.Repo.Infra().CreateInfra(infra)
  115. // if err != nil {
  116. // app.handleErrorDataWrite(err, w)
  117. // return
  118. // }
  119. // awsInt, err := app.Repo.AWSIntegration().ReadAWSIntegration(infra.AWSIntegrationID)
  120. // if err != nil {
  121. // infra.Status = types.StatusError
  122. // infra, _ = app.Repo.Infra().UpdateInfra(infra)
  123. // app.handleErrorDataRead(err, w)
  124. // return
  125. // }
  126. // // launch provisioning pod
  127. // _, err = app.ProvisionerAgent.ProvisionEKS(
  128. // uint(projID),
  129. // awsInt,
  130. // form.EKSName,
  131. // form.MachineType,
  132. // app.Repo,
  133. // infra,
  134. // provisioner.Apply,
  135. // &app.DBConf,
  136. // app.RedisConf,
  137. // app.ServerConf.ProvisionerImageTag,
  138. // app.ServerConf.ProvisionerImagePullSecret,
  139. // )
  140. // if err != nil {
  141. // infra.Status = types.StatusError
  142. // infra, _ = app.Repo.Infra().UpdateInfra(infra)
  143. // app.handleErrorInternal(err, w)
  144. // return
  145. // }
  146. // app.Logger.Info().Msgf("New aws eks infra created: %d", infra.ID)
  147. // app.analyticsClient.Track(analytics.CreateSegmentNewClusterEvent(
  148. // &analytics.NewClusterEventOpts{
  149. // UserId: fmt.Sprintf("%d", userID),
  150. // ProjId: fmt.Sprintf("%d", infra.ProjectID),
  151. // ClusterName: form.EKSName,
  152. // ClusterType: "EKS",
  153. // EventType: "provisioned",
  154. // },
  155. // ))
  156. // w.WriteHeader(http.StatusCreated)
  157. // infraExt := infra.ToInfraType()
  158. // if err := json.NewEncoder(w).Encode(infraExt); err != nil {
  159. // app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  160. // return
  161. // }
  162. // }