Skip to content

Commit

Permalink
WIP - upgrade jsonschema to v6
Browse files Browse the repository at this point in the history
  • Loading branch information
yannh committed Feb 16, 2025
1 parent 1bd4498 commit 3571814
Show file tree
Hide file tree
Showing 113 changed files with 24,459 additions and 4,755 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.bats
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM bats/bats:1.11.0
RUN apk --no-cache add ca-certificates parallel libxml2-utils
COPY dist/kubeconform_linux_amd64_v1/kubeconform /code/bin/
COPY bin/kubeconform /code/bin/
COPY acceptance.bats acceptance-nonetwork.bats /code/
COPY fixtures /code/fixtures
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ go 1.22

require (
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1
sigs.k8s.io/yaml v1.4.0
)

require github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
require (
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
golang.org/x/text v0.14.0 // indirect
)
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
Expand All @@ -12,10 +14,12 @@ github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxec
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 h1:PKK9DyHxif4LZo+uQSgXNqs0jj5+xZwwfKHgph2lxBw=
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
Expand Down
24 changes: 17 additions & 7 deletions pkg/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"context"
"errors"
"fmt"
"golang.org/x/text/language"
"golang.org/x/text/message"
"io"

jsonschema "github.com/santhosh-tekuri/jsonschema/v5"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
jsonschema "github.com/santhosh-tekuri/jsonschema/v6"
"github.com/yannh/kubeconform/pkg/cache"
"github.com/yannh/kubeconform/pkg/registry"
"github.com/yannh/kubeconform/pkg/resource"
Expand Down Expand Up @@ -197,9 +198,13 @@ func (val *v) ValidateResource(res resource.Resource) Result {
var e *jsonschema.ValidationError
if errors.As(err, &e) {
for _, ve := range e.Causes {
path := ""
for _, f := range ve.InstanceLocation {
path = path + "/" + f
}
validationErrors = append(validationErrors, ValidationError{
Path: ve.InstanceLocation,
Msg: ve.Message,
Path: path,
Msg: ve.LocalizedError(message.NewPrinter(language.English)),
})
}

Expand Down Expand Up @@ -249,16 +254,21 @@ func (val *v) Validate(filename string, r io.ReadCloser) []Result {
}

func downloadSchema(registries []registry.Registry, kind, version, k8sVersion string) (*jsonschema.Schema, error) {
var err error
var schemaBytes []byte
var path string
var err error

for _, reg := range registries {
path, schemaBytes, err = reg.DownloadSchema(kind, version, k8sVersion)
if err == nil {
s, err := jsonschema.UnmarshalJSON(bytes.NewReader(schemaBytes))
if err != nil {
continue
}
c := jsonschema.NewCompiler()
c.Draft = jsonschema.Draft4
if err := c.AddResource(path, bytes.NewReader(schemaBytes)); err != nil {
//c.Draft = jsonschema.Draft4
c.DefaultDraft(jsonschema.Draft4)
if err := c.AddResource(path, s); err != nil {
continue
}
schema, err := c.Compile(path)
Expand Down
8 changes: 4 additions & 4 deletions pkg/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,11 @@ lastName: bar
}

if len(got.ValidationErrors) != len(testCase.expectErrors) {
t.Errorf("Test '%s': expected ValidationErrors: %+v, got: % v", testCase.name, testCase.expectErrors, got.ValidationErrors)
t.Errorf("Test '%s': expected ValidationErrors: %+#v, got: %+#v", testCase.name, testCase.expectErrors, got.ValidationErrors)
}
for i, _ := range testCase.expectErrors {
if testCase.expectErrors[i] != got.ValidationErrors[i] {
t.Errorf("Test '%s': expected ValidationErrors: %+v, got: % v", testCase.name, testCase.expectErrors, got.ValidationErrors)
t.Errorf("Test '%s': expected ValidationErrors: %+#v, got: %+#v", testCase.name, testCase.expectErrors, got.ValidationErrors)
}
}
}
Expand Down Expand Up @@ -467,7 +467,7 @@ age: not a number

got := val.ValidateResource(resource.Resource{Bytes: rawResource})
if !reflect.DeepEqual(expectedErrors, got.ValidationErrors) {
t.Errorf("Expected %+v, got %+v", expectedErrors, got.ValidationErrors)
t.Errorf("Expected %+#v, got %+#v", expectedErrors, got.ValidationErrors)
}
}

Expand Down Expand Up @@ -529,6 +529,6 @@ firstName: foo
t.Errorf("Expected %+v, got %+v", expectedStatuses, gotStatuses)
}
if !reflect.DeepEqual(expectedValidationErrors, gotValidationErrors) {
t.Errorf("Expected %+v, got %+v", expectedValidationErrors, gotValidationErrors)
t.Errorf("Expected %+#v, got %+#v", expectedValidationErrors, gotValidationErrors)
}
}
4 changes: 0 additions & 4 deletions vendor/github.com/santhosh-tekuri/jsonschema/v5/.gitignore

This file was deleted.

220 changes: 0 additions & 220 deletions vendor/github.com/santhosh-tekuri/jsonschema/v5/README.md

This file was deleted.

Loading

0 comments on commit 3571814

Please sign in to comment.