Skip to content

Commit

Permalink
chore(GlobalInsight): allow using GlobalInsightService in other places (
Browse files Browse the repository at this point in the history
#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 <[email protected]>
  • Loading branch information
bartsmykla authored Sep 22, 2023
1 parent 3a1ad77 commit 25726ff
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 41 deletions.
3 changes: 2 additions & 1 deletion pkg/api-server/api_server_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/api-server/customization/customization_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
14 changes: 3 additions & 11 deletions pkg/api-server/global_insight_endpoint.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
}
Expand Down
18 changes: 4 additions & 14 deletions pkg/api-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions pkg/core/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions pkg/core/runtime/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/core/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package insights
package globalinsight

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package insights
package globalinsight

import (
"context"
Expand All @@ -9,28 +9,28 @@ 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 {
GetGlobalInsight(ctx context.Context) (*api_types.GlobalInsight, error)
}

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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package insights
package globalinsight_test

import (
"context"
Expand All @@ -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"
Expand All @@ -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())
Expand Down
11 changes: 11 additions & 0 deletions pkg/insights/globalinsight/globalinsight_suite_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 25726ff

Please sign in to comment.