Skip to content

Commit

Permalink
feat: add compiler field in configuration api
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly committed Sep 29, 2024
1 parent d56d81b commit b40ca42
Show file tree
Hide file tree
Showing 74 changed files with 405 additions and 207 deletions.
14 changes: 14 additions & 0 deletions .crds/chainsaw.kyverno.io_configurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,13 @@ spec:
description: Clusters holds a registry to clusters to support multi-cluster
tests.
type: object
compiler:
description: Compiler defines the default compiler to use when evaluating
expressions.
enum:
- jp
- cel
type: string
delayBeforeCleanup:
description: DelayBeforeCleanup adds a delay between the time a test
ends and the time cleanup starts.
Expand Down Expand Up @@ -1883,6 +1890,13 @@ spec:
default: {}
description: Templating contains the templating config.
properties:
compiler:
description: Compiler defines the default compiler to use when
evaluating expressions.
enum:
- jp
- cel
type: string
enabled:
default: true
description: Enabled determines whether resources should be considered
Expand Down
11 changes: 11 additions & 0 deletions .schemas/json/configuration-chainsaw-v1alpha1.json
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,17 @@
"additionalProperties": false
}
},
"compiler": {
"description": "Compiler defines the default compiler to use when evaluating expressions.",
"type": [
"string",
"null"
],
"enum": [
"jp",
"cel"
]
},
"delayBeforeCleanup": {
"description": "DelayBeforeCleanup adds a delay between the time a test ends and the time cleanup starts.",
"type": [
Expand Down
11 changes: 11 additions & 0 deletions .schemas/json/configuration-chainsaw-v1alpha2.json
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,17 @@
],
"default": {},
"properties": {
"compiler": {
"description": "Compiler defines the default compiler to use when evaluating expressions.",
"type": [
"string",
"null"
],
"enum": [
"jp",
"cel"
]
},
"enabled": {
"description": "Enabled determines whether resources should be considered for templating.",
"type": [
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var (
Jp: jp.NewCompiler(jp.WithFunctionCaller(functions.Caller())),
Cel: cel.NewCompiler(),
}
DefaultCompilers = defaultCompilers.WithDefaultCompiler(compilers.CompilerJP)
XDefaultCompilers = defaultCompilers.WithDefaultCompiler(compilers.CompilerJP)
)

type Bindings = binding.Bindings
Expand Down
4 changes: 3 additions & 1 deletion pkg/apis/conversion/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func Convert_v1alpha2_ConfigurationSpec_To_v1alpha1_ConfigurationSpec(in *v1alph
out.ReportName = in.Name
}
out.Template = in.Templating.Enabled
out.Compiler = in.Templating.Compiler

Check warning on line 32 in pkg/apis/conversion/configuration.go

View check run for this annotation

Codecov / codecov/patch

pkg/apis/conversion/configuration.go#L32

Added line #L32 was not covered by tests
out.Timeouts = in.Timeouts
return nil
}
Expand Down Expand Up @@ -68,7 +69,8 @@ func Convert_v1alpha1_ConfigurationSpec_To_v1alpha2_ConfigurationSpec(in *v1alph
Name: in.ReportName,
}
out.Templating = v1alpha2.TemplatingOptions{
Enabled: in.Template,
Enabled: in.Template,
Compiler: in.Compiler,
}
out.Timeouts = in.Timeouts
return nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/v1alpha1/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type ConfigurationSpec struct {
// +kubebuilder:default:=true
Template bool `json:"template"`

// Compiler defines the default compiler to use when evaluating expressions.
// +optional
Compiler *Compiler `json:"compiler,omitempty"`

// FailFast determines whether the test should stop upon encountering the first failure.
// +optional
FailFast bool `json:"failFast,omitempty"`
Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/kyverno/chainsaw/pkg/apis"
"github.com/kyverno/chainsaw/pkg/expressions"
"github.com/kyverno/kyverno-json/pkg/apis/policy/v1alpha1"
"github.com/kyverno/kyverno-json/pkg/core/compilers"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -102,8 +103,8 @@ func (e *Expression) UnmarshalJSON(data []byte) error {
return nil
}

func (e Expression) Value(ctx context.Context, bindings apis.Bindings) (string, error) {
return expressions.String(ctx, string(e), bindings)
func (e Expression) Value(ctx context.Context, compilers compilers.Compilers, bindings apis.Bindings) (string, error) {
return expressions.String(ctx, compilers, string(e), bindings)

Check warning on line 107 in pkg/apis/v1alpha1/types.go

View check run for this annotation

Codecov / codecov/patch

pkg/apis/v1alpha1/types.go#L106-L107

Added lines #L106 - L107 were not covered by tests
}

// Format determines the output format (json or yaml).
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions pkg/apis/v1alpha2/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package v1alpha2

import (
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
kjson "github.com/kyverno/kyverno-json/pkg/apis/policy/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -89,7 +88,7 @@ type NamespaceOptions struct {

// Compiler defines the default compiler to use when evaluating expressions.
// +optional
Compiler *kjson.Compiler `json:"compiler,omitempty"`
Compiler *Compiler `json:"compiler,omitempty"`

// Template defines a template to create the test namespace.
// +optional
Expand Down Expand Up @@ -130,4 +129,8 @@ type TemplatingOptions struct {
// +optional
// +kubebuilder:default:=true
Enabled bool `json:"enabled"`

// Compiler defines the default compiler to use when evaluating expressions.
// +optional
Compiler *Compiler `json:"compiler,omitempty"`
}
1 change: 1 addition & 0 deletions pkg/apis/v1alpha2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

type (
Clusters = v1alpha1.Clusters
Compiler = v1alpha1.Compiler
DefaultTimeouts = v1alpha1.DefaultTimeouts
Projection = v1alpha1.Projection
)
7 changes: 6 additions & 1 deletion pkg/apis/v1alpha2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/commands/assert/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"time"

"github.com/kyverno/chainsaw/pkg/apis"
"github.com/kyverno/chainsaw/pkg/client"
"github.com/kyverno/chainsaw/pkg/client/simple"
tclient "github.com/kyverno/chainsaw/pkg/client/testing"
Expand Down Expand Up @@ -139,7 +140,7 @@ func runE(opts options, cmd *cobra.Command, client client.Client, namespacer nsp
func assert(opts options, client client.Client, resource unstructured.Unstructured, namespacer nspacer.Namespacer) error {
ctx, cancel := context.WithTimeout(context.Background(), opts.timeout.Duration)
defer cancel()
op := opassert.New(client, resource, namespacer, false)
op := opassert.New(apis.XDefaultCompilers, client, resource, namespacer, false)
_, err := op.Exec(ctx, nil)
return err
}
14 changes: 14 additions & 0 deletions pkg/data/crds/chainsaw.kyverno.io_configurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,13 @@ spec:
description: Clusters holds a registry to clusters to support multi-cluster
tests.
type: object
compiler:
description: Compiler defines the default compiler to use when evaluating
expressions.
enum:
- jp
- cel
type: string
delayBeforeCleanup:
description: DelayBeforeCleanup adds a delay between the time a test
ends and the time cleanup starts.
Expand Down Expand Up @@ -1883,6 +1890,13 @@ spec:
default: {}
description: Templating contains the templating config.
properties:
compiler:
description: Compiler defines the default compiler to use when
evaluating expressions.
enum:
- jp
- cel
type: string
enabled:
default: true
description: Enabled determines whether resources should be considered
Expand Down
11 changes: 11 additions & 0 deletions pkg/data/schemas/json/configuration-chainsaw-v1alpha1.json
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,17 @@
"additionalProperties": false
}
},
"compiler": {
"description": "Compiler defines the default compiler to use when evaluating expressions.",
"type": [
"string",
"null"
],
"enum": [
"jp",
"cel"
]
},
"delayBeforeCleanup": {
"description": "DelayBeforeCleanup adds a delay between the time a test ends and the time cleanup starts.",
"type": [
Expand Down
11 changes: 11 additions & 0 deletions pkg/data/schemas/json/configuration-chainsaw-v1alpha2.json
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,17 @@
],
"default": {},
"properties": {
"compiler": {
"description": "Compiler defines the default compiler to use when evaluating expressions.",
"type": [
"string",
"null"
],
"enum": [
"jp",
"cel"
]
},
"enabled": {
"description": "Enabled determines whether resources should be considered for templating.",
"type": [
Expand Down
6 changes: 3 additions & 3 deletions pkg/engine/bindings/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/kyverno/chainsaw/pkg/apis"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/engine/templating"
"github.com/kyverno/kyverno-json/pkg/core/compilers"
)

var identifier = regexp.MustCompile(`^\w+$`)
Expand All @@ -23,15 +24,14 @@ func RegisterBinding(ctx context.Context, bindings apis.Bindings, name string, v
return bindings.Register("$"+name, apis.NewBinding(value))
}

func ResolveBinding(ctx context.Context, bindings apis.Bindings, input any, variable v1alpha1.Binding) (string, any, error) {
name, err := variable.Name.Value(ctx, bindings)
func ResolveBinding(ctx context.Context, compilers compilers.Compilers, bindings apis.Bindings, input any, variable v1alpha1.Binding) (string, any, error) {
name, err := variable.Name.Value(ctx, compilers, bindings)
if err != nil {
return "", nil, err
}
if err := checkBindingName(name); err != nil {
return "", nil, err
}
compilers := apis.DefaultCompilers
if variable.Compiler != nil {
compilers = compilers.WithDefaultCompiler(string(*variable.Compiler))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/bindings/bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestResolveBinding(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
name, value, err := ResolveBinding(context.TODO(), tt.bindings, tt.input, tt.variable)
name, value, err := ResolveBinding(context.TODO(), apis.XDefaultCompilers, tt.bindings, tt.input, tt.variable)
if tt.wantErr {
assert.Error(t, err)
} else {
Expand Down
5 changes: 3 additions & 2 deletions pkg/engine/checks/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import (

"github.com/kyverno/chainsaw/pkg/apis"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/kyverno-json/pkg/core/compilers"
"k8s.io/apimachinery/pkg/util/validation/field"
)

func Check(ctx context.Context, obj any, bindings apis.Bindings, check *v1alpha1.Check) (field.ErrorList, error) {
func Check(ctx context.Context, compilers compilers.Compilers, obj any, bindings apis.Bindings, check *v1alpha1.Check) (field.ErrorList, error) {
if check == nil {
return nil, errors.New("check is null")
}
if check.IsNil() {
return nil, errors.New("check value is null")
}
if assertion, err := check.Compile(nil, apis.DefaultCompilers); err != nil {
if assertion, err := check.Compile(nil, compilers); err != nil {
return nil, err
} else {
if bindings == nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/checks/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestCheck(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Check(context.TODO(), tt.obj, tt.bindings, tt.check)
got, err := Check(context.TODO(), apis.XDefaultCompilers, tt.obj, tt.bindings, tt.check)
if tt.wantErr {
assert.Error(t, err)
} else {
Expand Down
7 changes: 4 additions & 3 deletions pkg/engine/checks/expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ import (

"github.com/kyverno/chainsaw/pkg/apis"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/kyverno-json/pkg/core/compilers"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
)

func Expect(ctx context.Context, obj unstructured.Unstructured, bindings apis.Bindings, expect ...v1alpha1.Expectation) (bool, error) {
func Expect(ctx context.Context, compilers compilers.Compilers, obj unstructured.Unstructured, bindings apis.Bindings, expect ...v1alpha1.Expectation) (bool, error) {
matched := false
var results field.ErrorList
for _, expectation := range expect {
// if a match is specified, skip the check if the resource doesn't match
if expectation.Match != nil && !expectation.Match.IsNil() {
if errs, err := Check(ctx, obj.UnstructuredContent(), nil, expectation.Match); err != nil {
if errs, err := Check(ctx, compilers, obj.UnstructuredContent(), nil, expectation.Match); err != nil {
return true, err
} else if len(errs) != 0 {
continue
}
}
matched = true
if errs, err := Check(ctx, obj.UnstructuredContent(), bindings, ptr.To(expectation.Check)); err != nil {
if errs, err := Check(ctx, compilers, obj.UnstructuredContent(), bindings, ptr.To(expectation.Check)); err != nil {
return true, err
} else {
results = append(results, errs...)
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/checks/expect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestExpectations(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Expect(context.TODO(), tt.obj, tt.bindings, tt.expect...)
got, err := Expect(context.TODO(), apis.XDefaultCompilers, tt.obj, tt.bindings, tt.expect...)
if tt.wantErr {
assert.Error(t, err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func WithBindings(ctx context.Context, tc Context, variables ...v1alpha1.Binding) (Context, error) {
for _, variable := range variables {
name, value, err := bindings.ResolveBinding(ctx, tc.Bindings(), nil, variable)
name, value, err := bindings.ResolveBinding(ctx, tc.Compilers(), tc.Bindings(), nil, variable)

Check warning on line 14 in pkg/engine/context.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/context.go#L14

Added line #L14 was not covered by tests
if err != nil {
return tc, err
}
Expand Down
Loading

0 comments on commit b40ca42

Please sign in to comment.