Quellcode durchsuchen

moved updating overall contract logic to grpc endpoint in cluster control plane

Stefan McShane vor 3 Jahren
Ursprung
Commit
08f221f5d3
3 geänderte Dateien mit 106 neuen und 83 gelöschten Zeilen
  1. 103 80
      api/server/handlers/api_contract/update.go
  2. 1 1
      go.mod
  3. 2 2
      go.sum

+ 103 - 80
api/server/handlers/api_contract/update.go

@@ -1,19 +1,17 @@
 package api_contract
 
 import (
-	"encoding/base64"
-	"errors"
 	"fmt"
 	"net/http"
 
-	"github.com/nats-io/nats.go"
+	"github.com/bufbuild/connect-go"
+	"github.com/google/uuid"
 	helpers "github.com/porter-dev/api-contracts/generated/go/helpers"
 	porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"
 	"github.com/porter-dev/porter/api/server/shared/apierrors"
 	"github.com/porter-dev/porter/api/server/shared/config"
-	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/internal/models"
 )
 
@@ -45,89 +43,114 @@ func (c *APIContractUpdateHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 		return
 	}
 
-	if apiContract.Cluster == nil {
-		e := errors.New("missing cluster object")
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
-		return
-	}
-
-	cl := apiContract.Cluster
-
-	if cl.CloudProviderCredentialsId == "" {
-		e := errors.New("missing cloud_provider_credential_identifier")
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
-		return
-	}
-
-	if cl.GetEksKind() == nil {
-		e := errors.New("missing eks_kind_values")
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
-		return
-	}
-
-	if cl.ClusterId == 0 {
-		dbClusterInput := models.Cluster{
-			ProjectID:                         uint(cl.ProjectId),
-			Status:                            types.UpdatingUnavailable,
-			ProvisionedBy:                     "CAPI",
-			CloudProvider:                     "AWS",
-			CloudProviderCredentialIdentifier: cl.CloudProviderCredentialsId,
-			Name:                              cl.GetEksKind().ClusterName,
-			VanityName:                        cl.GetEksKind().ClusterName,
+	if c.Config().DisableCAPIProvisioner {
+		// return dummy data if capi provisioner disabled
+		// remove this stub when we can spin up all services locally, easily
+		rev := models.APIContractRevision{
+			ID:        uuid.New(),
+			ClusterID: int(apiContract.Cluster.ClusterId),
+			ProjectID: int(apiContract.Cluster.ProjectId),
 		}
-		dbCluster, err := c.Config().Repo.Cluster().CreateCluster(&dbClusterInput)
-		if err != nil {
-			e := fmt.Errorf("error creating new cluster: %w", err)
-			c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
-			return
-		}
-		apiContract.Cluster.ClusterId = int32(dbCluster.ID)
+		w.WriteHeader(http.StatusCreated)
+		c.WriteResult(w, r, rev)
 	}
 
-	by, err := helpers.MarshalContractObject(ctx, &apiContract)
+	updateRequest := connect.NewRequest(&porterv1.UpdateContractRequest{
+		Contract: &apiContract,
+	})
+	revision, err := c.Config().ClusterControlPlaneClient.UpdateContract(ctx, updateRequest)
 	if err != nil {
-		e := fmt.Errorf("error marshalling api contract: %w", err)
+		e := fmt.Errorf("error sending contract for update: %w", err)
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
 		return
 	}
-	b64 := base64.StdEncoding.EncodeToString([]byte(by))
-
-	apiContractRevision := models.APIContractRevision{
-		ClusterID:      int(cl.ClusterId),
-		ProjectID:      int(cl.ProjectId),
-		Base64Contract: string(b64),
-	}
-
-	contractRevision, err := c.Config().Repo.APIContractRevisioner().Insert(ctx, apiContractRevision)
-	if err != nil {
-		e := fmt.Errorf("error creating new capi config: %w", err)
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
-		return
-	}
-
-	// This gates the cluster actually being provisioned by CAPI
-	// This can be removed whenever we are able to run NATS and CCP locally, easier
-	if !c.Config().DisableCAPIProvisioner {
-		resp := porterv1.ContractRevision{
-			ProjectId:  cl.ProjectId,
-			ClusterId:  cl.ClusterId,
-			RevisionId: contractRevision.ID.String(),
-		}
-		kubeBy, err := helpers.MarshalContractObject(ctx, &resp)
-		if err != nil {
-			e := fmt.Errorf("error marshalling api contract: %w", err)
-			c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
-			return
-		}
-		subject := "porter.system.infrastructure.update"
-		_, err = c.Config().NATS.JetStream.Publish(subject, kubeBy, nats.Context(ctx))
-		if err != nil {
-			e := fmt.Errorf("error publishing cluster for creation: %w", err)
-			c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
-			return
-		}
-	}
 
 	w.WriteHeader(http.StatusCreated)
-	c.WriteResult(w, r, contractRevision)
+	c.WriteResult(w, r, revision)
+
+	// if apiContract.Cluster == nil {
+	// 	e := errors.New("missing cluster object")
+	// 	c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
+	// 	return
+	// }
+
+	// cl := apiContract.Cluster
+
+	// if cl.CloudProviderCredentialsId == "" {
+	// 	e := errors.New("missing cloud_provider_credential_identifier")
+	// 	c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
+	// 	return
+	// }
+
+	// if cl.GetEksKind() == nil {
+	// 	e := errors.New("missing eks_kind_values")
+	// 	c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
+	// 	return
+	// }
+
+	// if cl.ClusterId == 0 {
+	// 	dbClusterInput := models.Cluster{
+	// 		ProjectID:                         uint(cl.ProjectId),
+	// 		Status:                            types.UpdatingUnavailable,
+	// 		ProvisionedBy:                     "CAPI",
+	// 		CloudProvider:                     "AWS",
+	// 		CloudProviderCredentialIdentifier: cl.CloudProviderCredentialsId,
+	// 		Name:                              cl.GetEksKind().ClusterName,
+	// 		VanityName:                        cl.GetEksKind().ClusterName,
+	// 	}
+	// 	dbCluster, err := c.Config().Repo.Cluster().CreateCluster(&dbClusterInput)
+	// 	if err != nil {
+	// 		e := fmt.Errorf("error creating new cluster: %w", err)
+	// 		c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
+	// 		return
+	// 	}
+	// 	apiContract.Cluster.ClusterId = int32(dbCluster.ID)
+	// }
+
+	// by, err := helpers.MarshalContractObject(ctx, &apiContract)
+	// if err != nil {
+	// 	e := fmt.Errorf("error marshalling api contract: %w", err)
+	// 	c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
+	// 	return
+	// }
+	// b64 := base64.StdEncoding.EncodeToString([]byte(by))
+
+	// apiContractRevision := models.APIContractRevision{
+	// 	ClusterID:      int(cl.ClusterId),
+	// 	ProjectID:      int(cl.ProjectId),
+	// 	Base64Contract: string(b64),
+	// }
+
+	// contractRevision, err := c.Config().Repo.APIContractRevisioner().Insert(ctx, apiContractRevision)
+	// if err != nil {
+	// 	e := fmt.Errorf("error creating new capi config: %w", err)
+	// 	c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
+	// 	return
+	// }
+
+	// // This gates the cluster actually being provisioned by CAPI
+	// // This can be removed whenever we are able to run NATS and CCP locally, easier
+	// if !c.Config().DisableCAPIProvisioner {
+	// 	resp := porterv1.ContractRevision{
+	// 		ProjectId:  cl.ProjectId,
+	// 		ClusterId:  cl.ClusterId,
+	// 		RevisionId: contractRevision.ID.String(),
+	// 	}
+	// 	kubeBy, err := helpers.MarshalContractObject(ctx, &resp)
+	// 	if err != nil {
+	// 		e := fmt.Errorf("error marshalling api contract: %w", err)
+	// 		c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
+	// 		return
+	// 	}
+	// 	subject := "porter.system.infrastructure.update"
+	// 	_, err = c.Config().NATS.JetStream.Publish(subject, kubeBy, nats.Context(ctx))
+	// 	if err != nil {
+	// 		e := fmt.Errorf("error publishing cluster for creation: %w", err)
+	// 		c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
+	// 		return
+	// 	}
+	// }
+
+	// w.WriteHeader(http.StatusCreated)
+	// c.WriteResult(w, r, contractRevision)
 }

+ 1 - 1
go.mod

@@ -74,7 +74,7 @@ require (
 	github.com/glebarez/sqlite v1.6.0
 	github.com/nats-io/nats.go v1.24.0
 	github.com/open-policy-agent/opa v0.44.0
-	github.com/porter-dev/api-contracts v0.0.39
+	github.com/porter-dev/api-contracts v0.0.41
 	github.com/santhosh-tekuri/jsonschema/v5 v5.0.1
 	github.com/stefanmcshane/helm v0.0.0-20221213002717-88a4a2c6e77d
 	github.com/xanzy/go-gitlab v0.68.0

+ 2 - 2
go.sum

@@ -1466,8 +1466,8 @@ github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/polyfloyd/go-errorlint v0.0.0-20210722154253-910bb7978349/go.mod h1:wi9BfjxjF/bwiZ701TzmfKu6UKC357IOAtNr0Td0Lvw=
-github.com/porter-dev/api-contracts v0.0.39 h1:CWMIEXQUaRv8JncDT4l0sisRwXXEKRvfgQCQ6HtAOmE=
-github.com/porter-dev/api-contracts v0.0.39/go.mod h1:qr2L58mJLr5DUGV5OPw3REiSrQvJq6TgkKyEWP95dyU=
+github.com/porter-dev/api-contracts v0.0.41 h1:663IyYZyHroVD+2axN/4y/8cMTW81uEWNVpmXj7E/Do=
+github.com/porter-dev/api-contracts v0.0.41/go.mod h1:qr2L58mJLr5DUGV5OPw3REiSrQvJq6TgkKyEWP95dyU=
 github.com/porter-dev/switchboard v0.0.0-20221019155755-67ff2bf04935 h1:hfb3nt3AJXIBbevu6ARTg9SdOkMP6WLbKBiG5hT5rcc=
 github.com/porter-dev/switchboard v0.0.0-20221019155755-67ff2bf04935/go.mod h1:xSPzqSFMQ6OSbp42fhCi4AbGbQbsm6nRvOkrblFeXU4=
 github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=