kuma
190 строк · 6.1 Кб
1package envoyadmin_test2
3import (4"context"5
6. "github.com/onsi/ginkgo/v2"7. "github.com/onsi/gomega"8"google.golang.org/grpc"9
10mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"11system_proto "github.com/kumahq/kuma/api/system/v1alpha1"12"github.com/kumahq/kuma/pkg/core/resources/apis/system"13"github.com/kumahq/kuma/pkg/core/resources/manager"14"github.com/kumahq/kuma/pkg/core/resources/model"15core_store "github.com/kumahq/kuma/pkg/core/resources/store"16"github.com/kumahq/kuma/pkg/envoy/admin"17"github.com/kumahq/kuma/pkg/intercp/catalog"18"github.com/kumahq/kuma/pkg/intercp/envoyadmin"19"github.com/kumahq/kuma/pkg/plugins/resources/memory"20"github.com/kumahq/kuma/pkg/test/resources/samples"21"github.com/kumahq/kuma/pkg/test/runtime"22)
23
24var _ = Describe("Forwarding KDS Client", func() {25const thisInstanceID = "instance-1"26const otherInstanceID = "instance-2"27
28var forwardClient *countingForwardClient29var adminClient *runtime.DummyEnvoyAdminClient30var resManager manager.ResourceManager31
32var forwardingClient admin.EnvoyAdminClient33
34dp := samples.DataplaneBackendBuilder().35WithName("east.backend").36Build()37
38BeforeEach(func() {39forwardClient = &countingForwardClient{}40adminClient = &runtime.DummyEnvoyAdminClient{}41resManager = manager.NewResourceManager(memory.NewStore())42cat := catalog.NewConfigCatalog(resManager)43forwardingClient = envoyadmin.NewForwardingEnvoyAdminClient(44resManager,45cat,46thisInstanceID,47func(url string) (mesh_proto.InterCPEnvoyAdminForwardServiceClient, error) {48return forwardClient, nil49},50adminClient,51)52
53_, err := cat.Replace(context.Background(), []catalog.Instance{54{Id: thisInstanceID},55{Id: otherInstanceID},56})57Expect(err).ToNot(HaveOccurred())58
59Expect(samples.MeshDefaultBuilder().Create(resManager)).To(Succeed())60err = resManager.Create(context.Background(), dp, core_store.CreateByKey("east.backend", "default"))61Expect(err).ToNot(HaveOccurred())62})63
64createZoneInsightConnectedToGlobal := func(insight string, globalInstanceID string) {65zoneInsight := system.NewZoneInsightResource()66zoneInsight.Spec.EnvoyAdminStreams = &system_proto.EnvoyAdminStreams{67ConfigDumpGlobalInstanceId: globalInstanceID,68StatsGlobalInstanceId: globalInstanceID,69ClustersGlobalInstanceId: globalInstanceID,70}71err := resManager.Create(context.Background(), zoneInsight, core_store.CreateByKey(insight, model.NoMesh))72Expect(err).ToNot(HaveOccurred())73}74
75type testCase struct {76globalInstanceID string77forwardedRequests int78executedRequests int79}80
81DescribeTable("when request for config dump is executed",82func(given testCase) {83// given84createZoneInsightConnectedToGlobal("east", given.globalInstanceID)85
86// when87_, err := forwardingClient.ConfigDump(context.Background(), dp)88
89// then90Expect(err).ToNot(HaveOccurred())91Expect(forwardClient.xdsConfigCalled).To(Equal(given.forwardedRequests))92Expect(adminClient.ConfigDumpCalled).To(Equal(given.executedRequests))93},94Entry("should forward request when the zone of dp is connected to other instance of Global CP", testCase{95globalInstanceID: otherInstanceID,96forwardedRequests: 1,97executedRequests: 0,98}),99Entry("should execute request when the zone of dp is connected to this instance of Global CP", testCase{100globalInstanceID: thisInstanceID,101forwardedRequests: 0,102executedRequests: 1,103}),104)105
106DescribeTable("when request for stats is executed",107func(given testCase) {108// given109createZoneInsightConnectedToGlobal("east", given.globalInstanceID)110
111// when112_, err := forwardingClient.Stats(context.Background(), dp)113
114// then115Expect(err).ToNot(HaveOccurred())116Expect(forwardClient.statsCalled).To(Equal(given.forwardedRequests))117Expect(adminClient.StatsCalled).To(Equal(given.executedRequests))118},119Entry("should forward request when the zone of dp is connected to other instance of Global CP", testCase{120globalInstanceID: otherInstanceID,121forwardedRequests: 1,122executedRequests: 0,123}),124Entry("should execute request when the zone of dp is connected to this instance of Global CP", testCase{125globalInstanceID: thisInstanceID,126forwardedRequests: 0,127executedRequests: 1,128}),129)130
131DescribeTable("when request for clusters is executed",132func(given testCase) {133// given134createZoneInsightConnectedToGlobal("east", given.globalInstanceID)135
136// when137_, err := forwardingClient.Clusters(context.Background(), dp)138
139// then140Expect(err).ToNot(HaveOccurred())141Expect(forwardClient.clustersCalled).To(Equal(given.forwardedRequests))142Expect(adminClient.ClustersCalled).To(Equal(given.executedRequests))143},144Entry("should forward request when the zone of dp is connected to other instance of Global CP", testCase{145globalInstanceID: otherInstanceID,146forwardedRequests: 1,147executedRequests: 0,148}),149Entry("should execute request when the zone of dp is connected to this instance of Global CP", testCase{150globalInstanceID: thisInstanceID,151forwardedRequests: 0,152executedRequests: 1,153}),154)155})156
157type countingForwardClient struct {158xdsConfigCalled int159statsCalled int160clustersCalled int161}
162
163var _ mesh_proto.InterCPEnvoyAdminForwardServiceClient = &countingForwardClient{}164
165func (c *countingForwardClient) XDSConfig(ctx context.Context, in *mesh_proto.XDSConfigRequest, opts ...grpc.CallOption) (*mesh_proto.XDSConfigResponse, error) {166c.xdsConfigCalled++167return &mesh_proto.XDSConfigResponse{168Result: &mesh_proto.XDSConfigResponse_Config{169Config: []byte("forwarded"),170},171}, nil172}
173
174func (c *countingForwardClient) Stats(ctx context.Context, in *mesh_proto.StatsRequest, opts ...grpc.CallOption) (*mesh_proto.StatsResponse, error) {175c.statsCalled++176return &mesh_proto.StatsResponse{177Result: &mesh_proto.StatsResponse_Stats{178Stats: []byte("forwarded"),179},180}, nil181}
182
183func (c *countingForwardClient) Clusters(ctx context.Context, in *mesh_proto.ClustersRequest, opts ...grpc.CallOption) (*mesh_proto.ClustersResponse, error) {184c.clustersCalled++185return &mesh_proto.ClustersResponse{186Result: &mesh_proto.ClustersResponse_Clusters{187Clusters: []byte("forwarded"),188},189}, nil190}
191