Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions central/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,15 @@ func servicesToRegister(registry authproviders.Registry, authzTraceSink observe.
servicesToRegister = append(servicesToRegister, clusterCVEService.Singleton())
servicesToRegister = append(servicesToRegister, imageCVEService.Singleton())
servicesToRegister = append(servicesToRegister, nodeCVEService.Singleton())

if features.ObjectCollections.Enabled() {
servicesToRegister = append(servicesToRegister, collectionService.Singleton())
}

} else {
servicesToRegister = append(servicesToRegister, cveService.Singleton())
}

if features.ObjectCollections.Enabled() {
servicesToRegister = append(servicesToRegister, collectionService.Singleton())
}

if features.NewPolicyCategories.Enabled() {
servicesToRegister = append(servicesToRegister, policyCategoryService.Singleton())
}
Expand Down
10 changes: 6 additions & 4 deletions central/resourcecollection/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ type DataStore interface {
}

// New returns a new instance of a DataStore.
func New(storage store.Store, indexer index.Indexer, searcher search.Searcher) DataStore {
func New(storage store.Store, indexer index.Indexer, searcher search.Searcher) (DataStore, error) {
ds := &datastoreImpl{
storage: storage,
indexer: indexer,
searcher: searcher,
graph: nil,
}

return ds
if err := ds.initGraph(); err != nil {
return nil, err
}
return ds, nil
}

// GetTestPostgresDataStore provides a datastore connected to postgres for testing purposes.
Expand All @@ -57,5 +59,5 @@ func GetTestPostgresDataStore(t *testing.T, pool *pgxpool.Pool) (DataStore, erro
dbstore := postgres.New(pool)
indexer := postgres.NewIndexer(pool)
searcher := search.New(dbstore, indexer)
return New(dbstore, indexer, searcher), nil
return New(dbstore, indexer, searcher)
}
19 changes: 0 additions & 19 deletions central/resourcecollection/datastore/datastore_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
pkgSearch "github.com/stackrox/rox/pkg/search"
"github.com/stackrox/rox/pkg/set"
"github.com/stackrox/rox/pkg/sync"
"github.com/stackrox/rox/pkg/utils"
"github.com/stackrox/rox/pkg/uuid"
)

Expand Down Expand Up @@ -48,19 +47,7 @@ func (ge graphEntry) ID() string {
return ge.id
}

func (ds *datastoreImpl) initGraphOnce() {
once.Do(ds.initGraphCrashOnError)
}

func (ds *datastoreImpl) initGraphCrashOnError() {
err := ds.initGraph()
utils.CrashOnError(err)
}

func (ds *datastoreImpl) initGraph() error {
if ds.graph != nil {
return nil
}

// build graph object
graph := dag.NewDAG()
Expand Down Expand Up @@ -128,8 +115,6 @@ func (ds *datastoreImpl) initGraph() error {

// addCollectionToGraphNoLock creates a copy of the existing DAG and returns that copy with the collection added, or an appropriate error
func (ds *datastoreImpl) addCollectionToGraphNoLock(obj *storage.ResourceCollection) (*dag.DAG, error) {
ds.initGraphOnce()

var err error

// input validation
Expand Down Expand Up @@ -169,8 +154,6 @@ func (ds *datastoreImpl) addCollectionToGraphNoLock(obj *storage.ResourceCollect

// updateCollectionInGraphNoLock creates a copy of the existing DAG and returns that copy with the collection updated, or an appropriate error
func (ds *datastoreImpl) updateCollectionInGraphNoLock(obj *storage.ResourceCollection) (*dag.DAG, error) {
ds.initGraphOnce()

var err error

if err = verifyCollectionObjectNotEmpty(obj); err != nil {
Expand Down Expand Up @@ -229,8 +212,6 @@ func (ds *datastoreImpl) updateCollectionInGraphNoLock(obj *storage.ResourceColl

// deleteCollectionFromGraphNoLock removes the collection from the DAG, or returns an appropriate error
func (ds *datastoreImpl) deleteCollectionFromGraphNoLock(id string) error {
ds.initGraphOnce()

// this function covers removal of the vertex and any edges to or from other vertices, it is also threadsafe
return ds.graph.DeleteVertex(id)
}
Expand Down
3 changes: 2 additions & 1 deletion central/resourcecollection/datastore/datastore_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func (s *CollectionPostgresDataStoreTestSuite) SetupSuite() {
s.gormDB = pgtest.OpenGormDB(s.T(), source)
s.store = postgres.CreateTableAndNewStore(s.ctx, s.db, s.gormDB)
index := postgres.NewIndexer(s.db)
s.datastore = New(s.store, index, search.New(s.store, index))
s.datastore, err = New(s.store, index, search.New(s.store, index))
s.NoError(err)
}

// SetupTest removes the local graph before every test
Expand Down
8 changes: 6 additions & 2 deletions central/resourcecollection/datastore/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"github.com/stackrox/rox/central/globaldb"
"github.com/stackrox/rox/central/resourcecollection/datastore/search"
"github.com/stackrox/rox/central/resourcecollection/datastore/store/postgres"
"github.com/stackrox/rox/pkg/env"
"github.com/stackrox/rox/pkg/features"
"github.com/stackrox/rox/pkg/sync"
"github.com/stackrox/rox/pkg/utils"
)

var (
Expand All @@ -15,14 +17,16 @@ var (
)

func initialize() {
var err error
storage := postgres.New(globaldb.GetPostgres())
indexer := postgres.NewIndexer(globaldb.GetPostgres())
ds = New(storage, indexer, search.New(storage, indexer))
ds, err = New(storage, indexer, search.New(storage, indexer))
utils.CrashOnError(err)
}

// Singleton returns a singleton instance of cve datastore
func Singleton() DataStore {
if !features.ObjectCollections.Enabled() {
if !env.PostgresDatastoreEnabled.BooleanSetting() || !features.ObjectCollections.Enabled() {
return nil
}
once.Do(initialize)
Expand Down
3 changes: 2 additions & 1 deletion central/resourcecollection/service/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"github.com/stackrox/rox/central/resourcecollection/datastore"
"github.com/stackrox/rox/pkg/env"
"github.com/stackrox/rox/pkg/features"
"github.com/stackrox/rox/pkg/sync"
)
Expand All @@ -18,7 +19,7 @@ func initialize() {

// Singleton provides the instance of the Service interface to register.
func Singleton() Service {
if !features.ObjectCollections.Enabled() {
if !env.PostgresDatastoreEnabled.BooleanSetting() || !features.ObjectCollections.Enabled() {
return nil
}
once.Do(initialize)
Expand Down