From 25726ff41f539d31c33fe44e493f57b0187ba76c Mon Sep 17 00:00:00 2001 From: Bart Smykla Date: Fri, 22 Sep 2023 10:24:49 +0200 Subject: [PATCH] chore(GlobalInsight): allow using GlobalInsightService in other places (#7813) Allow using GlobalInsightService in other than API endpoint places - Modified GlobalInsightService to use ResourceStore instead of ResourceManager, as it didn't use any specific ResourceManager functionality. - Exposed GlobalInsightService in the Runtime and RuntimeBuilder interfaces. - Fixed the returned type of GlobalInsight endpoint to be JSON instead of text. Explanation: The GlobalInsightService was previously only accessible from API endpoint places. To allow it to be used in other places, such as Bootstrap plugins, it needed to be modified to use ResourceStore instead of ResourceManager. In addition, the GlobalInsightService needed to be exposed in the Runtime and RuntimeBuilder interfaces. This makes it available to a wider range of code, including Bootstrap plugins. Finally, the commit fixes the returned type of the GlobalInsight endpoint to be JSON instead of text. This is because JSON is the standard format for data exchange between web applications. Overall, this commit makes the GlobalInsightService more accessible and easier to use. Signed-off-by: Bart Smykla --- pkg/api-server/api_server_suite_test.go | 3 ++- .../customization/customization_suite_test.go | 3 ++- pkg/api-server/global_insight_endpoint.go | 14 +++---------- pkg/api-server/server.go | 18 ++++------------- pkg/core/bootstrap/bootstrap.go | 17 ++++++++++++++++ pkg/core/runtime/builder.go | 12 +++++++++++ pkg/core/runtime/runtime.go | 9 ++++++++- .../cached_global_insight_service.go | 2 +- .../global_insight_service.go | 20 +++++++++---------- .../global_insight_service_test.go | 5 +++-- .../globalinsight/globalinsight_suite_test.go | 11 ++++++++++ .../testdata/full_global_insight.golden.json | 0 12 files changed, 73 insertions(+), 41 deletions(-) rename pkg/insights/{ => globalinsight}/cached_global_insight_service.go (98%) rename pkg/insights/{ => globalinsight}/global_insight_service.go (91%) rename pkg/insights/{ => globalinsight}/global_insight_service_test.go (97%) create mode 100644 pkg/insights/globalinsight/globalinsight_suite_test.go rename pkg/insights/{ => globalinsight}/testdata/full_global_insight.golden.json (100%) diff --git a/pkg/api-server/api_server_suite_test.go b/pkg/api-server/api_server_suite_test.go index efad95bddfe1..cf24530809e3 100644 --- a/pkg/api-server/api_server_suite_test.go +++ b/pkg/api-server/api_server_suite_test.go @@ -31,6 +31,7 @@ import ( "github.com/kumahq/kuma/pkg/core/runtime" "github.com/kumahq/kuma/pkg/dns/vips" "github.com/kumahq/kuma/pkg/envoy/admin/access" + "github.com/kumahq/kuma/pkg/insights/globalinsight" core_metrics "github.com/kumahq/kuma/pkg/metrics" "github.com/kumahq/kuma/pkg/plugins/authn/api-server/certs" "github.com/kumahq/kuma/pkg/plugins/resources/memory" @@ -249,7 +250,7 @@ func tryStartApiServer(t *testApiServerConfigurer) (*api_server.ApiServer, kuma_ ZoneToken: builtin.NewZoneTokenIssuer(resManager), }, func(*restful.WebService) error { return nil }, - nil, + globalinsight.NewDefaultGlobalInsightService(t.store), ) if err != nil { return nil, cfg, stop, err diff --git a/pkg/api-server/customization/customization_suite_test.go b/pkg/api-server/customization/customization_suite_test.go index 39c0a652cad7..19afbc079a27 100644 --- a/pkg/api-server/customization/customization_suite_test.go +++ b/pkg/api-server/customization/customization_suite_test.go @@ -21,6 +21,7 @@ import ( "github.com/kumahq/kuma/pkg/core/runtime" "github.com/kumahq/kuma/pkg/dns/vips" "github.com/kumahq/kuma/pkg/envoy/admin/access" + "github.com/kumahq/kuma/pkg/insights/globalinsight" core_metrics "github.com/kumahq/kuma/pkg/metrics" "github.com/kumahq/kuma/pkg/plugins/authn/api-server/certs" "github.com/kumahq/kuma/pkg/test" @@ -86,7 +87,7 @@ func createTestApiServer(store store.ResourceStore, config *config_api_server.Ap ZoneToken: builtin.NewZoneTokenIssuer(resManager), }, func(*restful.WebService) error { return nil }, - nil, + globalinsight.NewDefaultGlobalInsightService(store), ) Expect(err).ToNot(HaveOccurred()) return apiServer diff --git a/pkg/api-server/global_insight_endpoint.go b/pkg/api-server/global_insight_endpoint.go index ae5d1c9e60b3..7192771f6262 100644 --- a/pkg/api-server/global_insight_endpoint.go +++ b/pkg/api-server/global_insight_endpoint.go @@ -1,18 +1,16 @@ package api_server import ( - "encoding/json" - "github.com/emicklei/go-restful/v3" "github.com/kumahq/kuma/pkg/core/rest/errors" - "github.com/kumahq/kuma/pkg/insights" + "github.com/kumahq/kuma/pkg/insights/globalinsight" ) const globalInsightPath = "/global-insight" type globalInsightEndpoint struct { - globalInsightService insights.GlobalInsightService + globalInsightService globalinsight.GlobalInsightService } func (ge *globalInsightEndpoint) addEndpoint(ws *restful.WebService) { @@ -31,13 +29,7 @@ func (ge *globalInsightEndpoint) getGlobalInsight(request *restful.Request, resp return } - marshal, err := json.Marshal(globalInsight) - if err != nil { - errors.HandleError(ctx, response, err, "Error serializing GlobalInsight") - return - } - _, err = response.ResponseWriter.Write(marshal) - if err != nil { + if err = response.WriteAsJson(globalInsight); err != nil { errors.HandleError(ctx, response, err, "Could not write response") return } diff --git a/pkg/api-server/server.go b/pkg/api-server/server.go index 9f1ccde4f0e8..11dc53cad88d 100644 --- a/pkg/api-server/server.go +++ b/pkg/api-server/server.go @@ -39,9 +39,8 @@ import ( "github.com/kumahq/kuma/pkg/core/runtime" "github.com/kumahq/kuma/pkg/dns/vips" "github.com/kumahq/kuma/pkg/envoy/admin" - "github.com/kumahq/kuma/pkg/insights" + "github.com/kumahq/kuma/pkg/insights/globalinsight" "github.com/kumahq/kuma/pkg/metrics" - "github.com/kumahq/kuma/pkg/multitenant" "github.com/kumahq/kuma/pkg/plugins/authn/api-server/certs" "github.com/kumahq/kuma/pkg/plugins/resources/k8s" "github.com/kumahq/kuma/pkg/tokens/builtin" @@ -103,7 +102,7 @@ func NewApiServer( envoyAdminClient admin.EnvoyAdminClient, tokenIssuers builtin.TokenIssuers, wsCustomize func(*restful.WebService) error, - tenants multitenant.Tenants, + globalInsightService globalinsight.GlobalInsightService, ) (*ApiServer, error) { serverConfig := cfg.ApiServer container := restful.NewContainer() @@ -140,15 +139,6 @@ func NewApiServer( Consumes(restful.MIME_JSON). Produces(restful.MIME_JSON) - globalInsightService := insights.NewDefaultGlobalInsightService(resManager) - if cfg.Store.Cache.Enabled { - globalInsightService = insights.NewCachedGlobalInsightService( - globalInsightService, - tenants, - cfg.Store.Cache.ExpirationTime.Duration, - ) - } - addResourcesEndpoints(ws, defs, resManager, cfg, access.ResourceAccess, globalInsightService) addPoliciesWsEndpoints(ws, cfg.Mode, cfg.ApiServer.ReadOnly, defs) addInspectEndpoints(ws, cfg, meshContextBuilder, resManager) @@ -236,7 +226,7 @@ func addResourcesEndpoints( resManager manager.ResourceManager, cfg *kuma_cp.Config, resourceAccess resources_access.ResourceAccess, - globalInsightService insights.GlobalInsightService, + globalInsightService globalinsight.GlobalInsightService, ) { dpOverviewEndpoints := dataplaneOverviewEndpoints{ resManager: resManager, @@ -512,7 +502,7 @@ func SetupServer(rt runtime.Runtime) error { rt.EnvoyAdminClient(), rt.TokenIssuers(), rt.APIWebServiceCustomize(), - rt.Tenants(), + rt.GlobalInsightService(), ) if err != nil { return err diff --git a/pkg/core/bootstrap/bootstrap.go b/pkg/core/bootstrap/bootstrap.go index 9bddbea08b94..f21120f99f64 100644 --- a/pkg/core/bootstrap/bootstrap.go +++ b/pkg/core/bootstrap/bootstrap.go @@ -41,6 +41,7 @@ import ( "github.com/kumahq/kuma/pkg/envoy/admin" "github.com/kumahq/kuma/pkg/envoy/admin/access" "github.com/kumahq/kuma/pkg/events" + "github.com/kumahq/kuma/pkg/insights/globalinsight" "github.com/kumahq/kuma/pkg/intercp" "github.com/kumahq/kuma/pkg/intercp/catalog" "github.com/kumahq/kuma/pkg/intercp/envoyadmin" @@ -90,6 +91,9 @@ func buildRuntime(appCtx context.Context, cfg kuma_cp.Config) (core_runtime.Runt if err := initializeConfigStore(cfg, builder); err != nil { return nil, err } + + initializeGlobalInsightService(cfg, builder) + // we add Secret store to unified ResourceStore so global<->zone synchronizer can use unified interface builder.ResourceStore().Customize(system.SecretType, builder.SecretStore()) builder.ResourceStore().Customize(system.GlobalSecretType, builder.SecretStore()) @@ -343,6 +347,19 @@ func initializeConfigStore(cfg kuma_cp.Config, builder *core_runtime.Builder) er } } +func initializeGlobalInsightService(cfg kuma_cp.Config, builder *core_runtime.Builder) { + globalInsightService := globalinsight.NewDefaultGlobalInsightService(builder.ResourceStore()) + if cfg.Store.Cache.Enabled { + globalInsightService = globalinsight.NewCachedGlobalInsightService( + globalInsightService, + builder.Tenants(), + cfg.Store.Cache.ExpirationTime.Duration, + ) + } + + builder.WithGlobalInsightService(globalInsightService) +} + func initializeCaManagers(builder *core_runtime.Builder) error { for pluginName, caPlugin := range core_plugins.Plugins().CaPlugins() { caManager, err := caPlugin.NewCaManager(builder, nil) diff --git a/pkg/core/runtime/builder.go b/pkg/core/runtime/builder.go index 8c3ab0cbf911..091560f87659 100644 --- a/pkg/core/runtime/builder.go +++ b/pkg/core/runtime/builder.go @@ -24,6 +24,7 @@ import ( dp_server "github.com/kumahq/kuma/pkg/dp-server/server" "github.com/kumahq/kuma/pkg/envoy/admin" "github.com/kumahq/kuma/pkg/events" + "github.com/kumahq/kuma/pkg/insights/globalinsight" "github.com/kumahq/kuma/pkg/intercp/client" kds_context "github.com/kumahq/kuma/pkg/kds/context" "github.com/kumahq/kuma/pkg/metrics" @@ -74,6 +75,7 @@ type Builder struct { cs core_store.ResourceStore rm core_manager.CustomizableResourceManager rom core_manager.ReadOnlyResourceManager + gis globalinsight.GlobalInsightService cam core_ca.Managers dsl datasource.Loader ext context.Context @@ -140,6 +142,11 @@ func (b *Builder) WithConfigStore(cs core_store.ResourceStore) *Builder { return b } +func (b *Builder) WithGlobalInsightService(gis globalinsight.GlobalInsightService) *Builder { + b.gis = gis + return b +} + func (b *Builder) WithResourceManager(rm core_manager.CustomizableResourceManager) *Builder { b.rm = rm return b @@ -362,6 +369,7 @@ func (b *Builder) Build() (Runtime, error) { rs: b.rs, ss: b.ss, cam: b.cam, + gis: b.gis, dsl: b.dsl, ext: b.ext, configm: b.configm, @@ -407,6 +415,10 @@ func (b *Builder) ConfigStore() core_store.ResourceStore { return b.cs } +func (b *Builder) GlobalInsightService() globalinsight.GlobalInsightService { + return b.gis +} + func (b *Builder) ResourceManager() core_manager.CustomizableResourceManager { return b.rm } diff --git a/pkg/core/runtime/runtime.go b/pkg/core/runtime/runtime.go index 31f2d1306b81..051a84790f90 100644 --- a/pkg/core/runtime/runtime.go +++ b/pkg/core/runtime/runtime.go @@ -25,6 +25,7 @@ import ( "github.com/kumahq/kuma/pkg/envoy/admin" "github.com/kumahq/kuma/pkg/envoy/admin/access" "github.com/kumahq/kuma/pkg/events" + "github.com/kumahq/kuma/pkg/insights/globalinsight" "github.com/kumahq/kuma/pkg/intercp/client" kds_context "github.com/kumahq/kuma/pkg/kds/context" "github.com/kumahq/kuma/pkg/metrics" @@ -33,7 +34,7 @@ import ( "github.com/kumahq/kuma/pkg/tokens/builtin" tokens_access "github.com/kumahq/kuma/pkg/tokens/builtin/access" zone_access "github.com/kumahq/kuma/pkg/tokens/builtin/zone/access" - mesh "github.com/kumahq/kuma/pkg/xds/cache/mesh" + "github.com/kumahq/kuma/pkg/xds/cache/mesh" xds_runtime "github.com/kumahq/kuma/pkg/xds/runtime" "github.com/kumahq/kuma/pkg/xds/secrets" ) @@ -60,6 +61,7 @@ type RuntimeContext interface { ReadOnlyResourceManager() core_manager.ReadOnlyResourceManager SecretStore() store.SecretStore ConfigStore() core_store.ResourceStore + GlobalInsightService() globalinsight.GlobalInsightService CaManagers() ca.Managers Extensions() context.Context ConfigManager() config_manager.ConfigManager @@ -147,6 +149,7 @@ type runtimeContext struct { rs core_store.ResourceStore ss store.SecretStore cs core_store.ResourceStore + gis globalinsight.GlobalInsightService rom core_manager.ReadOnlyResourceManager cam ca.Managers dsl datasource.Loader @@ -211,6 +214,10 @@ func (rc *runtimeContext) ConfigStore() core_store.ResourceStore { return rc.cs } +func (rc *runtimeContext) GlobalInsightService() globalinsight.GlobalInsightService { + return rc.gis +} + func (rc *runtimeContext) ReadOnlyResourceManager() core_manager.ReadOnlyResourceManager { return rc.rom } diff --git a/pkg/insights/cached_global_insight_service.go b/pkg/insights/globalinsight/cached_global_insight_service.go similarity index 98% rename from pkg/insights/cached_global_insight_service.go rename to pkg/insights/globalinsight/cached_global_insight_service.go index ac9c00481eb2..c9db9f7f96e9 100644 --- a/pkg/insights/cached_global_insight_service.go +++ b/pkg/insights/globalinsight/cached_global_insight_service.go @@ -1,4 +1,4 @@ -package insights +package globalinsight import ( "context" diff --git a/pkg/insights/global_insight_service.go b/pkg/insights/globalinsight/global_insight_service.go similarity index 91% rename from pkg/insights/global_insight_service.go rename to pkg/insights/globalinsight/global_insight_service.go index 360d83f6af4b..c31619204ef4 100644 --- a/pkg/insights/global_insight_service.go +++ b/pkg/insights/globalinsight/global_insight_service.go @@ -1,4 +1,4 @@ -package insights +package globalinsight import ( "context" @@ -9,7 +9,7 @@ import ( "github.com/kumahq/kuma/pkg/core" "github.com/kumahq/kuma/pkg/core/resources/apis/mesh" "github.com/kumahq/kuma/pkg/core/resources/apis/system" - resources_manager "github.com/kumahq/kuma/pkg/core/resources/manager" + core_store "github.com/kumahq/kuma/pkg/core/resources/store" ) type GlobalInsightService interface { @@ -17,20 +17,20 @@ type GlobalInsightService interface { } type defaultGlobalInsightService struct { - resManager resources_manager.ResourceManager + resourceStore core_store.ResourceStore } var _ GlobalInsightService = &defaultGlobalInsightService{} -func NewDefaultGlobalInsightService(resManager resources_manager.ResourceManager) GlobalInsightService { - return &defaultGlobalInsightService{resManager: resManager} +func NewDefaultGlobalInsightService(resourceStore core_store.ResourceStore) GlobalInsightService { + return &defaultGlobalInsightService{resourceStore: resourceStore} } func (gis *defaultGlobalInsightService) GetGlobalInsight(ctx context.Context) (*api_types.GlobalInsight, error) { globalInsights := &api_types.GlobalInsight{CreatedAt: core.Now()} meshInsights := &mesh.MeshInsightResourceList{} - if err := gis.resManager.List(ctx, meshInsights); err != nil { + if err := gis.resourceStore.List(ctx, meshInsights); err != nil { return nil, err } @@ -103,7 +103,7 @@ func (gis *defaultGlobalInsightService) aggregateServices( globalInsight *api_types.GlobalInsight, ) error { serviceInsights := &mesh.ServiceInsightResourceList{} - if err := gis.resManager.List(ctx, serviceInsights); err != nil { + if err := gis.resourceStore.List(ctx, serviceInsights); err != nil { return err } @@ -146,7 +146,7 @@ func (gis *defaultGlobalInsightService) aggregateZoneControlPlanes( globalInsight *api_types.GlobalInsight, ) error { zoneInsights := &system.ZoneInsightResourceList{} - if err := gis.resManager.List(ctx, zoneInsights); err != nil { + if err := gis.resourceStore.List(ctx, zoneInsights); err != nil { return err } @@ -173,7 +173,7 @@ func (gis *defaultGlobalInsightService) aggregateZoneIngresses( globalInsight *api_types.GlobalInsight, ) error { zoneIngressInsights := &mesh.ZoneIngressInsightResourceList{} - if err := gis.resManager.List(ctx, zoneIngressInsights); err != nil { + if err := gis.resourceStore.List(ctx, zoneIngressInsights); err != nil { return err } @@ -197,7 +197,7 @@ func (gis *defaultGlobalInsightService) aggregateZoneEgresses( globalInsight *api_types.GlobalInsight, ) error { zoneEgressInsights := &mesh.ZoneEgressInsightResourceList{} - if err := gis.resManager.List(ctx, zoneEgressInsights); err != nil { + if err := gis.resourceStore.List(ctx, zoneEgressInsights); err != nil { return err } diff --git a/pkg/insights/global_insight_service_test.go b/pkg/insights/globalinsight/global_insight_service_test.go similarity index 97% rename from pkg/insights/global_insight_service_test.go rename to pkg/insights/globalinsight/global_insight_service_test.go index 296fb555e2cb..6ed2b29a67b2 100644 --- a/pkg/insights/global_insight_service_test.go +++ b/pkg/insights/globalinsight/global_insight_service_test.go @@ -1,4 +1,4 @@ -package insights +package globalinsight_test import ( "context" @@ -15,6 +15,7 @@ import ( "github.com/kumahq/kuma/pkg/core/resources/manager" core_model "github.com/kumahq/kuma/pkg/core/resources/model" "github.com/kumahq/kuma/pkg/core/resources/store" + "github.com/kumahq/kuma/pkg/insights/globalinsight" "github.com/kumahq/kuma/pkg/plugins/resources/memory" "github.com/kumahq/kuma/pkg/test/matchers" "github.com/kumahq/kuma/pkg/test/resources/builders" @@ -34,7 +35,7 @@ var _ = Describe("Global Insight", func() { It("should compute global insight", func() { // given - globalInsightService := NewDefaultGlobalInsightService(rm) + globalInsightService := globalinsight.NewDefaultGlobalInsightService(rm) err := createMeshInsight("default", rs) Expect(err).ToNot(HaveOccurred()) diff --git a/pkg/insights/globalinsight/globalinsight_suite_test.go b/pkg/insights/globalinsight/globalinsight_suite_test.go new file mode 100644 index 000000000000..f77dc7c624f6 --- /dev/null +++ b/pkg/insights/globalinsight/globalinsight_suite_test.go @@ -0,0 +1,11 @@ +package globalinsight_test + +import ( + "testing" + + "github.com/kumahq/kuma/pkg/test" +) + +func TestGlobalInsights(t *testing.T) { + test.RunSpecs(t, "GlobalInsight Suite") +} diff --git a/pkg/insights/testdata/full_global_insight.golden.json b/pkg/insights/globalinsight/testdata/full_global_insight.golden.json similarity index 100% rename from pkg/insights/testdata/full_global_insight.golden.json rename to pkg/insights/globalinsight/testdata/full_global_insight.golden.json