Skip to content
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

feat: add tracing #61

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions pkg/commands/scan/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ func Command() *cobra.Command {
cmd.Flags().StringVar(&command.payload, "payload", "", "Path to payload (json or yaml file)")
cmd.Flags().StringSliceVar(&command.preprocessors, "pre-process", nil, "JmesPath expression used to pre process payload")
cmd.Flags().StringSliceVar(&command.policies, "policy", nil, "Path to kyverno-json policies")
cmd.Flags().BoolVar(&command.trace, "trace", false, "Enable tracing")
return cmd
}
9 changes: 7 additions & 2 deletions pkg/commands/scan/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
jsonengine "github.com/kyverno/kyverno-json/pkg/json-engine"
"github.com/kyverno/kyverno-json/pkg/payload"
"github.com/kyverno/kyverno-json/pkg/policy"
"github.com/kyverno/kyverno-json/pkg/tracing"
"github.com/kyverno/kyverno-json/pkg/tracing/tracer"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/output/pluralize"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -18,9 +20,12 @@ type options struct {
payload string
preprocessors []string
policies []string
trace bool
}

func (c *options) run(cmd *cobra.Command, _ []string) error {
var tracer tracer.Tracer
ctx := tracing.WithTracer(context.Background(), &tracer)
out := cmd.OutOrStdout()
fmt.Fprintln(out, "Loading policies ...")
policies, err := policy.Load(c.policies...)
Expand All @@ -37,7 +42,7 @@ func (c *options) run(cmd *cobra.Command, _ []string) error {
}
fmt.Fprintln(out, "Pre processing ...")
for _, preprocessor := range c.preprocessors {
result, err := template.Execute(context.Background(), preprocessor, payload, nil)
result, err := template.Execute(ctx, preprocessor, payload, nil)
if err != nil {
return err
}
Expand All @@ -54,7 +59,7 @@ func (c *options) run(cmd *cobra.Command, _ []string) error {
}
fmt.Fprintln(out, "Running", "(", "evaluating", len(resources), pluralize.Pluralize(len(resources), "resource", "resources"), "against", len(policies), pluralize.Pluralize(len(policies), "policy", "policies"), ")", "...")
e := jsonengine.New()
responses := e.Run(context.Background(), jsonengine.JsonEngineRequest{
responses := e.Run(ctx, jsonengine.JsonEngineRequest{
Resources: resources,
Policies: policies,
})
Expand Down
28 changes: 26 additions & 2 deletions pkg/json-engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/kyverno/kyverno-json/pkg/engine/blocks/loop"
"github.com/kyverno/kyverno-json/pkg/engine/builder"
"github.com/kyverno/kyverno-json/pkg/engine/template"
"github.com/kyverno/kyverno-json/pkg/tracing"
)

type JsonEngineRequest struct {
Expand Down Expand Up @@ -75,15 +76,38 @@ func New() engine.Engine[JsonEngineRequest, JsonEngineResponse] {
return response
}).
Predicate(func(ctx context.Context, r request) bool {
if r.rule.Exclude == nil {
tracing.Tracef(ctx, "No exclude statement, will never exclude")
return true
}
errs, err := assert.Match(ctx, nil, r.rule.Exclude, r.value, r.bindings)
return err == nil && len(errs) != 0
if err != nil {
tracing.Tracef(ctx, "An error occurred while matching exclude: %s", err)
return false
} else if len(errs) == 0 {
tracing.Trace(ctx, "Exclude statement matched the resource")
return false
} else {
tracing.Tracef(ctx, "Exclude statement didn't match the resource: %s", errs.ToAggregate())
return true
}
}).
Predicate(func(ctx context.Context, r request) bool {
if r.rule.Match == nil {
tracing.Tracef(ctx, "No match statement, will always match")
return true
}
errs, err := assert.Match(ctx, nil, r.rule.Match, r.value, r.bindings)
return err == nil && len(errs) == 0
if err != nil {
tracing.Tracef(ctx, "An error occurred while matching: %s", err)
return false
} else if len(errs) == 0 {
tracing.Trace(ctx, "Match statement matched the resource")
return true
} else {
tracing.Tracef(ctx, "Match statement didn't match the resource: %s", errs.ToAggregate())
return false
}
})
// TODO: we can't use the builder package for loops :(
return loop.New(inner, looper)
Expand Down
13 changes: 13 additions & 0 deletions pkg/tracing/tracer/tracer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tracer

type Tracer struct {
traces []string
}

func (t *Tracer) Trace(trace string) {
t.traces = append(t.traces, trace)
}

func (t *Tracer) Traces() []string {
return t.traces
}
34 changes: 34 additions & 0 deletions pkg/tracing/tracing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tracing

import (
"context"
"fmt"
)

type Tracer interface {
Trace(string)
}

type contextKey struct{}

func WithTracer(ctx context.Context, tracer Tracer) context.Context {
return context.WithValue(ctx, contextKey{}, tracer)
}

func Trace(ctx context.Context, a ...any) {
value := ctx.Value(contextKey{})
if value != nil {
if tracer, ok := value.(Tracer); ok {
tracer.Trace(fmt.Sprint(a...))
}
}
}

func Tracef(ctx context.Context, format string, a ...any) {
value := ctx.Value(contextKey{})
if value != nil {
if tracer, ok := value.(Tracer); ok {
tracer.Trace(fmt.Sprintf(format, a...))
}
}
}
Loading