Skip to content

Commit

Permalink
feat: add server side validation support (#1894)
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 authored Aug 27, 2024
1 parent aa9468f commit 5abe84c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 13 deletions.
7 changes: 6 additions & 1 deletion pkg/client/simple/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package simple

import (
"github.com/kyverno/chainsaw/pkg/client"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
)

func New(cfg *rest.Config) (client.Client, error) {
var opts ctrlclient.Options
return ctrlclient.New(cfg, opts)
client, err := ctrlclient.New(cfg, opts)
if err != nil {
return nil, err
}
return ctrlclient.WithFieldValidation(client, metav1.FieldValidationStrict), nil
}
1 change: 1 addition & 0 deletions testdata/e2e/examples/CATALOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [delete](delete/README.md)
- [deployment](deployment/README.md)
- [dynamic-clusters](dynamic-clusters/README.md)
- [field-validation](field-validation/README.md)
- [finally](finally/README.md)
- [inline](inline/README.md)
- [k8s-server-version](k8s-server-version/README.md)
Expand Down
22 changes: 22 additions & 0 deletions testdata/e2e/examples/field-validation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Test: `field-validation`

*No description*

## Steps

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

### Step: `step-1`

*No description*

#### Try

| # | Operation | Bindings | Outputs | Description |
|:-:|---|:-:|:-:|---|
| 1 | `apply` | 0 | 0 | *No description* |

---

21 changes: 21 additions & 0 deletions testdata/e2e/examples/field-validation/chainsaw-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# yaml-language-server: $schema=../../../../.schemas/json/test-chainsaw-v1alpha1.json
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: field-validation
spec:
steps:
- try:
- apply:
resource:
apiVersion: v1
kind: ConfigMap
metadata:
name: chainsaw-quick-start
data:
foo: bar
bar: baz
expect:
- check:
($error): |-
ConfigMap in version "v1" cannot be handled as a ConfigMap: strict decoding error: unknown field "bar"
3 changes: 1 addition & 2 deletions testdata/e2e/examples/quick-start/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

### Step: `step-1`

Expand All @@ -18,7 +18,6 @@
|:-:|---|:-:|:-:|---|
| 1 | `apply` | 0 | 0 | *No description* |
| 2 | `assert` | 0 | 0 | *No description* |
| 3 | `error` | 0 | 0 | *No description* |

---

10 changes: 0 additions & 10 deletions testdata/e2e/examples/quick-start/chainsaw-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,3 @@ spec:
- assert:
# file is relative to the test folder
file: configmap.yaml
# this make sure a non-existent field cannot be compared to an empty object
- error:
resource:
apiVersion: v1
kind: ConfigMap
metadata:
name: chainsaw-quick-start
status:
vulnerabilitySummary:
severityCount: {}

0 comments on commit 5abe84c

Please sign in to comment.