update.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package api_contract
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "net/http"
  6. "github.com/golang/protobuf/jsonpb"
  7. "github.com/nats-io/nats.go"
  8. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  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/types"
  14. "github.com/porter-dev/porter/internal/models"
  15. "google.golang.org/protobuf/proto"
  16. )
  17. type APIContractUpdateHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. }
  20. func NewAPIContractUpdateHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *APIContractUpdateHandler {
  25. return &APIContractUpdateHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. }
  28. }
  29. // ServeHTTP parses the Porter API contract for validity, and forwards the requests for handling on to another service
  30. // For now, this handling cluster creation only, by insertin a row into the cluster table in order to create an ID for this cluster, as well as stores the raw request JSON for updating later
  31. func (c *APIContractUpdateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  32. var apiContract porterv1.Contract
  33. ctx := r.Context()
  34. if ok := c.DecodeAndValidate(w, r, &apiContract); !ok {
  35. return
  36. }
  37. // handle cluster object for now
  38. cl := apiContract.Cluster
  39. if cl.ClusterId == 0 {
  40. dbClusterInput := models.Cluster{
  41. ProjectID: uint(cl.ProjectId),
  42. Status: types.UpdatingUnavailable,
  43. ProvisionedBy: "CAPI",
  44. CloudProvider: "AWS",
  45. CloudProviderCredentialIdentifier: cl.CloudProviderCredentialsId,
  46. Name: cl.GetEksKind().ClusterName,
  47. }
  48. dbCluster, err := c.Config().Repo.Cluster().CreateCluster(&dbClusterInput)
  49. if err != nil {
  50. e := fmt.Errorf("error creating new cluster: %w", err)
  51. c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  52. return
  53. }
  54. apiContract.Cluster.ClusterId = int32(dbCluster.ID)
  55. }
  56. var jpbm jsonpb.Marshaler
  57. by, err := jpbm.MarshalToString(&apiContract)
  58. if err != nil {
  59. e := fmt.Errorf("error marshalling api contract: %w", err)
  60. c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  61. return
  62. }
  63. b64 := base64.StdEncoding.EncodeToString([]byte(by))
  64. apiContractRevision := models.APIContractRevision{
  65. ClusterID: int(cl.ClusterId),
  66. ProjectID: int(cl.ProjectId),
  67. Base64Contract: string(b64),
  68. }
  69. contractRevision, err := c.Config().Repo.APIContractRevisioner().Insert(ctx, apiContractRevision)
  70. if err != nil {
  71. e := fmt.Errorf("error creating new capi config: %w", err)
  72. c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  73. return
  74. }
  75. // This gates the cluster actually being provisioned by CAPI
  76. // This can be removed whenever we are able to run NATS and CCP locally, easier
  77. kubeBy, err := proto.Marshal(&apiContract)
  78. if err != nil {
  79. e := fmt.Errorf("error marshalling proto: %w", err)
  80. c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  81. return
  82. }
  83. if !c.Config().DisableCAPIProvisioner {
  84. subject := "porter.system.infrastructure.update"
  85. _, err = c.Config().NATS.JetStream.Publish(subject, kubeBy, nats.Context(ctx))
  86. if err != nil {
  87. e := fmt.Errorf("error publishing cluster for creation: %w", err)
  88. c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
  89. return
  90. }
  91. }
  92. w.WriteHeader(http.StatusCreated)
  93. c.WriteResult(w, r, contractRevision)
  94. }