@@ -10,6 +10,7 @@ import (
1010 "github.com/open-policy-agent/frameworks/constraint/pkg/client/reviews"
1111 "github.com/open-policy-agent/gatekeeper/v3/pkg/drivers/k8scel"
1212 "github.com/open-policy-agent/gatekeeper/v3/pkg/expansion"
13+ "github.com/open-policy-agent/gatekeeper/v3/pkg/gator"
1314 "github.com/open-policy-agent/gatekeeper/v3/pkg/gator/expand"
1415 "github.com/open-policy-agent/gatekeeper/v3/pkg/gator/reader"
1516 mutationtypes "github.com/open-policy-agent/gatekeeper/v3/pkg/mutation/types"
@@ -29,28 +30,26 @@ func init() {
2930 }
3031}
3132
32- // options for the Test func.
33- type Opts struct {
34- // Driver specific options
35- IncludeTrace bool
36- GatherStats bool
37- UseK8sCEL bool
38- }
39-
40- func Test (objs []* unstructured.Unstructured , tOpts Opts ) (* GatorResponses , error ) {
33+ func Test (objs []* unstructured.Unstructured , opts ... gator.Opt ) (* GatorResponses , error ) {
4134 args := []constraintclient.Opt {constraintclient .Targets (& target.K8sValidationTarget {})}
42- if tOpts .UseK8sCEL {
43- k8sDriver , err := makeK8sCELDriver (tOpts )
35+
36+ driverArgs := []rego.Arg {}
37+
38+ for _ , opt := range opts {
39+ extraArgs , extraDriverArgs , err := opt ()
4440 if err != nil {
45- return nil , fmt . Errorf ( "creating K8s native driver: %w" , err )
41+ return nil , err
4642 }
47- args = append (args , constraintclient .Driver (k8sDriver ))
43+
44+ args = append (args , extraArgs ... )
45+ driverArgs = append (driverArgs , extraDriverArgs ... )
4846 }
4947
50- driver , err := makeRegoDriver ( tOpts )
48+ driver , err := rego . New ( driverArgs ... )
5149 if err != nil {
5250 return nil , fmt .Errorf ("creating Rego driver: %w" , err )
5351 }
52+
5453 args = append (args , constraintclient .Driver (driver ), constraintclient .EnforcementPoints (util .GatorEnforcementPoint ))
5554
5655 client , err := constraintclient .NewClient (args ... )
@@ -176,24 +175,37 @@ func Test(objs []*unstructured.Unstructured, tOpts Opts) (*GatorResponses, error
176175 return responses , nil
177176}
178177
179- func makeRegoDriver (tOpts Opts ) (* rego.Driver , error ) {
180- var args []rego.Arg
181- if tOpts .GatherStats {
182- args = append (args , rego .GatherStats ())
183- }
184- if tOpts .IncludeTrace {
185- args = append (args , rego .Tracing (tOpts .IncludeTrace ))
178+ func WithGatherStats () gator.Opt {
179+ return func () ([]constraintclient.Opt , []rego.Arg , error ) {
180+ return []constraintclient.Opt {}, []rego.Arg {
181+ rego .GatherStats (),
182+ }, nil
186183 }
184+ }
187185
188- return rego .New (args ... )
186+ func WithTrace () gator.Opt {
187+ return func () ([]constraintclient.Opt , []rego.Arg , error ) {
188+ return []constraintclient.Opt {}, []rego.Arg {
189+ rego .Tracing (true ),
190+ }, nil
191+ }
189192}
190193
191- func makeK8sCELDriver (tOpts Opts ) (* k8scel.Driver , error ) {
192- var args []k8scel.Arg
194+ func WithK8sCEL (gatherStats bool ) gator.Opt {
195+ return func () ([]constraintclient.Opt , []rego.Arg , error ) {
196+ var args []k8scel.Arg
193197
194- if tOpts . GatherStats {
195- args = append (args , k8scel .GatherStats ())
196- }
198+ if gatherStats {
199+ args = append (args , k8scel .GatherStats ())
200+ }
197201
198- return k8scel .New (args ... )
202+ k8sDriver , err := k8scel .New (args ... )
203+ if err != nil {
204+ return []constraintclient.Opt {}, []rego.Arg {}, fmt .Errorf ("creating K8s native driver: %w" , err )
205+ }
206+
207+ return []constraintclient.Opt {
208+ constraintclient .Driver (k8sDriver ),
209+ }, []rego.Arg {}, nil
210+ }
199211}
0 commit comments