Skip to content

Commit

Permalink
Add warning when attempting to cache referential data when no driver …
Browse files Browse the repository at this point in the history
…supports it

Signed-off-by: Max Smythe <smythe@google.com>
  • Loading branch information
maxsmythe committed Mar 4, 2023
1 parent 8d38e31 commit 19851c7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
9 changes: 9 additions & 0 deletions constraint/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ type Client struct {
// added to the client.
driverPriority map[string]int

// ignoreNoReferentialDriverWarning toggles whether we warn the user
// when there is no registered driver that supports referential data when
// they call AddData()
ignoreNoReferentialDriverWarning bool

// drivers contains the drivers for policy engines understood
// by the constraint framework client.
// Does not require mutex locking as Driver is threadsafe
Expand Down Expand Up @@ -547,6 +552,8 @@ func (c *Client) AddData(ctx context.Context, data interface{}) (*types.Response
}
continue
}
} else if !c.ignoreNoReferentialDriverWarning {
errMap[name] = ErrNoReferentialDriver
}

resp.Handled[name] = true
Expand Down Expand Up @@ -586,6 +593,8 @@ func (c *Client) RemoveData(ctx context.Context, data interface{}) (*types.Respo
errMap[target] = err
continue
}
} else if !c.ignoreNoReferentialDriverWarning {
errMap[target] = ErrNoReferentialDriver
}

resp.Handled[target] = true
Expand Down
7 changes: 7 additions & 0 deletions constraint/pkg/client/client_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,10 @@ func Driver(d drivers.Driver) Opt {
return nil
}
}

func IgnoreNoReferentialDriverWarning(ignore bool) Opt {
return func(client *Client) error {
client.ignoreNoReferentialDriverWarning = ignore
return nil
}
}
6 changes: 4 additions & 2 deletions constraint/pkg/client/drivers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ type Driver interface {
RemoveConstraint(ctx context.Context, constraint *unstructured.Unstructured) error

// AddData caches data to be used for referential Constraints. Replaces data
// if it already exists at the specified path.
// if it already exists at the specified path. This is a deprecated method that
// will only be called for the "Rego" driver.
AddData(ctx context.Context, target string, path storage.Path, data interface{}) error
// RemoveData removes cached data, so the data at the specified path can no
// longer be used in referential Constraints.
// longer be used in referential Constraints. This is a deprecated method that
// will only be called for the "Rego" driver.
RemoveData(ctx context.Context, target string, path storage.Path) error

// Query runs the passed target's Constraints against review.
Expand Down
1 change: 1 addition & 0 deletions constraint/pkg/client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
var (
ErrCreatingBackend = errors.New("unable to create backend")
ErrNoDriverName = errors.New("driver has no name")
ErrNoReferentialDriver = errors.New("no driver that supports referential constraints added")
ErrDuplicateDriver = errors.New("duplicate drivers of the same name")
ErrCreatingClient = errors.New("unable to create client")
ErrMissingConstraint = errors.New("missing Constraint")
Expand Down

0 comments on commit 19851c7

Please sign in to comment.