Skip to content

Commit 79565bd

Browse files
authored
Convert kubeval to kubeconform in golang (#1181)
* Convert kubeval to kubeconform in golang Signed-off-by: Fiachra Corcoran <[email protected]> * Reduce docker image size Signed-off-by: Fiachra Corcoran <[email protected]> * Clean up k8s schema inclusion Signed-off-by: Fiachra Corcoran <[email protected]> * Update details of schemas Signed-off-by: Fiachra Corcoran <[email protected]> * Remove redundant logging Signed-off-by: Fiachra Corcoran <[email protected]> * Retrigger netlify Signed-off-by: Fiachra Corcoran <[email protected]> * Update to trigger netlify deploy Signed-off-by: Fiachra Corcoran <[email protected]> --------- Signed-off-by: Fiachra Corcoran <[email protected]>
1 parent 2cf7583 commit 79565bd

File tree

564 files changed

+442899
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

564 files changed

+442899
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ documentation/resources
1515
*.bak
1616

1717
scripts/patch_reader/patch_reader
18-
shell_files.out
18+
shell_files.out
19+
!jsonschema-k8s.tar.gz
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
exitCode: 1
2+
skip: false
3+
testType: eval
4+
image: ghcr.io/kptdev/krm-functions-catalog/krm-fn-contrib/kubeconform:latest
5+
args:
6+
strict: 'true'
7+
skip_kinds: MyCustom,MyOtherCustom
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: kpt.dev/v1
2+
kind: FunctionResultList
3+
metadata:
4+
name: fnresults
5+
exitCode: 1
6+
items:
7+
- image: ghcr.io/kptdev/krm-functions-catalog/krm-fn-contrib/kubeconform:latest
8+
stderr: 'failed to evaluate function: error: function failure'
9+
exitCode: 1
10+
results:
11+
- message: got string, want null or integer
12+
severity: error
13+
resourceRef:
14+
apiVersion: v1
15+
kind: ReplicationController
16+
name: bob
17+
field:
18+
path: spec.replicas
19+
file:
20+
path: app.yaml
21+
- message: additional properties 'templates' not allowed
22+
severity: error
23+
resourceRef:
24+
apiVersion: v1
25+
kind: ReplicationController
26+
name: bob
27+
field:
28+
path: spec
29+
file:
30+
path: app.yaml
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.expected
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# kubeconform: Imperative Example
2+
3+
### Overview
4+
5+
This example demonstrates how to imperatively invoke [`kubeconform`] function to
6+
validate KRM resources.
7+
8+
### Fetch the example package
9+
10+
Get the example package by running the following commands:
11+
12+
```shell
13+
$ kpt pkg get https://github.com/kptdev/krm-functions-catalog.git/contrib/examples/kubeconform-imperative
14+
```
15+
16+
We have a `ReplicationController` in `app.yaml` that has 2 schema violations:
17+
- `.spec.templates` is unknown, since it should be `.spec.template`.
18+
- `spec.replicas` must not be a string.
19+
20+
### Function invocation
21+
22+
Try it out by running the following command:
23+
24+
```shell
25+
# We set `strict=true` to disallow unknown field and `skip_kinds=MyCustom,MyOtherCustom` to skip 2 kinds that we don't have schemas.
26+
$ kpt fn eval kubeconform-imperative --image ghcr.io/kptdev/krm-functions-catalog/krm-fn-contrib/kubeconform:latest --results-dir /tmp -- strict=true skip_kinds=MyCustom,MyOtherCustom
27+
```
28+
29+
The key-value pair(s) provided after `--` will be converted to `ConfigMap` by
30+
kpt and used as the function configuration.
31+
32+
### Expected Results
33+
34+
Let's look at the structured results in `/tmp/results.yaml`:
35+
36+
```yaml
37+
apiVersion: kpt.dev/v1
38+
kind: FunctionResultList
39+
metadata:
40+
name: fnresults
41+
exitCode: 1
42+
items:
43+
- image: ghcr.io/kptdev/krm-functions-catalog/krm-fn-contrib/kubeconform:latest
44+
stderr: 'failed to evaluate function: error: function failure'
45+
exitCode: 1
46+
results:
47+
- message: got string, want null or integer
48+
severity: error
49+
resourceRef:
50+
apiVersion: v1
51+
kind: ReplicationController
52+
name: bob
53+
field:
54+
path: spec.replicas
55+
file:
56+
path: app.yaml
57+
- message: additional properties 'templates' not allowed
58+
severity: error
59+
resourceRef:
60+
apiVersion: v1
61+
kind: ReplicationController
62+
name: bob
63+
field:
64+
path: spec
65+
file:
66+
path: app.yaml
67+
```
68+
69+
To fix them:
70+
71+
- replace the value of `spec.replicas` with an integer
72+
- change `templates` to `template`
73+
74+
Rerun the command, and it should succeed.
75+
76+
[`kubeconform`]:https://github.com/yannh/kubeconform
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# From https://github.com/yannh/kubeconform/blob/master/fixtures/invalid.yaml
2+
apiVersion: v1
3+
kind: ReplicationController
4+
metadata:
5+
name: bob
6+
spec:
7+
# If replicas is not an integer, kubeval will report error.
8+
replicas: asdf # Replace "asdf" with an integer to fix the error.
9+
selector:
10+
app: nginx
11+
templates:
12+
metadata:
13+
name: nginx
14+
labels:
15+
app: nginx
16+
spec:
17+
containers:
18+
- name: nginx
19+
image: nginx
20+
ports:
21+
- containerPort: 80
22+
---
23+
apiVersion: example.com/v1
24+
kind: MyCustom
25+
metadata:
26+
name: alice
27+
spec: {}
28+
---
29+
apiVersion: example.com/v1
30+
kind: MyOtherCustom
31+
metadata:
32+
name: charlie
33+
spec: {}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exitCode: 1
2+
skip: false
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
kpt fn eval -i ghcr.io/kptdev/krm-functions-catalog/krm-fn-contrib/kubeconform:latest --image-pull-policy never \
6+
--results-dir="$(pwd)/../results" \
7+
--mount type=bind,src="$(pwd)/jsonschema",dst=/schema-dir/master-standalone \
8+
-- schema_location=file:///schema-dir
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: kpt.dev/v1
2+
kind: FunctionResultList
3+
metadata:
4+
name: fnresults
5+
exitCode: 1
6+
items:
7+
- image: ghcr.io/kptdev/krm-functions-catalog/krm-fn-contrib/kubeconform:latest
8+
stderr: 'failed to evaluate function: error: function failure'
9+
exitCode: 1
10+
results:
11+
- message: got string, want null or integer
12+
severity: error
13+
resourceRef:
14+
apiVersion: v1
15+
kind: ReplicationController
16+
name: bob
17+
field:
18+
path: spec.replicas
19+
file:
20+
path: replicationcontroller.yaml
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.expected
2+
jsonschema

0 commit comments

Comments
 (0)