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 compiler to the namespace template api #2035

Merged
merged 1 commit into from
Sep 29, 2024
Merged
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
14 changes: 14 additions & 0 deletions .crds/chainsaw.kyverno.io_configurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,13 @@ spec:
description: NamespaceTemplate defines a template to create the test
namespace.
x-kubernetes-preserve-unknown-fields: true
namespaceTemplateCompiler:
description: NamespaceTemplateCompiler defines the default compiler
to use when evaluating expressions.
enum:
- jp
- cel
type: string
parallel:
description: The maximum number of tests to run at once.
format: int
Expand Down Expand Up @@ -1833,6 +1840,13 @@ spec:
description: Namespace contains properties for the namespace to use
for tests.
properties:
compiler:
description: Compiler defines the default compiler to use when
evaluating expressions.
enum:
- jp
- cel
type: string
name:
description: |-
Name defines the namespace to use for tests.
Expand Down
7 changes: 7 additions & 0 deletions .crds/chainsaw.kyverno.io_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,13 @@ spec:
description: NamespaceTemplate defines a template to create the test
namespace.
x-kubernetes-preserve-unknown-fields: true
namespaceTemplateCompiler:
description: NamespaceTemplateCompiler defines the default compiler
to use when evaluating expressions.
enum:
- jp
- cel
type: string
scenarios:
description: Scenarios defines test scenarios.
items:
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 @@ -1698,6 +1698,17 @@
"description": "NamespaceTemplate defines a template to create the test namespace.",
"x-kubernetes-preserve-unknown-fields": true
},
"namespaceTemplateCompiler": {
"description": "NamespaceTemplateCompiler defines the default compiler to use when evaluating expressions.",
"type": [
"string",
"null"
],
"enum": [
"jp",
"cel"
]
},
"parallel": {
"description": "The maximum number of tests to run at once.",
"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 @@ -1783,6 +1783,17 @@
],
"default": {},
"properties": {
"compiler": {
"description": "Compiler defines the default compiler to use when evaluating expressions.",
"type": [
"string",
"null"
],
"enum": [
"jp",
"cel"
]
},
"name": {
"description": "Name defines the namespace to use for tests.\nIf not specified, every test will execute in a random ephemeral namespace\nunless the namespace is overridden in a the test spec.",
"type": [
Expand Down
11 changes: 11 additions & 0 deletions .schemas/json/test-chainsaw-v1alpha1.json
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,17 @@
"description": "NamespaceTemplate defines a template to create the test namespace.",
"x-kubernetes-preserve-unknown-fields": true
},
"namespaceTemplateCompiler": {
"description": "NamespaceTemplateCompiler defines the default compiler to use when evaluating expressions.",
"type": [
"string",
"null"
],
"enum": [
"jp",
"cel"
]
},
"scenarios": {
"description": "Scenarios defines test scenarios.",
"type": [
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/conversion/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
out.ForceTerminationGracePeriod = in.Execution.ForceTerminationGracePeriod
out.Namespace = in.Namespace.Name
out.NamespaceTemplate = in.Namespace.Template
out.NamespaceTemplateCompiler = in.Namespace.Compiler
if in := in.Report; in != nil {

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

View check run for this annotation

Codecov / codecov/patch

pkg/apis/conversion/configuration.go#L25-L26

Added lines #L25 - L26 were not covered by tests
out.ReportFormat = v1alpha1.ReportFormatType(in.Format)
out.ReportPath = in.Path
out.ReportName = in.Name
Expand Down Expand Up @@ -58,6 +59,7 @@
}
out.Namespace = v1alpha2.NamespaceOptions{
Name: in.Namespace,
Compiler: in.NamespaceTemplateCompiler,
Template: in.NamespaceTemplate,
}
out.Report = &v1alpha2.ReportOptions{
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 @@ -75,6 +75,10 @@ type ConfigurationSpec struct {
// +optional
Namespace string `json:"namespace,omitempty"`

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

// NamespaceTemplate defines a template to create the test namespace.
// +optional
NamespaceTemplate *Projection `json:"namespaceTemplate,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/v1alpha1/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ type TestSpec struct {
// +optional
NamespaceTemplate *Projection `json:"namespaceTemplate,omitempty"`

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

// Scenarios defines test scenarios.
// +optional
Scenarios []Scenario `json:"scenarios,omitempty"`
Expand Down
4 changes: 3 additions & 1 deletion pkg/apis/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var (
NewMatch = v1alpha1.NewAssertionTree
)

type Compiler = v1alpha1.Compiler

// Binding represents a key/value set as a binding in an executing test.
type Binding struct {
// Name the name of the binding.
Expand All @@ -29,7 +31,7 @@ type Binding struct {

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

// Value value of the binding.
Value Projection `json:"value"`
Expand Down
10 changes: 10 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.

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

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

Expand Down Expand Up @@ -87,6 +87,10 @@ type NamespaceOptions struct {
// +optional
Name string `json:"name,omitempty"`

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

// Template defines a template to create the test namespace.
// +optional
Template *Projection `json:"template,omitempty"`
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/v1alpha2/zz_generated.deepcopy.go

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

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 @@ -860,6 +860,13 @@ spec:
description: NamespaceTemplate defines a template to create the test
namespace.
x-kubernetes-preserve-unknown-fields: true
namespaceTemplateCompiler:
description: NamespaceTemplateCompiler defines the default compiler
to use when evaluating expressions.
enum:
- jp
- cel
type: string
parallel:
description: The maximum number of tests to run at once.
format: int
Expand Down Expand Up @@ -1833,6 +1840,13 @@ spec:
description: Namespace contains properties for the namespace to use
for tests.
properties:
compiler:
description: Compiler defines the default compiler to use when
evaluating expressions.
enum:
- jp
- cel
type: string
name:
description: |-
Name defines the namespace to use for tests.
Expand Down
7 changes: 7 additions & 0 deletions pkg/data/crds/chainsaw.kyverno.io_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,13 @@ spec:
description: NamespaceTemplate defines a template to create the test
namespace.
x-kubernetes-preserve-unknown-fields: true
namespaceTemplateCompiler:
description: NamespaceTemplateCompiler defines the default compiler
to use when evaluating expressions.
enum:
- jp
- cel
type: string
scenarios:
description: Scenarios defines test scenarios.
items:
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 @@ -1698,6 +1698,17 @@
"description": "NamespaceTemplate defines a template to create the test namespace.",
"x-kubernetes-preserve-unknown-fields": true
},
"namespaceTemplateCompiler": {
"description": "NamespaceTemplateCompiler defines the default compiler to use when evaluating expressions.",
"type": [
"string",
"null"
],
"enum": [
"jp",
"cel"
]
},
"parallel": {
"description": "The maximum number of tests to run at once.",
"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 @@ -1783,6 +1783,17 @@
],
"default": {},
"properties": {
"compiler": {
"description": "Compiler defines the default compiler to use when evaluating expressions.",
"type": [
"string",
"null"
],
"enum": [
"jp",
"cel"
]
},
"name": {
"description": "Name defines the namespace to use for tests.\nIf not specified, every test will execute in a random ephemeral namespace\nunless the namespace is overridden in a the test spec.",
"type": [
Expand Down
11 changes: 11 additions & 0 deletions pkg/data/schemas/json/test-chainsaw-v1alpha1.json
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,17 @@
"description": "NamespaceTemplate defines a template to create the test namespace.",
"x-kubernetes-preserve-unknown-fields": true
},
"namespaceTemplateCompiler": {
"description": "NamespaceTemplateCompiler defines the default compiler to use when evaluating expressions.",
"type": [
"string",
"null"
],
"enum": [
"jp",
"cel"
]
},
"scenarios": {
"description": "Scenarios defines test scenarios.",
"type": [
Expand Down
10 changes: 6 additions & 4 deletions pkg/runner/processors/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
"github.com/kyverno/chainsaw/pkg/cleanup/cleaner"
"github.com/kyverno/chainsaw/pkg/client"
"github.com/kyverno/chainsaw/pkg/engine"
"github.com/kyverno/kyverno-json/pkg/core/compilers"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
)

type namespaceData struct {
name string
template *v1alpha1.Projection
cleaner cleaner.CleanerCollector
name string
compilers compilers.Compilers
template *v1alpha1.Projection
cleaner cleaner.CleanerCollector
}

type contextData struct {
Expand All @@ -40,7 +42,7 @@ func setupContextData(ctx context.Context, tc engine.Context, data contextData)
}
var ns *corev1.Namespace
if data.namespace != nil {
if namespace, err := buildNamespace(ctx, data.namespace.name, data.namespace.template, tc.Bindings()); err != nil {
if namespace, err := buildNamespace(ctx, data.namespace.compilers, data.namespace.name, data.namespace.template, tc.Bindings()); err != nil {
return tc, nil, err
} else if _, clusterClient, err := tc.CurrentClusterClient(); err != nil {
return tc, nil, err
Expand Down
5 changes: 3 additions & 2 deletions pkg/runner/processors/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"github.com/kyverno/chainsaw/pkg/engine/bindings"
"github.com/kyverno/chainsaw/pkg/engine/templating"
"github.com/kyverno/chainsaw/pkg/utils/kube"
"github.com/kyverno/kyverno-json/pkg/core/compilers"
"github.com/kyverno/pkg/ext/resource/convert"
corev1 "k8s.io/api/core/v1"
)

func buildNamespace(ctx context.Context, name string, template *v1alpha1.Projection, tc apis.Bindings) (*corev1.Namespace, error) {
func buildNamespace(ctx context.Context, compilers compilers.Compilers, name string, template *v1alpha1.Projection, tc apis.Bindings) (*corev1.Namespace, error) {
namespace := kube.Namespace(name)
if template == nil {
return &namespace, nil
Expand All @@ -22,7 +23,7 @@ func buildNamespace(ctx context.Context, name string, template *v1alpha1.Project
}
object := kube.ToUnstructured(&namespace)
tc = bindings.RegisterBinding(ctx, tc, "namespace", object.GetName())
merged, err := templating.TemplateAndMerge(ctx, apis.DefaultCompilers, object, tc, *template)
merged, err := templating.TemplateAndMerge(ctx, compilers, object, tc, *template)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading