diff --git a/go.mod b/go.mod index e8121a831..146d5fae8 100644 --- a/go.mod +++ b/go.mod @@ -73,7 +73,8 @@ require ( github.com/spf13/cobra v1.2.1 github.com/stretchr/testify v1.7.0 github.com/talos-systems/crypto v0.3.4 - github.com/talos-systems/discovery-service v0.1.1 + github.com/talos-systems/discovery-api v0.1.0 + github.com/talos-systems/discovery-client v0.1.0 github.com/talos-systems/go-blockdevice v0.2.4 github.com/talos-systems/go-cmd v0.1.0 github.com/talos-systems/go-debug v0.2.1 diff --git a/go.sum b/go.sum index bb8c951bf..b99c94756 100644 --- a/go.sum +++ b/go.sum @@ -1044,8 +1044,10 @@ github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/talos-systems/crypto v0.3.4 h1:bg4N27CH1MvUBasr70BlZObPXQYEhUTwOOm/jhCRFxg= github.com/talos-systems/crypto v0.3.4/go.mod h1:xaNCB2/Bxaj+qrkdeodhRv5eKQVvKOGBBMj58MrIPY8= -github.com/talos-systems/discovery-service v0.1.1 h1:GCCRnLT0GzJiU1l55WsiaNHDc88+wi7YZMsLaOT7uhY= -github.com/talos-systems/discovery-service v0.1.1/go.mod h1:7o4Fo240P3e2Bo7vZZ+4d/sykqPYI8LTtXCgjKgbqo4= +github.com/talos-systems/discovery-api v0.1.0 h1:aKod6uqakH6VfeQ6HaxPF7obqFAL1QTJe4HHTb2mVKk= +github.com/talos-systems/discovery-api v0.1.0/go.mod h1:ZsbzzOC5bzToaF3+YvUXDf9paeWV5bedpDu5RPXrglM= +github.com/talos-systems/discovery-client v0.1.0 h1:m+f96TKGFckMWrhDI+o9+QhcGn8f1A61Jp6YYVwiulI= +github.com/talos-systems/discovery-client v0.1.0/go.mod h1:LxqCv16VBB68MgaMnV8jXujYd3Q097DAn22U5gaHmkU= github.com/talos-systems/go-blockdevice v0.2.4 h1:/E5I95byCxfdmQIiBEyWgdUo+6vPBbbOJQIF9+yeysU= github.com/talos-systems/go-blockdevice v0.2.4/go.mod h1:qnn/zDc09I1DA2BUDDCOSA2D0P8pIDjN8pGiRoRaQig= github.com/talos-systems/go-cmd v0.0.0-20210216164758-68eb0067e0f0/go.mod h1:kf+rZzTEmlDiYQ6ulslvRONnKLQH8x83TowltGMhO+k= diff --git a/internal/app/machined/pkg/controllers/cluster/discovery_service.go b/internal/app/machined/pkg/controllers/cluster/discovery_service.go index 3da1f7c25..b0ecf60c5 100644 --- a/internal/app/machined/pkg/controllers/cluster/discovery_service.go +++ b/internal/app/machined/pkg/controllers/cluster/discovery_service.go @@ -16,8 +16,8 @@ import ( "github.com/cosi-project/runtime/pkg/controller" "github.com/cosi-project/runtime/pkg/resource" "github.com/cosi-project/runtime/pkg/state" - "github.com/talos-systems/discovery-service/api/v1alpha1/client/pb" - discoveryclient "github.com/talos-systems/discovery-service/pkg/client" + "github.com/talos-systems/discovery-api/api/v1alpha1/client/pb" + discoveryclient "github.com/talos-systems/discovery-client/pkg/client" "go.uber.org/zap" "inet.af/netaddr" diff --git a/internal/app/machined/pkg/controllers/cluster/discovery_service_test.go b/internal/app/machined/pkg/controllers/cluster/discovery_service_test.go index d54d97c1d..cb68be681 100644 --- a/internal/app/machined/pkg/controllers/cluster/discovery_service_test.go +++ b/internal/app/machined/pkg/controllers/cluster/discovery_service_test.go @@ -5,31 +5,27 @@ package cluster_test import ( - "bytes" "context" "crypto/aes" + "crypto/rand" + "encoding/base64" + "io" "log" - "net" + "net/url" "testing" "time" "github.com/cosi-project/runtime/pkg/resource" - grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" - grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap" - grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" - "github.com/talos-systems/discovery-service/api/v1alpha1/client/pb" - serverpb "github.com/talos-systems/discovery-service/api/v1alpha1/server/pb" - "github.com/talos-systems/discovery-service/pkg/client" - "github.com/talos-systems/discovery-service/pkg/server" + "github.com/talos-systems/discovery-api/api/v1alpha1/client/pb" + "github.com/talos-systems/discovery-client/pkg/client" "github.com/talos-systems/go-retry/retry" - "google.golang.org/grpc" "inet.af/netaddr" clusterctrl "github.com/talos-systems/talos/internal/app/machined/pkg/controllers/cluster" "github.com/talos-systems/talos/pkg/logging" "github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine" + "github.com/talos-systems/talos/pkg/machinery/constants" "github.com/talos-systems/talos/pkg/machinery/proto" "github.com/talos-systems/talos/pkg/resources/cluster" "github.com/talos-systems/talos/pkg/resources/config" @@ -40,52 +36,35 @@ type DiscoveryServiceSuite struct { ClusterSuite } -func setupServer(t *testing.T) (address string) { - t.Helper() - - lis, err := net.Listen("tcp", "localhost:0") - require.NoError(t, err) - - logger := logging.Wrap(log.Writer()) - - serverOptions := []grpc.ServerOption{ - grpc_middleware.WithUnaryServerChain( - grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(server.FieldExtractor)), - grpc_zap.UnaryServerInterceptor(logger), - ), - grpc_middleware.WithStreamServerChain( - grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(server.FieldExtractor)), - grpc_zap.StreamServerInterceptor(logger), - ), - } - - s := grpc.NewServer(serverOptions...) - serverpb.RegisterClusterServer(s, server.NewTestClusterServer(logger)) - - go func() { - require.NoError(t, s.Serve(lis)) - }() - - t.Cleanup(s.Stop) - - return lis.Addr().String() -} - func (suite *DiscoveryServiceSuite) TestReconcile() { suite.startRuntime() suite.Require().NoError(suite.runtime.RegisterController(&clusterctrl.DiscoveryServiceController{})) - address := setupServer(suite.T()) + serviceEndpoint, err := url.Parse(constants.DefaultDiscoveryServiceEndpoint) + suite.Require().NoError(err) + + if serviceEndpoint.Port() == "" { + serviceEndpoint.Host += ":443" + } + + clusterIDRaw := make([]byte, constants.DefaultClusterIDSize) + _, err = io.ReadFull(rand.Reader, clusterIDRaw) + suite.Require().NoError(err) + + clusterID := base64.StdEncoding.EncodeToString(clusterIDRaw) + + encryptionKey := make([]byte, constants.DefaultClusterSecretSize) + _, err = io.ReadFull(rand.Reader, encryptionKey) + suite.Require().NoError(err) // regular discovery affiliate discoveryConfig := cluster.NewConfig(config.NamespaceName, cluster.ConfigID) discoveryConfig.TypedSpec().DiscoveryEnabled = true discoveryConfig.TypedSpec().RegistryServiceEnabled = true - discoveryConfig.TypedSpec().ServiceEndpoint = address - discoveryConfig.TypedSpec().ServiceEndpointInsecure = true - discoveryConfig.TypedSpec().ServiceClusterID = "fake" - discoveryConfig.TypedSpec().ServiceEncryptionKey = bytes.Repeat([]byte{1}, 32) + discoveryConfig.TypedSpec().ServiceEndpoint = serviceEndpoint.Host + discoveryConfig.TypedSpec().ServiceClusterID = clusterID + discoveryConfig.TypedSpec().ServiceEncryptionKey = encryptionKey suite.Require().NoError(suite.state.Create(suite.ctx, discoveryConfig)) nodeIdentity := cluster.NewIdentity(cluster.NamespaceName, cluster.LocalIdentity) @@ -114,11 +93,10 @@ func (suite *DiscoveryServiceSuite) TestReconcile() { cli, err := client.NewClient(client.Options{ Cipher: cipher, - Endpoint: address, + Endpoint: serviceEndpoint.Host, ClusterID: discoveryConfig.TypedSpec().ServiceClusterID, AffiliateID: "7x1SuC8Ege5BGXdAfTEff5iQnlWZLfv9h1LGMxA2pYkC", TTL: 5 * time.Minute, - Insecure: true, }) suite.Require().NoError(err)