Skip to content

feat: support print statement in gator (#2949) #3872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion cmd/gator/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func runE(cmd *cobra.Command, args []string) error {
func runSuites(ctx context.Context, fileSystem fs.FS, suites []*verify.Suite, filter verify.Filter) error {
isFailure := false

runner, err := verify.NewRunner(fileSystem, gator.NewOPAClient, verify.IncludeTrace(includeTrace), verify.UseK8sCEL(flagEnableK8sCel))
runner, err := verify.NewRunner(fileSystem, gator.NewOPAClient, verify.IncludeTrace(includeTrace), verify.EnablePrint(verbose), verify.UseK8sCEL(flagEnableK8sCel))
if err != nil {
return err
}
Expand Down
16 changes: 14 additions & 2 deletions pkg/gator/opa.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"github.com/open-policy-agent/gatekeeper/v3/pkg/drivers/k8scel"
"github.com/open-policy-agent/gatekeeper/v3/pkg/target"
"github.com/open-policy-agent/gatekeeper/v3/pkg/util"
"os"
)

func NewOPAClient(includeTrace bool, k8sCEL bool) (Client, error) {
func NewOPAClient(includeTrace bool, printEnabled bool, k8sCEL bool) (Client, error) {
args := []constraintclient.Opt{constraintclient.Targets(&target.K8sValidationTarget{})}

if k8sCEL {
Expand All @@ -19,7 +20,18 @@ func NewOPAClient(includeTrace bool, k8sCEL bool) (Client, error) {
args = append(args, constraintclient.Driver(k8sDriver))
}

driver, err := rego.New(rego.Tracing(includeTrace))
driverArgs := []rego.Arg{
rego.Tracing(includeTrace),
}

if printEnabled {
driverArgs = append(driverArgs,
rego.PrintEnabled(printEnabled),
rego.PrintHook(NewPrintHook(os.Stdout)),
)
}

driver, err := rego.New(driverArgs...)
if err != nil {
return nil, err
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/gator/print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gator

import (
"fmt"
v1 "github.com/open-policy-agent/opa/v1/topdown/print"
"io"
)

type PrintHook struct {
writer io.Writer
}

func NewPrintHook(writer io.Writer) PrintHook {
return PrintHook{writer}
}

func (h PrintHook) Print(ctx v1.Context, string string) error {
_, err := fmt.Fprintln(h.writer, string)
return err
}
16 changes: 12 additions & 4 deletions pkg/gator/verify/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ type Runner struct {

// newClient instantiates a Client for compiling Templates/Constraints, and
// validating objects against them.
newClient func(includeTrace bool, useK8sCEL bool) (gator.Client, error)
newClient func(includeTrace bool, printEnabled bool, useK8sCEL bool) (gator.Client, error)

scheme *runtime.Scheme

includeTrace bool

printEnabled bool

useK8sCEL bool
}

func NewRunner(filesystem fs.FS, newClient func(includeTrace bool, useK8sCEL bool) (gator.Client, error), opts ...RunnerOptions) (*Runner, error) {
func NewRunner(filesystem fs.FS, newClient func(includeTrace bool, printEnabled bool, useK8sCEL bool) (gator.Client, error), opts ...RunnerOptions) (*Runner, error) {
s := runtime.NewScheme()
err := apis.AddToScheme(s)
if err != nil {
Expand Down Expand Up @@ -71,6 +73,12 @@ func IncludeTrace(includeTrace bool) RunnerOptions {
}
}

func EnablePrint(printEnabled bool) RunnerOptions {
return func(r *Runner) {
r.printEnabled = printEnabled
}
}

func UseK8sCEL(useK8sCEL bool) RunnerOptions {
return func(r *Runner) {
r.useK8sCEL = useK8sCEL
Expand Down Expand Up @@ -144,7 +152,7 @@ func (r *Runner) runTest(ctx context.Context, suiteDir string, filter Filter, t
}

func (r *Runner) tryAddConstraint(ctx context.Context, suiteDir string, t *Test) error {
client, err := r.newClient(r.includeTrace, r.useK8sCEL)
client, err := r.newClient(r.includeTrace, r.printEnabled, r.useK8sCEL)
if err != nil {
return fmt.Errorf("%w: %w", gator.ErrCreatingClient, err)
}
Expand Down Expand Up @@ -213,7 +221,7 @@ func (r *Runner) skipCase(tc *Case) CaseResult {
}

func (r *Runner) makeTestClient(ctx context.Context, suiteDir string, t *Test) (gator.Client, error) {
client, err := r.newClient(r.includeTrace, r.useK8sCEL)
client, err := r.newClient(r.includeTrace, r.printEnabled, r.useK8sCEL)
if err != nil {
return nil, fmt.Errorf("%w: %w", gator.ErrCreatingClient, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gator/verify/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ func TestRunner_Run_ClientError(t *testing.T) {
TestResults: []TestResult{{Error: gator.ErrCreatingClient}},
}

runner, err := NewRunner(fstest.MapFS{}, func(_ bool, _ bool) (gator.Client, error) {
runner, err := NewRunner(fstest.MapFS{}, func(_ bool, _ bool, _ bool) (gator.Client, error) {
return nil, errors.New("error")
})
if err != nil {
Expand Down