Skip to content

Commit a418f8e

Browse files
feat: add server side validation support (#1894) (#1895)
Signed-off-by: Charles-Edouard Brétéché <[email protected]> Co-authored-by: Charles-Edouard Brétéché <[email protected]>
1 parent 923674f commit a418f8e

File tree

6 files changed

+51
-13
lines changed

6 files changed

+51
-13
lines changed

pkg/client/simple/client.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ package simple
22

33
import (
44
"github.com/kyverno/chainsaw/pkg/client"
5+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
56
"k8s.io/client-go/rest"
67
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
78
)
89

910
func New(cfg *rest.Config) (client.Client, error) {
1011
var opts ctrlclient.Options
11-
return ctrlclient.New(cfg, opts)
12+
client, err := ctrlclient.New(cfg, opts)
13+
if err != nil {
14+
return nil, err
15+
}
16+
return ctrlclient.WithFieldValidation(client, metav1.FieldValidationStrict), nil
1217
}

testdata/e2e/examples/CATALOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [delete](delete/README.md)
1313
- [deployment](deployment/README.md)
1414
- [dynamic-clusters](dynamic-clusters/README.md)
15+
- [field-validation](field-validation/README.md)
1516
- [finally](finally/README.md)
1617
- [inline](inline/README.md)
1718
- [k8s-server-version](k8s-server-version/README.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Test: `field-validation`
2+
3+
*No description*
4+
5+
## Steps
6+
7+
| # | Name | Bindings | Try | Catch | Finally | Cleanup |
8+
|:-:|---|:-:|:-:|:-:|:-:|
9+
| 1 | [step-1](#step-step-1) | 0 | 1 | 0 | 0 | 0 |
10+
11+
### Step: `step-1`
12+
13+
*No description*
14+
15+
#### Try
16+
17+
| # | Operation | Bindings | Outputs | Description |
18+
|:-:|---|:-:|:-:|---|
19+
| 1 | `apply` | 0 | 0 | *No description* |
20+
21+
---
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# yaml-language-server: $schema=../../../../.schemas/json/test-chainsaw-v1alpha1.json
2+
apiVersion: chainsaw.kyverno.io/v1alpha1
3+
kind: Test
4+
metadata:
5+
name: field-validation
6+
spec:
7+
steps:
8+
- try:
9+
- apply:
10+
resource:
11+
apiVersion: v1
12+
kind: ConfigMap
13+
metadata:
14+
name: chainsaw-quick-start
15+
data:
16+
foo: bar
17+
bar: baz
18+
expect:
19+
- check:
20+
($error): |-
21+
ConfigMap in version "v1" cannot be handled as a ConfigMap: strict decoding error: unknown field "bar"

testdata/e2e/examples/quick-start/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| # | Name | Bindings | Try | Catch | Finally | Cleanup |
88
|:-:|---|:-:|:-:|:-:|:-:|
9-
| 1 | [step-1](#step-step-1) | 0 | 3 | 0 | 0 | 0 |
9+
| 1 | [step-1](#step-step-1) | 0 | 2 | 0 | 0 | 0 |
1010

1111
### Step: `step-1`
1212

@@ -18,7 +18,6 @@
1818
|:-:|---|:-:|:-:|---|
1919
| 1 | `apply` | 0 | 0 | *No description* |
2020
| 2 | `assert` | 0 | 0 | *No description* |
21-
| 3 | `error` | 0 | 0 | *No description* |
2221

2322
---
2423

testdata/e2e/examples/quick-start/chainsaw-test.yaml

-10
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,3 @@ spec:
1414
- assert:
1515
# file is relative to the test folder
1616
file: configmap.yaml
17-
# this make sure a non-existent field cannot be compared to an empty object
18-
- error:
19-
resource:
20-
apiVersion: v1
21-
kind: ConfigMap
22-
metadata:
23-
name: chainsaw-quick-start
24-
status:
25-
vulnerabilitySummary:
26-
severityCount: {}

0 commit comments

Comments
 (0)