-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
:Merge branch 'master' into no-backend
Signed-off-by: Will Beason <willbeason@google.com>
- Loading branch information
Showing
7 changed files
with
362 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package handler | ||
|
||
// Cacher is a type - usually a Handler - which needs to cache state. | ||
// Handlers only need implement this interface if they have need of a cache. | ||
// Handlers which do not implement Cacher are assumed to be stateless from | ||
// Client's perspective. | ||
type Cacher interface { | ||
// GetCache returns the Cache. If nil, the Cacher is treated as having no | ||
// cache. | ||
GetCache() Cache | ||
} | ||
|
||
// Cache is an interface for Handlers to define which allows them to track | ||
// objects not currently under review. For example, this is required to make | ||
// referential constraints work, or to have Constraint match criteria which | ||
// relies on more than just the object under review. | ||
// | ||
// Implementations must satisfy the per-method requirements for Client to handle | ||
// the Cache properly. | ||
type Cache interface { | ||
// Add inserts a new object into Cache with identifier key. If an object | ||
// already exists, replaces the object at key. | ||
Add(key string, object interface{}) error | ||
|
||
// Remove deletes the object at key from Cache. Deletion succeeds if key | ||
// does not exist. | ||
// Remove always succeeds; if for some reason key cannot be deleted the application | ||
// should panic. | ||
Remove(key string) | ||
} | ||
|
||
type NoCache struct{} | ||
|
||
func (n NoCache) Add(key string, object interface{}) error { | ||
return nil | ||
} | ||
|
||
func (n NoCache) Get(key string) (interface{}, error) { | ||
return nil, nil | ||
} | ||
|
||
func (n NoCache) Remove(key string) {} | ||
|
||
var _ Cache = NoCache{} |
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,43 @@ | ||
package handlertest | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/open-policy-agent/frameworks/constraint/pkg/handler" | ||
) | ||
|
||
var ErrInvalidObject = errors.New("invalid object") | ||
|
||
// Cache is a threadsafe Cache for the test Handler which keeps track of | ||
// Namespaces. | ||
type Cache struct { | ||
Namespaces sync.Map | ||
} | ||
|
||
var _ handler.Cache = &Cache{} | ||
|
||
// Add inserts object into Cache if object is a Namespace. | ||
func (c *Cache) Add(key string, object interface{}) error { | ||
obj, ok := object.(*Object) | ||
if !ok { | ||
return fmt.Errorf("%w: got object type %T, want %T", ErrInvalidType, object, &Object{}) | ||
} | ||
|
||
if obj.Name != "" { | ||
return nil | ||
} | ||
|
||
if obj.Namespace == "" { | ||
return fmt.Errorf("%w: must specify one of Name or Namespace", ErrInvalidObject) | ||
} | ||
|
||
c.Namespaces.Store(key, object) | ||
|
||
return nil | ||
} | ||
|
||
func (c *Cache) Remove(key string) { | ||
c.Namespaces.Delete(key) | ||
} |
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
Oops, something went wrong.