Skip to content

Commit

Permalink
refactor: engine refactor (#274)
Browse files Browse the repository at this point in the history
* refactor: engine refactor

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

* fixes

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

* fixes

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

* todos

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

* rm

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

* api

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

* any

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

* codegen

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

* model

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

---------

Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Jan 10, 2024
1 parent f1cfec6 commit 7fe4b0e
Show file tree
Hide file tree
Showing 28 changed files with 257 additions and 243 deletions.
4 changes: 2 additions & 2 deletions pkg/apis/v1alpha1/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Any struct {
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Schemaless
// +optional
Value interface{} `json:",inline"`
Value any `json:",inline"`
}

func (in *Any) DeepCopyInto(out *Any) {
Expand All @@ -36,7 +36,7 @@ func (a *Any) MarshalJSON() ([]byte, error) {
}

func (a *Any) UnmarshalJSON(data []byte) error {
var v interface{}
var v any
err := json.Unmarshal(data, &v)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions pkg/apis/v1alpha1/any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestAny_DeepCopyInto(t *testing.T) {
out: &Any{nil},
}, {
name: "slice",
in: &Any{[]interface{}{42, "string"}},
in: &Any{[]any{42, "string"}},
out: &Any{nil},
}}
for _, tt := range tests {
Expand All @@ -39,7 +39,7 @@ func TestAny_DeepCopyInto(t *testing.T) {
func TestAny_MarshalJSON(t *testing.T) {
tests := []struct {
name string
value interface{}
value any
want []byte
wantErr bool
}{{
Expand All @@ -59,7 +59,7 @@ func TestAny_MarshalJSON(t *testing.T) {
wantErr: false,
}, {
name: "map",
value: map[string]interface{}{"foo": 42},
value: map[string]any{"foo": 42},
want: []byte(`{"foo":42}`),
wantErr: false,
}, {
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestAny_UnmarshalJSON(t *testing.T) {
}, {
name: "map",
data: []byte(`{"foo":42}`),
want: &Any{Value: map[string]interface{}{"foo": 42.0}},
want: &Any{Value: map[string]any{"foo": 42.0}},
wantErr: false,
}, {
name: "error",
Expand Down
12 changes: 6 additions & 6 deletions pkg/commands/jp/query/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,36 +126,36 @@ func loadQueries(cmd *cobra.Command, args []string, files []string) ([]string, e
return queries, nil
}

func readInput(cmd *cobra.Command) (interface{}, error) {
func readInput(cmd *cobra.Command) (any, error) {
fmt.Fprintln(cmd.OutOrStdout(), "Reading from terminal input.")
fmt.Fprintln(cmd.OutOrStdout(), "Enter input object and hit Ctrl+D.")
data, err := readFile(cmd.InOrStdin())
if err != nil {
return nil, err
}
var input interface{}
var input any
if err := yaml.Unmarshal(data, &input); err != nil {
return nil, fmt.Errorf("error parsing input json: %w", err)
}
return input, nil
}

func loadInput(cmd *cobra.Command, file string) (interface{}, error) {
func loadInput(cmd *cobra.Command, file string) (any, error) {
if file == "" {
return nil, nil
}
data, err := loadFile(cmd, file)
if err != nil {
return nil, err
}
var input interface{}
var input any
if err := yaml.Unmarshal(data, &input); err != nil {
return nil, fmt.Errorf("error parsing input json: %w", err)
}
return input, nil
}

func evaluate(input interface{}, query string) (interface{}, error) {
func evaluate(input any, query string) (any, error) {
result, err := template.Execute(context.Background(), query, input, nil)
if err != nil {
if syntaxError, ok := err.(parsing.SyntaxError); ok {
Expand All @@ -166,7 +166,7 @@ func evaluate(input interface{}, query string) (interface{}, error) {
return result, nil
}

func printResult(cmd *cobra.Command, query string, result interface{}, unquoted bool, compact bool) error {
func printResult(cmd *cobra.Command, query string, result any, unquoted bool, compact bool) error {
converted, isString := result.(string)
fmt.Fprintln(cmd.OutOrStdout(), "#", query)
if unquoted && isString {
Expand Down
26 changes: 15 additions & 11 deletions pkg/commands/scan/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,33 @@ func (c *options) run(cmd *cobra.Command, _ []string) error {
}
payload = result
}
var resources []interface{}
if slice, ok := payload.([]interface{}); ok {
var resources []any
if slice, ok := payload.([]any); ok {
resources = slice
} else {
resources = append(resources, payload)
}
out.println("Running", "(", "evaluating", len(resources), pluralize.Pluralize(len(resources), "resource", "resources"), "against", len(policies), pluralize.Pluralize(len(policies), "policy", "policies"), ")", "...")
e := jsonengine.New()
var responses []jsonengine.RuleResponse
var responses []jsonengine.Response
for _, resource := range resources {
responses = append(responses, e.Run(context.Background(), jsonengine.Request{
Resource: resource,
Policies: policies,
})...)
}))
}
for _, response := range responses {
if response.Result == jsonengine.StatusFail {
out.println("-", response.PolicyName, "/", response.RuleName, "/", response.Identifier, "FAILED:", response.Message)
} else if response.Result == jsonengine.StatusError {
out.println("-", response.PolicyName, "/", response.RuleName, "/", response.Identifier, "ERROR:", response.Message)
} else {
// TODO: handle skip, warn
out.println("-", response.PolicyName, "/", response.RuleName, "/", response.Identifier, "PASSED")
for _, policy := range response.Policies {
for _, rule := range policy.Rules {
if rule.Result == jsonengine.StatusFail {
out.println("-", policy.Policy.Name, "/", rule.Rule.Name, "/", rule.Identifier, "FAILED:", rule.Message)
} else if rule.Result == jsonengine.StatusError {
out.println("-", policy.Policy.Name, "/", rule.Rule.Name, "/", rule.Identifier, "ERROR:", rule.Message)
} else {
// TODO: handle skip, warn
out.println("-", policy.Policy.Name, "/", rule.Rule.Name, "/", rule.Identifier, "PASSED")
}
}
}
}
out.responses(responses...)
Expand Down
8 changes: 4 additions & 4 deletions pkg/commands/scan/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type output interface {
println(args ...any)
responses(responses ...jsonengine.RuleResponse)
responses(responses ...jsonengine.Response)
}

type textOutput struct {
Expand All @@ -21,7 +21,7 @@ func (t *textOutput) println(args ...any) {
fmt.Fprintln(t.out, args...)
}

func (t *textOutput) responses(responses ...jsonengine.RuleResponse) {
func (t *textOutput) responses(responses ...jsonengine.Response) {
}

type jsonOutput struct {
Expand All @@ -31,8 +31,8 @@ type jsonOutput struct {
func (t *jsonOutput) println(args ...any) {
}

func (t *jsonOutput) responses(responses ...jsonengine.RuleResponse) {
payload, err := json.MarshalIndent(&jsonengine.Response{Results: responses}, "", " ")
func (t *jsonOutput) responses(responses ...jsonengine.Response) {
payload, err := json.MarshalIndent(responses, "", " ")
if err != nil {
fmt.Fprintln(t.out, err)
} else {
Expand Down
10 changes: 5 additions & 5 deletions pkg/engine/blocks/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
)

type constant[TREQUEST any, TRESPONSE any] struct {
responses []TRESPONSE
response TRESPONSE
}

func (b *constant[TREQUEST, TRESPONSE]) Run(_ context.Context, _ TREQUEST) []TRESPONSE {
return b.responses
func (b *constant[TREQUEST, TRESPONSE]) Run(_ context.Context, _ TREQUEST) TRESPONSE {
return b.response
}

func New[TREQUEST any, TRESPONSE any](responses ...TRESPONSE) engine.Engine[TREQUEST, TRESPONSE] {
func New[TREQUEST any, TRESPONSE any](response TRESPONSE) engine.Engine[TREQUEST, TRESPONSE] {
return &constant[TREQUEST, TRESPONSE]{
responses: responses,
response: response,
}
}
4 changes: 2 additions & 2 deletions pkg/engine/blocks/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ type function[TREQUEST any, TRESPONSE any] struct {
function func(context.Context, TREQUEST) TRESPONSE
}

func (b *function[TREQUEST, TRESPONSE]) Run(ctx context.Context, request TREQUEST) []TRESPONSE {
return []TRESPONSE{b.function(ctx, request)}
func (b *function[TREQUEST, TRESPONSE]) Run(ctx context.Context, request TREQUEST) TRESPONSE {
return b.function(ctx, request)
}

func New[TREQUEST any, TRESPONSE any](f func(context.Context, TREQUEST) TRESPONSE) engine.Engine[TREQUEST, TRESPONSE] {
Expand Down
27 changes: 0 additions & 27 deletions pkg/engine/blocks/loop/loop.go

This file was deleted.

17 changes: 0 additions & 17 deletions pkg/engine/blocks/null/null.go

This file was deleted.

26 changes: 0 additions & 26 deletions pkg/engine/blocks/predicate/predicate.go

This file was deleted.

9 changes: 2 additions & 7 deletions pkg/engine/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/kyverno/kyverno-json/pkg/engine"
"github.com/kyverno/kyverno-json/pkg/engine/blocks/constant"
"github.com/kyverno/kyverno-json/pkg/engine/blocks/function"
"github.com/kyverno/kyverno-json/pkg/engine/blocks/predicate"
)

type Engine[TREQUEST any, TRESPONSE any] struct {
Expand All @@ -17,12 +16,8 @@ func new[TREQUEST any, TRESPONSE any](engine engine.Engine[TREQUEST, TRESPONSE])
return Engine[TREQUEST, TRESPONSE]{engine}
}

func Constant[TREQUEST any, TRESPONSE any](responses ...TRESPONSE) Engine[TREQUEST, TRESPONSE] {
return new(constant.New[TREQUEST](responses...))
}

func (inner Engine[TREQUEST, TRESPONSE]) Predicate(condition func(context.Context, TREQUEST) bool) Engine[TREQUEST, TRESPONSE] {
return new(predicate.New(inner, condition))
func Constant[TREQUEST any, TRESPONSE any](response TRESPONSE) Engine[TREQUEST, TRESPONSE] {
return new(constant.New[TREQUEST](response))
}

func Function[TREQUEST any, TRESPONSE any](f func(context.Context, TREQUEST) TRESPONSE) Engine[TREQUEST, TRESPONSE] {
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import (
// - explain

type Engine[TREQUEST any, TRESPONSE any] interface {
Run(context.Context, TREQUEST) []TRESPONSE
Run(context.Context, TREQUEST) TRESPONSE
}
Loading

0 comments on commit 7fe4b0e

Please sign in to comment.