Skip to content

Commit 4649c94

Browse files
feat: add compiler to the namespace template api (#2035) (#2036)
Signed-off-by: Charles-Edouard Brétéché <[email protected]> Co-authored-by: Charles-Edouard Brétéché <[email protected]>
1 parent 80afa51 commit 4649c94

25 files changed

+180
-15
lines changed

.crds/chainsaw.kyverno.io_configurations.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,13 @@ spec:
860860
description: NamespaceTemplate defines a template to create the test
861861
namespace.
862862
x-kubernetes-preserve-unknown-fields: true
863+
namespaceTemplateCompiler:
864+
description: NamespaceTemplateCompiler defines the default compiler
865+
to use when evaluating expressions.
866+
enum:
867+
- jp
868+
- cel
869+
type: string
863870
parallel:
864871
description: The maximum number of tests to run at once.
865872
format: int
@@ -1833,6 +1840,13 @@ spec:
18331840
description: Namespace contains properties for the namespace to use
18341841
for tests.
18351842
properties:
1843+
compiler:
1844+
description: Compiler defines the default compiler to use when
1845+
evaluating expressions.
1846+
enum:
1847+
- jp
1848+
- cel
1849+
type: string
18361850
name:
18371851
description: |-
18381852
Name defines the namespace to use for tests.

.crds/chainsaw.kyverno.io_tests.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,13 @@ spec:
881881
description: NamespaceTemplate defines a template to create the test
882882
namespace.
883883
x-kubernetes-preserve-unknown-fields: true
884+
namespaceTemplateCompiler:
885+
description: NamespaceTemplateCompiler defines the default compiler
886+
to use when evaluating expressions.
887+
enum:
888+
- jp
889+
- cel
890+
type: string
884891
scenarios:
885892
description: Scenarios defines test scenarios.
886893
items:

.schemas/json/configuration-chainsaw-v1alpha1.json

+11
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,17 @@
16981698
"description": "NamespaceTemplate defines a template to create the test namespace.",
16991699
"x-kubernetes-preserve-unknown-fields": true
17001700
},
1701+
"namespaceTemplateCompiler": {
1702+
"description": "NamespaceTemplateCompiler defines the default compiler to use when evaluating expressions.",
1703+
"type": [
1704+
"string",
1705+
"null"
1706+
],
1707+
"enum": [
1708+
"jp",
1709+
"cel"
1710+
]
1711+
},
17011712
"parallel": {
17021713
"description": "The maximum number of tests to run at once.",
17031714
"type": [

.schemas/json/configuration-chainsaw-v1alpha2.json

+11
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,17 @@
17831783
],
17841784
"default": {},
17851785
"properties": {
1786+
"compiler": {
1787+
"description": "Compiler defines the default compiler to use when evaluating expressions.",
1788+
"type": [
1789+
"string",
1790+
"null"
1791+
],
1792+
"enum": [
1793+
"jp",
1794+
"cel"
1795+
]
1796+
},
17861797
"name": {
17871798
"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.",
17881799
"type": [

.schemas/json/test-chainsaw-v1alpha1.json

+11
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,17 @@
17411741
"description": "NamespaceTemplate defines a template to create the test namespace.",
17421742
"x-kubernetes-preserve-unknown-fields": true
17431743
},
1744+
"namespaceTemplateCompiler": {
1745+
"description": "NamespaceTemplateCompiler defines the default compiler to use when evaluating expressions.",
1746+
"type": [
1747+
"string",
1748+
"null"
1749+
],
1750+
"enum": [
1751+
"jp",
1752+
"cel"
1753+
]
1754+
},
17441755
"scenarios": {
17451756
"description": "Scenarios defines test scenarios.",
17461757
"type": [

pkg/apis/conversion/configuration.go

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func Convert_v1alpha2_ConfigurationSpec_To_v1alpha1_ConfigurationSpec(in *v1alph
2222
out.ForceTerminationGracePeriod = in.Execution.ForceTerminationGracePeriod
2323
out.Namespace = in.Namespace.Name
2424
out.NamespaceTemplate = in.Namespace.Template
25+
out.NamespaceTemplateCompiler = in.Namespace.Compiler
2526
if in := in.Report; in != nil {
2627
out.ReportFormat = v1alpha1.ReportFormatType(in.Format)
2728
out.ReportPath = in.Path
@@ -58,6 +59,7 @@ func Convert_v1alpha1_ConfigurationSpec_To_v1alpha2_ConfigurationSpec(in *v1alph
5859
}
5960
out.Namespace = v1alpha2.NamespaceOptions{
6061
Name: in.Namespace,
62+
Compiler: in.NamespaceTemplateCompiler,
6163
Template: in.NamespaceTemplate,
6264
}
6365
out.Report = &v1alpha2.ReportOptions{

pkg/apis/v1alpha1/configuration.go

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ type ConfigurationSpec struct {
7575
// +optional
7676
Namespace string `json:"namespace,omitempty"`
7777

78+
// NamespaceTemplateCompiler defines the default compiler to use when evaluating expressions.
79+
// +optional
80+
NamespaceTemplateCompiler *Compiler `json:"namespaceTemplateCompiler,omitempty"`
81+
7882
// NamespaceTemplate defines a template to create the test namespace.
7983
// +optional
8084
NamespaceTemplate *Projection `json:"namespaceTemplate,omitempty"`

pkg/apis/v1alpha1/test.go

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ type TestSpec struct {
6868
// +optional
6969
NamespaceTemplate *Projection `json:"namespaceTemplate,omitempty"`
7070

71+
// NamespaceTemplateCompiler defines the default compiler to use when evaluating expressions.
72+
// +optional
73+
NamespaceTemplateCompiler *Compiler `json:"namespaceTemplateCompiler,omitempty"`
74+
7175
// Scenarios defines test scenarios.
7276
// +optional
7377
Scenarios []Scenario `json:"scenarios,omitempty"`

pkg/apis/v1alpha1/types.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ var (
2020
NewMatch = v1alpha1.NewAssertionTree
2121
)
2222

23+
type Compiler = v1alpha1.Compiler
24+
2325
// Binding represents a key/value set as a binding in an executing test.
2426
type Binding struct {
2527
// Name the name of the binding.
@@ -29,7 +31,7 @@ type Binding struct {
2931

3032
// Compiler defines the default compiler to use when evaluating expressions.
3133
// +optional
32-
Compiler *v1alpha1.Compiler `json:"compiler,omitempty"`
34+
Compiler *Compiler `json:"compiler,omitempty"`
3335

3436
// Value value of the binding.
3537
Value Projection `json:"value"`

pkg/apis/v1alpha1/zz_generated.deepcopy.go

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/v1alpha2/options.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package v1alpha2
22

33
import (
44
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
5-
_ "github.com/kyverno/kyverno-json/pkg/apis/policy/v1alpha1"
5+
kjson "github.com/kyverno/kyverno-json/pkg/apis/policy/v1alpha1"
66
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
77
)
88

@@ -87,6 +87,10 @@ type NamespaceOptions struct {
8787
// +optional
8888
Name string `json:"name,omitempty"`
8989

90+
// Compiler defines the default compiler to use when evaluating expressions.
91+
// +optional
92+
Compiler *kjson.Compiler `json:"compiler,omitempty"`
93+
9094
// Template defines a template to create the test namespace.
9195
// +optional
9296
Template *Projection `json:"template,omitempty"`

pkg/apis/v1alpha2/zz_generated.deepcopy.go

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/data/crds/chainsaw.kyverno.io_configurations.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,13 @@ spec:
860860
description: NamespaceTemplate defines a template to create the test
861861
namespace.
862862
x-kubernetes-preserve-unknown-fields: true
863+
namespaceTemplateCompiler:
864+
description: NamespaceTemplateCompiler defines the default compiler
865+
to use when evaluating expressions.
866+
enum:
867+
- jp
868+
- cel
869+
type: string
863870
parallel:
864871
description: The maximum number of tests to run at once.
865872
format: int
@@ -1833,6 +1840,13 @@ spec:
18331840
description: Namespace contains properties for the namespace to use
18341841
for tests.
18351842
properties:
1843+
compiler:
1844+
description: Compiler defines the default compiler to use when
1845+
evaluating expressions.
1846+
enum:
1847+
- jp
1848+
- cel
1849+
type: string
18361850
name:
18371851
description: |-
18381852
Name defines the namespace to use for tests.

pkg/data/crds/chainsaw.kyverno.io_tests.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,13 @@ spec:
881881
description: NamespaceTemplate defines a template to create the test
882882
namespace.
883883
x-kubernetes-preserve-unknown-fields: true
884+
namespaceTemplateCompiler:
885+
description: NamespaceTemplateCompiler defines the default compiler
886+
to use when evaluating expressions.
887+
enum:
888+
- jp
889+
- cel
890+
type: string
884891
scenarios:
885892
description: Scenarios defines test scenarios.
886893
items:

pkg/data/schemas/json/configuration-chainsaw-v1alpha1.json

+11
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,17 @@
16981698
"description": "NamespaceTemplate defines a template to create the test namespace.",
16991699
"x-kubernetes-preserve-unknown-fields": true
17001700
},
1701+
"namespaceTemplateCompiler": {
1702+
"description": "NamespaceTemplateCompiler defines the default compiler to use when evaluating expressions.",
1703+
"type": [
1704+
"string",
1705+
"null"
1706+
],
1707+
"enum": [
1708+
"jp",
1709+
"cel"
1710+
]
1711+
},
17011712
"parallel": {
17021713
"description": "The maximum number of tests to run at once.",
17031714
"type": [

pkg/data/schemas/json/configuration-chainsaw-v1alpha2.json

+11
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,17 @@
17831783
],
17841784
"default": {},
17851785
"properties": {
1786+
"compiler": {
1787+
"description": "Compiler defines the default compiler to use when evaluating expressions.",
1788+
"type": [
1789+
"string",
1790+
"null"
1791+
],
1792+
"enum": [
1793+
"jp",
1794+
"cel"
1795+
]
1796+
},
17861797
"name": {
17871798
"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.",
17881799
"type": [

pkg/data/schemas/json/test-chainsaw-v1alpha1.json

+11
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,17 @@
17411741
"description": "NamespaceTemplate defines a template to create the test namespace.",
17421742
"x-kubernetes-preserve-unknown-fields": true
17431743
},
1744+
"namespaceTemplateCompiler": {
1745+
"description": "NamespaceTemplateCompiler defines the default compiler to use when evaluating expressions.",
1746+
"type": [
1747+
"string",
1748+
"null"
1749+
],
1750+
"enum": [
1751+
"jp",
1752+
"cel"
1753+
]
1754+
},
17441755
"scenarios": {
17451756
"description": "Scenarios defines test scenarios.",
17461757
"type": [

pkg/runner/processors/context.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import (
77
"github.com/kyverno/chainsaw/pkg/cleanup/cleaner"
88
"github.com/kyverno/chainsaw/pkg/client"
99
"github.com/kyverno/chainsaw/pkg/engine"
10+
"github.com/kyverno/kyverno-json/pkg/core/compilers"
1011
corev1 "k8s.io/api/core/v1"
1112
"k8s.io/apimachinery/pkg/api/errors"
1213
)
1314

1415
type namespaceData struct {
15-
name string
16-
template *v1alpha1.Projection
17-
cleaner cleaner.CleanerCollector
16+
name string
17+
compilers compilers.Compilers
18+
template *v1alpha1.Projection
19+
cleaner cleaner.CleanerCollector
1820
}
1921

2022
type contextData struct {
@@ -40,7 +42,7 @@ func setupContextData(ctx context.Context, tc engine.Context, data contextData)
4042
}
4143
var ns *corev1.Namespace
4244
if data.namespace != nil {
43-
if namespace, err := buildNamespace(ctx, data.namespace.name, data.namespace.template, tc.Bindings()); err != nil {
45+
if namespace, err := buildNamespace(ctx, data.namespace.compilers, data.namespace.name, data.namespace.template, tc.Bindings()); err != nil {
4446
return tc, nil, err
4547
} else if _, clusterClient, err := tc.CurrentClusterClient(); err != nil {
4648
return tc, nil, err

pkg/runner/processors/namespace.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88
"github.com/kyverno/chainsaw/pkg/engine/bindings"
99
"github.com/kyverno/chainsaw/pkg/engine/templating"
1010
"github.com/kyverno/chainsaw/pkg/utils/kube"
11+
"github.com/kyverno/kyverno-json/pkg/core/compilers"
1112
"github.com/kyverno/pkg/ext/resource/convert"
1213
corev1 "k8s.io/api/core/v1"
1314
)
1415

15-
func buildNamespace(ctx context.Context, name string, template *v1alpha1.Projection, tc apis.Bindings) (*corev1.Namespace, error) {
16+
func buildNamespace(ctx context.Context, compilers compilers.Compilers, name string, template *v1alpha1.Projection, tc apis.Bindings) (*corev1.Namespace, error) {
1617
namespace := kube.Namespace(name)
1718
if template == nil {
1819
return &namespace, nil
@@ -22,7 +23,7 @@ func buildNamespace(ctx context.Context, name string, template *v1alpha1.Project
2223
}
2324
object := kube.ToUnstructured(&namespace)
2425
tc = bindings.RegisterBinding(ctx, tc, "namespace", object.GetName())
25-
merged, err := templating.TemplateAndMerge(ctx, apis.DefaultCompilers, object, tc, *template)
26+
merged, err := templating.TemplateAndMerge(ctx, compilers, object, tc, *template)
2627
if err != nil {
2728
return nil, err
2829
}

0 commit comments

Comments
 (0)