Skip to content

Commit

Permalink
Force Draft version of JsonSchema (#221)
Browse files Browse the repository at this point in the history
* Force Draft version of JsonSchema
* Add test validating using CRD that misses explicit draft version
  • Loading branch information
yannh committed Jul 16, 2023
1 parent 278385f commit ae67bb4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions acceptance.bats
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,8 @@ resetCacheFolder() {
run xmllint --noout --schema fixtures/junit.xsd output.xml
[ "$status" -eq 0 ]
}

@test "passes when trying to use a CRD that does not have the JSONSchema set" {
run bash -c "bin/kubeconform -schema-location default -schema-location 'https://raw.githubusercontent.com/datreeio/CRDs-catalog/main/{{.Group}}/{{.ResourceKind}}_{{.ResourceAPIVersion}}.json' fixtures/httpproxy.yaml"
[ "$status" -eq 0 ]
}
13 changes: 13 additions & 0 deletions fixtures/httpproxy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: projectcontour.io/v1
kind: HTTPProxy
metadata:
name: basic
spec:
virtualhost:
fqdn: foo-basic.example.com
routes:
- conditions:
- prefix: /
services:
- name: s1
port: 80
8 changes: 7 additions & 1 deletion pkg/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"strings"

jsonschema "github.com/santhosh-tekuri/jsonschema/v5"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
Expand Down Expand Up @@ -254,7 +255,12 @@ func downloadSchema(registries []registry.Registry, kind, version, k8sVersion st
for _, reg := range registries {
path, schemaBytes, err = reg.DownloadSchema(kind, version, k8sVersion)
if err == nil {
schema, err := jsonschema.CompileString(path, string(schemaBytes))
c := jsonschema.NewCompiler()
c.Draft = jsonschema.Draft4
if err := c.AddResource(path, strings.NewReader(string(schemaBytes))); err != nil {
continue
}
schema, err := c.Compile(path)
// If we got a non-parseable response, we try the next registry
if err != nil {
continue
Expand Down

0 comments on commit ae67bb4

Please sign in to comment.