Skip to content

Commit 9cd46e3

Browse files
Revert jsonschema library to v0.1.2 to maintain old behavior
1 parent d3aa007 commit 9cd46e3

File tree

6 files changed

+52
-155
lines changed

6 files changed

+52
-155
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/fairwindsops/polaris
22

3-
go 1.19
3+
go 1.24.0
44

55
require (
66
github.com/AlecAivazis/survey/v2 v2.3.7
@@ -9,7 +9,7 @@ require (
99
github.com/fatih/color v1.18.0
1010
github.com/gorilla/mux v1.8.1
1111
github.com/pkg/errors v0.9.1
12-
github.com/qri-io/jsonschema v0.2.1
12+
github.com/qri-io/jsonschema v0.1.2
1313
github.com/sirupsen/logrus v1.9.3
1414
github.com/spf13/cobra v1.9.1
1515
github.com/stretchr/testify v1.10.0

pkg/config/checks/hostPortSet.yaml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,11 @@ schema:
88
required:
99
properties:
1010
ports:
11-
oneOf:
12-
- type: "null"
13-
- type: "array"
14-
items:
15-
not:
16-
properties:
17-
hostPort:
18-
not:
19-
enum: [0, null]
20-
required: ["hostPort"]
11+
type: array
12+
items:
13+
properties:
14+
hostPort:
15+
const: 0
2116
mutations:
2217
- op: remove
2318
path: /ports/*/hostPort

pkg/config/schema.go

Lines changed: 40 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ package config
1616

1717
import (
1818
"bytes"
19-
"context"
2019
"encoding/json"
2120
"errors"
2221
"fmt"
2322
"io"
2423
"strings"
2524
"text/template"
2625

27-
jptr "github.com/qri-io/jsonpointer"
2826
"github.com/qri-io/jsonschema"
2927
"github.com/thoas/go-funk"
3028
corev1 "k8s.io/api/core/v1"
@@ -74,11 +72,10 @@ type SchemaCheck struct {
7472
SchemaTarget TargetKind `yaml:"schemaTarget" json:"schemaTarget"`
7573
Schema map[string]interface{} `yaml:"schema" json:"schema"`
7674
SchemaString string `yaml:"schemaString" json:"schemaString"`
77-
JSONSchema string `yaml:"jsonSchema" json:"jsonSchema"`
78-
Validator *jsonschema.Schema `yaml:"-" json:"-"`
75+
Validator jsonschema.RootSchema `yaml:"-" json:"-"`
7976
AdditionalSchemas map[string]map[string]interface{} `yaml:"additionalSchemas" json:"additionalSchemas"`
8077
AdditionalSchemaStrings map[string]string `yaml:"additionalSchemaStrings" json:"additionalSchemaStrings"`
81-
AdditionalValidators map[string]*jsonschema.Schema `yaml:"-" json:"-"`
78+
AdditionalValidators map[string]jsonschema.RootSchema `yaml:"-" json:"-"`
8279
Mutations []Mutation `yaml:"mutations" json:"mutations"`
8380
}
8481

@@ -112,114 +109,59 @@ func ParseCheck(id string, rawBytes []byte) (SchemaCheck, error) {
112109
}
113110

114111
func init() {
115-
jsonschema.RegisterKeyword("resourceMinimum", newResourceMinimum)
116-
jsonschema.RegisterKeyword("resourceMaximum", newResourceMaximum)
112+
jsonschema.RegisterValidator("resourceMinimum", newResourceMinimum)
113+
jsonschema.RegisterValidator("resourceMaximum", newResourceMaximum)
117114
}
118115

119116
type includeExcludeList struct {
120117
Include []string `yaml:"include"`
121118
Exclude []string `yaml:"exclude"`
122119
}
123120

124-
func newResourceMinimum() jsonschema.Keyword {
121+
func newResourceMinimum() jsonschema.Validator {
125122
return new(resourceMinimum)
126123
}
127124

128-
func newResourceMaximum() jsonschema.Keyword {
125+
func newResourceMaximum() jsonschema.Validator {
129126
return new(resourceMaximum)
130127
}
131128

132-
// ValidateKeyword checks that a specified quanitity is not less than the minimum
133-
func (min resourceMinimum) ValidateKeyword(ctx context.Context, currentState *jsonschema.ValidationState, data interface{}) {
134-
path := ""
135-
if currentState.InstanceLocation != nil {
136-
path = currentState.InstanceLocation.String()
137-
}
129+
// Validate checks that a specified quanitity is not less than the minimum
130+
func (min resourceMinimum) Validate(path string, data interface{}, errs *[]jsonschema.ValError) {
138131
err := validateRange(path, string(min), data, true)
139132
if err != nil {
140-
for _, e := range *err {
141-
currentState.AddError(e.InvalidValue, e.Message)
142-
}
143-
}
144-
}
145-
146-
// Register implements jsonschema.Keyword
147-
func (min resourceMinimum) Register(uri string, registry *jsonschema.SchemaRegistry) {}
148-
149-
// Resolve implements jsonschema.Keyword
150-
func (min resourceMinimum) Resolve(pointer jptr.Pointer, uri string) *jsonschema.Schema {
151-
return nil
152-
}
153-
154-
// UnmarshalJSON implements json.Unmarshaler
155-
func (min *resourceMinimum) UnmarshalJSON(data []byte) error {
156-
var s string
157-
if err := json.Unmarshal(data, &s); err != nil {
158-
return err
133+
*errs = append(*errs, *err...)
159134
}
160-
*min = resourceMinimum(s)
161-
return nil
162135
}
163136

164-
// ValidateKeyword checks that a specified quanitity is not greater than the maximum
165-
func (max resourceMaximum) ValidateKeyword(ctx context.Context, currentState *jsonschema.ValidationState, data interface{}) {
166-
path := ""
167-
if currentState.InstanceLocation != nil {
168-
path = currentState.InstanceLocation.String()
169-
}
137+
// Validate checks that a specified quanitity is not greater than the maximum
138+
func (max resourceMaximum) Validate(path string, data interface{}, errs *[]jsonschema.ValError) {
170139
err := validateRange(path, string(max), data, false)
171140
if err != nil {
172-
for _, e := range *err {
173-
currentState.AddError(e.InvalidValue, e.Message)
174-
}
141+
*errs = append(*errs, *err...)
175142
}
176143
}
177144

178-
// Register implements jsonschema.Keyword
179-
func (max resourceMaximum) Register(uri string, registry *jsonschema.SchemaRegistry) {}
180-
181-
// Resolve implements jsonschema.Keyword
182-
func (max resourceMaximum) Resolve(pointer jptr.Pointer, uri string) *jsonschema.Schema {
183-
return nil
184-
}
185-
186-
// UnmarshalJSON implements json.Unmarshaler
187-
func (max *resourceMaximum) UnmarshalJSON(data []byte) error {
188-
var s string
189-
if err := json.Unmarshal(data, &s); err != nil {
190-
return err
191-
}
192-
*max = resourceMaximum(s)
193-
return nil
194-
}
195-
196-
// ValError is a custom type to maintain compatibility with the old API
197-
type ValError struct {
198-
PropertyPath string
199-
InvalidValue interface{}
200-
Message string
201-
}
202-
203-
func parseQuantity(i interface{}) (resource.Quantity, *[]ValError) {
145+
func parseQuantity(i interface{}) (resource.Quantity, *[]jsonschema.ValError) {
204146
if resNum, ok := i.(float64); ok {
205147
i = fmt.Sprintf("%f", resNum)
206148
}
207149
resStr, ok := i.(string)
208150
if !ok {
209-
return resource.Quantity{}, &[]ValError{
151+
return resource.Quantity{}, &[]jsonschema.ValError{
210152
{Message: fmt.Sprintf("Resource quantity %v is not a string", i)},
211153
}
212154
}
213155
q, err := resource.ParseQuantity(resStr)
214156
if err != nil {
215-
return resource.Quantity{}, &[]ValError{
157+
return resource.Quantity{}, &[]jsonschema.ValError{
216158
{Message: fmt.Sprintf("Could not parse resource quantity: %s", resStr)},
217159
}
218160
}
219161
return q, nil
220162
}
221163

222-
func validateRange(path string, limit interface{}, data interface{}, isMinimum bool) *[]ValError {
164+
func validateRange(path string, limit interface{}, data interface{}, isMinimum bool) *[]jsonschema.ValError {
223165
limitQuantity, err := parseQuantity(limit)
224166
if err != nil {
225167
return err
@@ -228,31 +170,17 @@ func validateRange(path string, limit interface{}, data interface{}, isMinimum b
228170
if err != nil {
229171
return err
230172
}
231-
173+
cmp := limitQuantity.Cmp(actualQuantity)
232174
if isMinimum {
233-
// For minimum, the actual quantity should be >= limit
234-
// If actual < limit, then it's an error
235-
// actualQuantity.Cmp(limitQuantity) returns -1 if actual < limit
236-
if actualQuantity.Cmp(limitQuantity) == -1 {
237-
return &[]ValError{
238-
{
239-
PropertyPath: path,
240-
InvalidValue: data,
241-
Message: fmt.Sprintf("%s quantity %v is less than minimum %v", path, actualQuantity, limitQuantity),
242-
},
175+
if cmp == 1 {
176+
return &[]jsonschema.ValError{
177+
{Message: fmt.Sprintf("%s quantity %v is > %v", path, actualQuantity, limitQuantity)},
243178
}
244179
}
245180
} else {
246-
// For maximum, the actual quantity should be <= limit
247-
// If actual > limit, then it's an error
248-
// actualQuantity.Cmp(limitQuantity) returns 1 if actual > limit
249-
if actualQuantity.Cmp(limitQuantity) == 1 {
250-
return &[]ValError{
251-
{
252-
PropertyPath: path,
253-
InvalidValue: data,
254-
Message: fmt.Sprintf("%s quantity %v is greater than maximum %v", path, actualQuantity, limitQuantity),
255-
},
181+
if cmp == -1 {
182+
return &[]jsonschema.ValError{
183+
{Message: fmt.Sprintf("%s quantity %v is < %v", path, actualQuantity, limitQuantity)},
256184
}
257185
}
258186
}
@@ -262,9 +190,7 @@ func validateRange(path string, limit interface{}, data interface{}, isMinimum b
262190
// Initialize sets up the schema
263191
func (check *SchemaCheck) Initialize(id string) error {
264192
check.ID = id
265-
if check.JSONSchema != "" {
266-
check.SchemaString = check.JSONSchema
267-
} else if check.SchemaString == "" {
193+
if check.SchemaString == "" {
268194
jsonBytes, err := json.Marshal(check.Schema)
269195
if err != nil {
270196
return err
@@ -325,74 +251,50 @@ func (check SchemaCheck) TemplateForResource(res interface{}) (*SchemaCheck, err
325251
}
326252
}
327253

328-
newCheck.AdditionalValidators = map[string]*jsonschema.Schema{}
254+
newCheck.AdditionalValidators = map[string]jsonschema.RootSchema{}
329255
for kind, schemaStr := range newCheck.AdditionalSchemaStrings {
330-
var schemaMap map[string]interface{}
331-
err := UnmarshalYAMLOrJSON([]byte(schemaStr), &schemaMap)
332-
if err != nil {
333-
return nil, err
334-
}
335-
jsonBytes, err := json.Marshal(schemaMap)
256+
val := jsonschema.RootSchema{}
257+
err := UnmarshalYAMLOrJSON([]byte(schemaStr), &val)
336258
if err != nil {
337259
return nil, err
338260
}
339-
val := jsonschema.Must(string(jsonBytes))
340261
newCheck.AdditionalValidators[kind] = val
341262
}
342-
if newCheck.SchemaString != "" {
343-
var schemaMap map[string]interface{}
344-
err := UnmarshalYAMLOrJSON([]byte(newCheck.SchemaString), &schemaMap)
345-
if err != nil {
346-
return nil, err
347-
}
348-
jsonBytes, err := json.Marshal(schemaMap)
349-
if err != nil {
350-
return nil, err
351-
}
352-
353-
// Custom keywords are now registered globally in init()
354-
355-
356-
newCheck.Validator = jsonschema.Must(string(jsonBytes))
263+
err := UnmarshalYAMLOrJSON([]byte(newCheck.SchemaString), &newCheck.Validator)
264+
if err != nil {
265+
return nil, err
357266
}
358-
return &newCheck, nil
267+
return &newCheck, err
359268
}
360269

361270
// CheckPodSpec checks a pod spec against the schema
362-
func (check SchemaCheck) CheckPodSpec(pod *corev1.PodSpec) (bool, []jsonschema.KeyError, error) {
271+
func (check SchemaCheck) CheckPodSpec(pod *corev1.PodSpec) (bool, []jsonschema.ValError, error) {
363272
return check.CheckObject(pod)
364273
}
365274

366275
// CheckPodTemplate checks a pod template against the schema
367-
func (check SchemaCheck) CheckPodTemplate(podTemplate interface{}) (bool, []jsonschema.KeyError, error) {
276+
func (check SchemaCheck) CheckPodTemplate(podTemplate interface{}) (bool, []jsonschema.ValError, error) {
368277
return check.CheckObject(podTemplate)
369278
}
370279

371280
// CheckController checks a controler's spec against the schema
372-
func (check SchemaCheck) CheckController(bytes []byte) (bool, []jsonschema.KeyError, error) {
373-
if check.Validator == nil {
374-
return true, nil, nil
375-
}
376-
errs, err := check.Validator.ValidateBytes(context.Background(), bytes)
281+
func (check SchemaCheck) CheckController(bytes []byte) (bool, []jsonschema.ValError, error) {
282+
errs, err := check.Validator.ValidateBytes(bytes)
377283
return len(errs) == 0, errs, err
378284
}
379285

380286
// CheckContainer checks a container spec against the schema
381-
func (check SchemaCheck) CheckContainer(container *corev1.Container) (bool, []jsonschema.KeyError, error) {
287+
func (check SchemaCheck) CheckContainer(container *corev1.Container) (bool, []jsonschema.ValError, error) {
382288
return check.CheckObject(container)
383289
}
384290

385291
// CheckObject checks arbitrary data against the schema
386-
func (check SchemaCheck) CheckObject(obj interface{}) (bool, []jsonschema.KeyError, error) {
292+
func (check SchemaCheck) CheckObject(obj interface{}) (bool, []jsonschema.ValError, error) {
387293
bytes, err := json.Marshal(obj)
388294
if err != nil {
389295
return false, nil, err
390296
}
391-
if check.Validator == nil {
392-
return true, nil, nil
393-
}
394-
395-
errs, err := check.Validator.ValidateBytes(context.Background(), bytes)
297+
errs, err := check.Validator.ValidateBytes(bytes)
396298
return len(errs) == 0, errs, err
397299
}
398300

@@ -407,7 +309,7 @@ func (check SchemaCheck) CheckAdditionalObjects(groupkind string, objects []inte
407309
if err != nil {
408310
return false, err
409311
}
410-
errs, err := val.ValidateBytes(context.Background(), bytes)
312+
errs, err := val.ValidateBytes(bytes)
411313
if err != nil {
412314
return false, err
413315
}
@@ -465,4 +367,4 @@ func (check SchemaCheck) IsActionable(target TargetKind, kind string, isInit boo
465367
}
466368
}
467369
return true
468-
}
370+
}

pkg/validator/custom.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/qri-io/jsonschema"
77
)
88

9-
type validatorFunction func(test schemaTestCase) (bool, []jsonschema.KeyError, error)
9+
type validatorFunction func(test schemaTestCase) (bool, []jsonschema.ValError, error)
1010

1111
var validatorMapper = map[string]validatorFunction{}
1212
var lock = &sync.Mutex{}

pkg/validator/pdb_hpa_validator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func init() {
1919
registerCustomChecks("pdbMinAvailableGreaterThanHPAMinReplicas", pdbMinAvailableGreaterThanHPAMinReplicas)
2020
}
2121

22-
func pdbMinAvailableGreaterThanHPAMinReplicas(test schemaTestCase) (bool, []jsonschema.KeyError, error) {
22+
func pdbMinAvailableGreaterThanHPAMinReplicas(test schemaTestCase) (bool, []jsonschema.ValError, error) {
2323
if test.ResourceProvider == nil {
2424
return true, nil, nil
2525
}
@@ -70,7 +70,7 @@ func pdbMinAvailableGreaterThanHPAMinReplicas(test schemaTestCase) (bool, []json
7070
}
7171

7272
if attachedHPA.Spec.MinReplicas != nil && pdbMinAvailable > int(*attachedHPA.Spec.MinReplicas) {
73-
return false, []jsonschema.KeyError{
73+
return false, []jsonschema.ValError{
7474
{
7575
PropertyPath: "spec.minAvailable",
7676
InvalidValue: pdbMinAvailable,

pkg/validator/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func getTemplateInput(test schemaTestCase) (map[string]interface{}, error) {
133133
return templateInput, nil
134134
}
135135

136-
func makeResult(conf *config.Configuration, check *config.SchemaCheck, passes bool, issues []jsonschema.KeyError) ResultMessage {
136+
func makeResult(conf *config.Configuration, check *config.SchemaCheck, passes bool, issues []jsonschema.ValError) ResultMessage {
137137
details := []string{}
138138
for _, issue := range issues {
139139
details = append(details, issue.Message)
@@ -394,7 +394,7 @@ func applySchemaCheck(conf *config.Configuration, checkID string, test schemaTes
394394
} else if validatorMapper[checkID] != nil {
395395
passes, issues, err = validatorMapper[checkID](test)
396396
} else {
397-
passes, issues, err = true, []jsonschema.KeyError{}, nil
397+
passes, issues, err = true, []jsonschema.ValError{}, nil
398398
}
399399
if err != nil {
400400
return nil, err

0 commit comments

Comments
 (0)