Skip to content

Commit

Permalink
feat: add bindings support to step templates (#1985) (#1993)
Browse files Browse the repository at this point in the history
* feat: add bindings support to step templates



* fix comment



---------

Signed-off-by: Charles-Edouard Brétéché <[email protected]>
Co-authored-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
gcp-cherry-pick-bot[bot] and eddycharly authored Sep 16, 2024
1 parent 41029f5 commit c6440e1
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 9 deletions.
23 changes: 23 additions & 0 deletions .crds/chainsaw.kyverno.io_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4625,6 +4625,29 @@ spec:
template:
description: Template references a step template.
type: string
with:
default: {}
description: With defines arguments passed to the step template.
properties:
bindings:
description: Bindings defines additional binding key/values.
items:
description: Binding represents a key/value set as
a binding in an executing test.
properties:
name:
description: Name the name of the binding.
pattern: ^(?:\w+|\(.+\))$
type: string
value:
description: Value value of the binding.
x-kubernetes-preserve-unknown-fields: true
required:
- name
- value
type: object
type: array
type: object
required:
- template
type: object
Expand Down
41 changes: 41 additions & 0 deletions .schemas/json/test-chainsaw-v1alpha1.json
Original file line number Diff line number Diff line change
Expand Up @@ -9192,6 +9192,47 @@
"template": {
"description": "Template references a step template.",
"type": "string"
},
"with": {
"description": "With defines arguments passed to the step template.",
"type": [
"object",
"null"
],
"default": {},
"properties": {
"bindings": {
"description": "Bindings defines additional binding key/values.",
"type": [
"array",
"null"
],
"items": {
"description": "Binding represents a key/value set as a binding in an executing test.",
"type": [
"object",
"null"
],
"required": [
"name",
"value"
],
"properties": {
"name": {
"description": "Name the name of the binding.",
"type": "string",
"pattern": "^(?:\\w+|\\(.+\\))$"
},
"value": {
"description": "Value value of the binding.",
"x-kubernetes-preserve-unknown-fields": true
}
},
"additionalProperties": false
}
}
},
"additionalProperties": false
}
},
"additionalProperties": false
Expand Down
12 changes: 12 additions & 0 deletions pkg/apis/v1alpha1/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ type TestStep struct {
type Use struct {
// Template references a step template.
Template string `json:"template"`

// With defines arguments passed to the step template.
// +optional
// +kubebuilder:default:={}
With With `json:"with"`
}

// With defines arguments passed to step templates.
type With struct {
// Bindings defines additional binding key/values.
// +optional
Bindings []Binding `json:"bindings,omitempty"`
}

// TestStepSpec defines the desired state and behavior for each test step.
Expand Down
26 changes: 25 additions & 1 deletion pkg/apis/v1alpha1/zz_generated.deepcopy.go

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

23 changes: 23 additions & 0 deletions pkg/data/crds/chainsaw.kyverno.io_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4625,6 +4625,29 @@ spec:
template:
description: Template references a step template.
type: string
with:
default: {}
description: With defines arguments passed to the step template.
properties:
bindings:
description: Bindings defines additional binding key/values.
items:
description: Binding represents a key/value set as
a binding in an executing test.
properties:
name:
description: Name the name of the binding.
pattern: ^(?:\w+|\(.+\))$
type: string
value:
description: Value value of the binding.
x-kubernetes-preserve-unknown-fields: true
required:
- name
- value
type: object
type: array
type: object
required:
- template
type: object
Expand Down
41 changes: 41 additions & 0 deletions pkg/data/schemas/json/test-chainsaw-v1alpha1.json
Original file line number Diff line number Diff line change
Expand Up @@ -9192,6 +9192,47 @@
"template": {
"description": "Template references a step template.",
"type": "string"
},
"with": {
"description": "With defines arguments passed to the step template.",
"type": [
"object",
"null"
],
"default": {},
"properties": {
"bindings": {
"description": "Bindings defines additional binding key/values.",
"type": [
"array",
"null"
],
"items": {
"description": "Binding represents a key/value set as a binding in an executing test.",
"type": [
"object",
"null"
],
"required": [
"name",
"value"
],
"properties": {
"name": {
"description": "Name the name of the binding.",
"type": "string",
"pattern": "^(?:\\w+|\\(.+\\))$"
},
"value": {
"description": "Value value of the binding.",
"x-kubernetes-preserve-unknown-fields": true
}
},
"additionalProperties": false
}
}
},
"additionalProperties": false
}
},
"additionalProperties": false
Expand Down
12 changes: 7 additions & 5 deletions pkg/discovery/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ func LoadTest(fileName string, path string, remarshal bool) ([]Test, error) {
if len(steptpl) != 1 {
return nil, errors.New("step template not found or multiple templates exist")
}
template := steptpl[0]
step.Bindings = append(step.Bindings, step.Use.With.Bindings...)
step.Bindings = append(step.Bindings, template.Spec.Bindings...)
step.Try = append(step.Try, template.Spec.Try...)
step.Catch = append(step.Catch, template.Spec.Catch...)
step.Finally = append(step.Finally, template.Spec.Finally...)
step.Cleanup = append(step.Cleanup, template.Spec.Cleanup...)
step.Use = nil
step.Bindings = append(step.Bindings, steptpl[0].Spec.Bindings...)
step.Try = append(step.Try, steptpl[0].Spec.Try...)
step.Catch = append(step.Catch, steptpl[0].Spec.Catch...)
step.Finally = append(step.Finally, steptpl[0].Spec.Finally...)
step.Cleanup = append(step.Cleanup, steptpl[0].Spec.Cleanup...)
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion testdata/e2e/examples/step-template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@

| # | Name | Bindings | Try | Catch | Finally | Cleanup |
|:-:|---|:-:|:-:|:-:|:-:|
| 1 | [step-1](#step-step-1) | 0 | 2 | 0 | 0 | 0 |
| 1 | [step-1](#step-step-1) | 1 | 2 | 0 | 0 | 0 |

### Step: `step-1`

*No description*

#### Bindings

| # | Name | Value |
|:-:|---|---|
| 1 | `file` | "configmap.yaml" |

#### Try

| # | Operation | Bindings | Outputs | Description |
Expand Down
4 changes: 4 additions & 0 deletions testdata/e2e/examples/step-template/chainsaw-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ spec:
steps:
- use:
template: step-template.yaml
with:
bindings:
- name: file
value: configmap.yaml
4 changes: 2 additions & 2 deletions testdata/e2e/examples/step-template/step-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ spec:
# first operation: create the config map
- apply:
# file is relative to the test folder
file: configmap.yaml
file: ($file)
# second operation: verify the config map exists and contains the expected data
- assert:
# file is relative to the test folder
file: configmap.yaml
file: ($file)
15 changes: 15 additions & 0 deletions website/docs/reference/apis/chainsaw.v1alpha1.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ during the testing process.</p>
- [StepTemplateSpec](#chainsaw-kyverno-io-v1alpha1-StepTemplateSpec)
- [TestSpec](#chainsaw-kyverno-io-v1alpha1-TestSpec)
- [TestStepSpec](#chainsaw-kyverno-io-v1alpha1-TestStepSpec)
- [With](#chainsaw-kyverno-io-v1alpha1-With)

<p>Binding represents a key/value set as a binding in an executing test.</p>

Expand Down Expand Up @@ -982,6 +983,7 @@ If a resource does not exist in the cluster it will fail.</p>
| Field | Type | Required | Inline | Description |
|---|---|---|---|---|
| `template` | `string` | :white_check_mark: | | <p>Template references a step template.</p> |
| `with` | [`With`](#chainsaw-kyverno-io-v1alpha1-With) | | | <p>With defines arguments passed to the step template.</p> |

## Wait {#chainsaw-kyverno-io-v1alpha1-Wait}

Expand Down Expand Up @@ -1053,4 +1055,17 @@ If a resource does not exist in the cluster it will fail.</p>
| `path` | [`Expression`](#chainsaw-kyverno-io-v1alpha1-Expression) | :white_check_mark: | | <p>Path defines the json path to wait for, e.g. '{.status.phase}'.</p> |
| `value` | [`Expression`](#chainsaw-kyverno-io-v1alpha1-Expression) | | | <p>Value defines the expected value to wait for, e.g., "Running".</p> |

## With {#chainsaw-kyverno-io-v1alpha1-With}

**Appears in:**

- [Use](#chainsaw-kyverno-io-v1alpha1-Use)

<p>With defines arguments passed to step templates.</p>


| Field | Type | Required | Inline | Description |
|---|---|---|---|---|
| `bindings` | [`[]Binding`](#chainsaw-kyverno-io-v1alpha1-Binding) | | | <p>Bindings defines additional binding key/values.</p> |


0 comments on commit c6440e1

Please sign in to comment.