@@ -15,7 +15,7 @@ import (
15
15
const maxBufferSize = 2048
16
16
17
17
// Validate takes a source CRD and a sample file and validates its contents against the CRD definition.
18
- func Validate (sourceCRD []byte , sampleFile []byte ) error {
18
+ func Validate (sourceCRD []byte , sampleFile []byte , ignoreErrors [] string ) error {
19
19
reader := yaml .NewYAMLOrJSONDecoder (bytes .NewReader (sampleFile ), maxBufferSize )
20
20
obj := & unstructured.Unstructured {}
21
21
err := reader .Decode (obj )
@@ -29,7 +29,7 @@ func Validate(sourceCRD []byte, sampleFile []byte) error {
29
29
}
30
30
31
31
if crd .Spec .Validation != nil && len (crd .Spec .Versions ) == 0 {
32
- return ValidateCRDValidation (crd , sampleFile )
32
+ return ValidateCRDValidation (crd , sampleFile , ignoreErrors )
33
33
}
34
34
35
35
availableVersions := make ([]string , 0 , len (crd .Spec .Versions ))
@@ -41,7 +41,7 @@ func Validate(sourceCRD []byte, sampleFile []byte) error {
41
41
// Make sure we are only testing versions that equal to the CRD's version.
42
42
// This is important in case there are multiple versions in the CRD.
43
43
if obj .GroupVersionKind ().Version == v .Name {
44
- if err := validate (v .Schema .OpenAPIV3Schema , obj , crd .Spec .Names .Kind , v .Name ); err != nil {
44
+ if err := validate (v .Schema .OpenAPIV3Schema , obj , crd .Spec .Names .Kind , v .Name , ignoreErrors ); err != nil {
45
45
return fmt .Errorf ("failed to validate kind %s and version %s: %w" , crd .Spec .Names .Kind , v .Name , err )
46
46
}
47
47
@@ -56,26 +56,33 @@ func Validate(sourceCRD []byte, sampleFile []byte) error {
56
56
)
57
57
}
58
58
59
- func ValidateCRDValidation (crd * apiextensions.CustomResourceDefinition , sampleFile []byte ) error {
59
+ func ValidateCRDValidation (crd * apiextensions.CustomResourceDefinition , sampleFile []byte , ignoreErrors [] string ) error {
60
60
reader := yaml .NewYAMLOrJSONDecoder (bytes .NewReader (sampleFile ), maxBufferSize )
61
61
obj := & unstructured.Unstructured {}
62
62
err := reader .Decode (obj )
63
63
if err != nil {
64
64
return fmt .Errorf ("failed to decode sample file: %w" , err )
65
65
}
66
66
67
- return validate (crd .Spec .Validation .OpenAPIV3Schema , obj , crd .Spec .Names .Kind , crd .Name )
67
+ return validate (crd .Spec .Validation .OpenAPIV3Schema , obj , crd .Spec .Names .Kind , crd .Name , ignoreErrors )
68
68
}
69
69
70
- func validate (props * apiextensions.JSONSchemaProps , obj * unstructured.Unstructured , kind , name string ) error {
70
+ func validate (props * apiextensions.JSONSchemaProps , obj * unstructured.Unstructured , kind , name string , ignoreErrors [] string ) error {
71
71
eval , _ , err := validation .NewSchemaValidator (props )
72
72
if err != nil {
73
73
return fmt .Errorf ("invalid schema: %w" , err )
74
74
}
75
75
76
76
var resultErrors error
77
77
result := eval .Validate (obj )
78
+ loop:
78
79
for _ , e := range result .Errors {
80
+ for _ , ignore := range ignoreErrors {
81
+ if strings .Contains (e .Error (), ignore ) {
82
+ continue loop
83
+ }
84
+ }
85
+
79
86
resultErrors = errors .Join (resultErrors , e )
80
87
}
81
88
0 commit comments