-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ReferrerStoreManager interface to wrap operations on namesp…
…aced stores [multi-tenancy PR 4] (#1380)
- Loading branch information
Showing
7 changed files
with
258 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
Copyright The Ratify Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package referrerstores | ||
|
||
import ( | ||
"github.com/deislabs/ratify/pkg/referrerstore" | ||
) | ||
|
||
// ReferrerStoreManager is an interface that defines the methods for managing referrer stores across different scopes. | ||
type ReferrerStoreManager interface { | ||
// Stores returns the list of referrer stores for the given scope. | ||
GetStores(scope string) []referrerstore.ReferrerStore | ||
|
||
// AddStore adds the given store under the given scope. | ||
AddStore(scope, storeName string, store referrerstore.ReferrerStore) | ||
|
||
// DeleteStore deletes the policy from the given scope. | ||
DeleteStore(scope, storeName string) | ||
|
||
// IsEmpty returns true if there are no stores. | ||
IsEmpty() bool | ||
|
||
// GetStoreCount returns the number of stores in all scopes. | ||
GetStoreCount() int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright The Ratify Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package referrerstores | ||
|
||
import ( | ||
"github.com/deislabs/ratify/pkg/referrerstore" | ||
) | ||
|
||
// ActiveStores implements the ReferrerStoreManager interface. | ||
type ActiveStores struct { | ||
// TODO: Implement concurrent safety using sync.Map | ||
// The structure of the map is as follows: | ||
// The first level maps from scope to stores | ||
// The second level maps from store name to store | ||
// Example: | ||
// { | ||
// "namespace1": { | ||
// "store1": store1, | ||
// "store2": store2 | ||
// } | ||
// } | ||
// Note: Scope is utilized for organizing and isolating stores. In a Kubernetes (K8s) environment, the scope can be either a namespace or an empty string ("") for cluster-wide stores. | ||
ScopedStores map[string]map[string]referrerstore.ReferrerStore | ||
} | ||
|
||
func NewActiveStores() ReferrerStoreManager { | ||
return &ActiveStores{ | ||
ScopedStores: make(map[string]map[string]referrerstore.ReferrerStore), | ||
} | ||
} | ||
|
||
// GetStores fulfills the ReferrerStoreManager interface. | ||
// It returns all the stores in the ActiveStores for the given scope. If no stores are found for the given scope, it returns cluster-wide stores. | ||
// TODO: Current implementation fetches stores for all namespaces including cluster-wide ones. Will support actual namespace based stores in future. | ||
func (s *ActiveStores) GetStores(_ string) []referrerstore.ReferrerStore { | ||
stores := []referrerstore.ReferrerStore{} | ||
for _, scopedStores := range s.ScopedStores { | ||
for _, store := range scopedStores { | ||
stores = append(stores, store) | ||
} | ||
} | ||
return stores | ||
} | ||
|
||
// AddStore fulfills the ReferrerStoreManager interface. | ||
// It adds the given store under the given scope. | ||
func (s *ActiveStores) AddStore(scope, storeName string, store referrerstore.ReferrerStore) { | ||
if _, ok := s.ScopedStores[scope]; !ok { | ||
s.ScopedStores[scope] = make(map[string]referrerstore.ReferrerStore) | ||
} | ||
s.ScopedStores[scope][storeName] = store | ||
} | ||
|
||
// DeleteStore fulfills the ReferrerStoreManager interface. | ||
// It deletes the store with the given name under the given scope. | ||
func (s *ActiveStores) DeleteStore(scope, storeName string) { | ||
if stores, ok := s.ScopedStores[scope]; ok { | ||
delete(stores, storeName) | ||
} | ||
} | ||
|
||
// IsEmpty fulfills the ReferrerStoreManager interface. | ||
// It returns true if there are no stores in the ActiveStores. | ||
func (s *ActiveStores) IsEmpty() bool { | ||
return s.GetStoreCount() == 0 | ||
} | ||
|
||
// GetStore fulfills the ReferrerStoreManager interface. | ||
// GetStoreCount returns the total number of stores in the ActiveStores. | ||
func (s *ActiveStores) GetStoreCount() int { | ||
count := 0 | ||
for _, stores := range s.ScopedStores { | ||
count += len(stores) | ||
} | ||
return count | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
Copyright The Ratify Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package referrerstores | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/deislabs/ratify/internal/constants" | ||
"github.com/deislabs/ratify/pkg/common" | ||
"github.com/deislabs/ratify/pkg/ocispecs" | ||
rs "github.com/deislabs/ratify/pkg/referrerstore" | ||
"github.com/deislabs/ratify/pkg/referrerstore/config" | ||
"github.com/opencontainers/go-digest" | ||
) | ||
|
||
type mockStore struct { | ||
name string | ||
} | ||
|
||
func (s mockStore) Name() string { | ||
return s.name | ||
} | ||
|
||
func (s mockStore) ListReferrers(_ context.Context, _ common.Reference, _ []string, _ string, _ *ocispecs.SubjectDescriptor) (rs.ListReferrersResult, error) { | ||
return rs.ListReferrersResult{}, nil | ||
} | ||
|
||
func (s mockStore) GetBlobContent(_ context.Context, _ common.Reference, _ digest.Digest) ([]byte, error) { | ||
return nil, nil | ||
} | ||
|
||
func (s mockStore) GetReferenceManifest(_ context.Context, _ common.Reference, _ ocispecs.ReferenceDescriptor) (ocispecs.ReferenceManifest, error) { | ||
return ocispecs.ReferenceManifest{}, nil | ||
} | ||
|
||
func (s mockStore) GetConfig() *config.StoreConfig { | ||
return nil | ||
} | ||
|
||
func (s mockStore) GetSubjectDescriptor(_ context.Context, _ common.Reference) (*ocispecs.SubjectDescriptor, error) { | ||
return nil, nil | ||
} | ||
|
||
const ( | ||
namespace1 = constants.EmptyNamespace | ||
namespace2 = "namespace2" | ||
name1 = "name1" | ||
name2 = "name2" | ||
) | ||
|
||
var ( | ||
store1 = mockStore{name: name1} | ||
store2 = mockStore{name: name2} | ||
) | ||
|
||
func TestStoresOperations(t *testing.T) { | ||
stores := NewActiveStores() | ||
stores.AddStore(namespace1, store1.Name(), store1) | ||
stores.AddStore(namespace1, store2.Name(), store2) | ||
stores.AddStore(namespace2, store1.Name(), store1) | ||
stores.AddStore(namespace2, store2.Name(), store2) | ||
|
||
if stores.GetStoreCount() != 4 { | ||
t.Fatalf("Expected 4 namespaces, got %d", stores.GetStoreCount()) | ||
} | ||
|
||
stores.DeleteStore(namespace2, store1.Name()) | ||
if len(stores.GetStores(namespace2)) != 3 { | ||
t.Fatalf("Expected 3 store in namespace %s, got %d", namespace2, len(stores.GetStores(namespace2))) | ||
} | ||
|
||
stores.DeleteStore(namespace2, store2.Name()) | ||
if len(stores.GetStores(namespace2)) != 2 { | ||
t.Fatalf("Expected 2 stores in namespace %s, got %d", namespace2, len(stores.GetStores(namespace2))) | ||
} | ||
|
||
stores.DeleteStore(namespace1, store1.Name()) | ||
if len(stores.GetStores(namespace1)) != 1 { | ||
t.Fatalf("Expected 1 store in namespace %s, got %d", namespace1, len(stores.GetStores(namespace1))) | ||
} | ||
|
||
stores.DeleteStore(namespace1, store2.Name()) | ||
if len(stores.GetStores(namespace1)) != 0 { | ||
t.Fatalf("Expected 0 stores in namespace %s, got %d", namespace1, len(stores.GetStores(namespace1))) | ||
} | ||
|
||
if !stores.IsEmpty() { | ||
t.Fatalf("Expected stores to be empty") | ||
} | ||
} |
Oops, something went wrong.