Skip to content

Commit 861aeb8

Browse files
janiszclaude
andauthored
Add fallback decoder for unknown resources to handle CRDs (#1037)
Signed-off-by: Tomasz Janiszewski <tomek@redhat.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent 7cf1d53 commit 861aeb8

File tree

13 files changed

+342
-39
lines changed

13 files changed

+342
-39
lines changed

docs/generated/checks.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,22 @@ key: owner
590590
```yaml
591591
AllowPrivilegedContainer: true
592592
```
593+
## schema-validation
594+
595+
**Enabled by default**: No
596+
597+
**Description**: Validate Kubernetes resources against their schemas using kubeconform
598+
599+
**Remediation**: Fix the resource to conform to the Kubernetes API schema.
600+
601+
**Template**: [kubeconform](templates.md#kubeconform)
602+
603+
**Parameters**:
604+
605+
```yaml
606+
ignoreMissingSchemas: true
607+
strict: true
608+
```
593609
## sensitive-host-mounts
594610
595611
**Enabled by default**: Yes

e2etests/bats-tests.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,24 @@ get_value_from() {
927927
[[ "${count}" == "1" ]]
928928
}
929929

930+
@test "schema-validation" {
931+
tmp="tests/checks/kubeconform.yml"
932+
cmd="${KUBE_LINTER_BIN} lint --config e2etests/testdata/schema-validation-config.yaml --do-not-auto-add-defaults --format json ${tmp}"
933+
run ${cmd}
934+
935+
print_info "${status}" "${output}" "${cmd}" "${tmp}"
936+
[ "$status" -eq 1 ]
937+
938+
message1=$(get_value_from "${lines[0]}" '.Reports[0].Object.K8sObject.GroupVersionKind.Kind + ": " + .Reports[0].Diagnostic.Message')
939+
message2=$(get_value_from "${lines[0]}" '.Reports[1].Object.K8sObject.GroupVersionKind.Kind + ": " + .Reports[1].Diagnostic.Message')
940+
count=$(get_value_from "${lines[0]}" '.Reports | length')
941+
942+
# Should find 2 validation errors using builtin schema-validation check
943+
[[ "${count}" == "2" ]]
944+
[[ "${message1}" =~ "DaemonSet: resource is not valid:" ]]
945+
[[ "${message2}" =~ "Pod: resource is not valid:" ]]
946+
}
947+
930948
@test "sensitive-host-mounts" {
931949
tmp="tests/checks/sensitive-host-mounts.yml"
932950
cmd="${KUBE_LINTER_BIN} lint --include sensitive-host-mounts --do-not-auto-add-defaults --format json ${tmp}"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
checks:
2+
addAllBuiltIn: false
3+
include:
4+
- "schema-validation"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: "schema-validation"
2+
description: "Validate Kubernetes resources against their schemas using kubeconform"
3+
remediation: "Fix the resource to conform to the Kubernetes API schema."
4+
scope:
5+
objectKinds:
6+
- Any
7+
template: "kubeconform"
8+
params:
9+
strict: true
10+
ignoreMissingSchemas: true

pkg/command/lint/command.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func Command() *cobra.Command {
156156
c.Flags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging")
157157
c.Flags().Var(format, "format", format.Usage())
158158
c.Flags().BoolVarP(&errorOnInvalidResource, "fail-on-invalid-resource", "", false, "Error out when we have an invalid resource")
159+
_ = c.Flags().MarkDeprecated("fail-on-invalid-resource", "Use 'schema-validation' builtin check or kubeconform template for better schema validation.")
159160

160161
config.AddFlags(c, v)
161162
return c

pkg/command/lint/command_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ func TestCommand_InvalidResources(t *testing.T) {
1616
failure bool
1717
output string
1818
}{
19-
{name: "InvalidPodResource", cmd: createLintCommand("./testdata/invalid-pod-resources.yaml", "--fail-on-invalid-resource"), failure: true},
20-
{name: "InvalidPVCResource", cmd: createLintCommand("./testdata/invalid-pvc-resources.yaml", "--fail-on-invalid-resource"), failure: true},
19+
{name: "InvalidYAML", cmd: createLintCommand("./testdata/invalid.yaml", "--fail-on-invalid-resource"), failure: true},
2120
{name: "NonexistentFile", cmd: createLintCommand("./testdata/foo-bar.yaml", "--fail-on-invalid-resource"), failure: true},
2221
{name: "ValidPod", cmd: createLintCommand("./testdata/valid-pod.yaml", "--fail-on-invalid-resource"), failure: false},
2322
}

pkg/command/lint/testdata/invalid-pod-resources.yaml

Lines changed: 0 additions & 24 deletions
This file was deleted.

pkg/command/lint/testdata/invalid-pvc-resources.yaml

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
this is malformed YAML that should fail to parse: {
2+
invalid: unclosed bracket

pkg/lintcontext/create_contexts_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ func TestCreateContextsWithIgnorePaths(t *testing.T) {
3131
"../../.pre-commit-hooks*",
3232
"../../dist/**/*",
3333
"../../pkg/**/*",
34+
"../../demo/**",
35+
"../../stackrox-kube-linter-bug-example/**",
36+
"../../tests/**/*",
37+
"../../cmd/**/*",
38+
"../../docs/**/*",
39+
"../../internal/**/*",
3440
"/**/*/checks/**/*",
3541
"/**/*/test_helper/**/*",
3642
"/**/*/testdata/**/*",

0 commit comments

Comments
 (0)