Skip to content

Commit

Permalink
Merge pull request #1895 from weaveworks/no-k8s-needed-to-mount-endpo…
Browse files Browse the repository at this point in the history
…ints

Make namespace cache mockable when starting server
  • Loading branch information
Robin Sonefors authored Apr 12, 2022
2 parents c6226ee + 5aab594 commit 9e9d481
Show file tree
Hide file tree
Showing 8 changed files with 590 additions and 28 deletions.
8 changes: 7 additions & 1 deletion cmd/gitops-server/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/spf13/cobra"
"github.com/weaveworks/weave-gitops/api/v1alpha1"
"github.com/weaveworks/weave-gitops/cmd/gitops/cmderrors"
corecache "github.com/weaveworks/weave-gitops/core/cache"
"github.com/weaveworks/weave-gitops/core/logger"
core "github.com/weaveworks/weave-gitops/core/server"
"github.com/weaveworks/weave-gitops/pkg/helm/watcher"
Expand Down Expand Up @@ -203,7 +204,12 @@ func runCmd(cmd *cobra.Command, args []string) error {
authServer = srv
}

coreConfig := core.NewCoreConfig(log, rest, clusterName)
cache, err := corecache.NewContainer(context.Background(), rest, log)
if err != nil {
return fmt.Errorf("could not create cache container: %w", err)
}

coreConfig := core.NewCoreConfig(log, rest, cache, clusterName)

appConfig, err := server.DefaultApplicationsConfig(log)
if err != nil {
Expand Down
212 changes: 212 additions & 0 deletions core/cache/cachefakes/fake_container.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 19 additions & 9 deletions core/cache/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,30 @@ import (
"k8s.io/client-go/rest"
)

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate

const (
NamespaceStorage StorageType = "namespace"
)

type StorageType string

type Container struct {
//counterfeiter:generate . Container
type Container interface {
Start(ctx context.Context)
Stop()
ForceRefresh(name StorageType)
Namespaces() map[string][]v1.Namespace
}

type container struct {
namespace namespaceStore
logger logr.Logger
}

var globalCacheContainer *Container
var globalCacheContainer Container

func NewContainer(ctx context.Context, restCfg *rest.Config, logger logr.Logger) (*Container, error) {
func NewContainer(ctx context.Context, restCfg *rest.Config, logger logr.Logger) (Container, error) {
if globalCacheContainer != nil {
return globalCacheContainer, nil
}
Expand All @@ -33,34 +43,34 @@ func NewContainer(ctx context.Context, restCfg *rest.Config, logger logr.Logger)
return nil, err
}

globalCacheContainer = &Container{
globalCacheContainer = &container{
namespace: newNamespaceStore(clusterClient, logger),
logger: logger,
}

return globalCacheContainer, nil
}

func GlobalContainer() *Container {
func GlobalContainer() Container {
return globalCacheContainer
}

func (c *Container) Start(ctx context.Context) {
func (c *container) Start(ctx context.Context) {
c.namespace.Start(ctx)
}

func (c *Container) Stop() {
func (c *container) Stop() {
c.namespace.Stop()
}

func (c *Container) ForceRefresh(name StorageType) {
func (c *container) ForceRefresh(name StorageType) {
switch name {
case NamespaceStorage:
c.namespace.ForceRefresh()
}
}

func (c *Container) Namespaces() map[string][]v1.Namespace {
func (c *container) Namespaces() map[string][]v1.Namespace {
return c.namespace.Namespaces()
}

Expand Down
1 change: 1 addition & 0 deletions core/clustersmngr/clustersmngr.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type ClusterFetcher interface {
}

// ClientsPool stores all clients to the leaf clusters
//counterfeiter:generate . ClientsPool
type ClientsPool interface {
Add(cfg ClusterClientConfig, cluster Cluster) error
Clients() map[string]ClusterClient
Expand Down
Loading

0 comments on commit 9e9d481

Please sign in to comment.