@@ -2,16 +2,14 @@ package kubeval
22
33import (
44 "bytes"
5- "errors"
65 "fmt"
76 "regexp"
8- "runtime"
97 "strings"
108
119 "github.com/hashicorp/go-multierror"
1210 "github.com/spf13/viper"
1311 "github.com/xeipuuv/gojsonschema"
14- "gopkg.in /yaml.v2 "
12+ "sigs.k8s.io /yaml"
1513
1614 "github.com/instrumenta/kubeval/log"
1715)
@@ -51,16 +49,6 @@ var ExitOnError bool
5149// schema validation
5250var KindsToSkip []string
5351
54- // in is a method which tests whether the `key` is in the set
55- func in (set []string , key string ) bool {
56- for _ , k := range set {
57- if k == key {
58- return true
59- }
60- }
61- return false
62- }
63-
6452// ValidFormat is a type for quickly forcing
6553// new formats on the gojsonschema loader
6654type ValidFormat struct {}
@@ -81,15 +69,6 @@ type ValidationResult struct {
8169 Errors []gojsonschema.ResultError
8270}
8371
84- // detectLineBreak returns the relevant platform specific line ending
85- func detectLineBreak (haystack []byte ) string {
86- windowsLineEnding := bytes .Contains (haystack , []byte ("\r \n " ))
87- if windowsLineEnding && runtime .GOOS == "windows" {
88- return "\r \n "
89- }
90- return "\n "
91- }
92-
9372func determineSchema (kind string , apiVersion string ) string {
9473 // We have both the upstream Kubernetes schemas and the OpenShift schemas available
9574 // the tool can toggle between then using the --openshift boolean flag and here we
@@ -148,57 +127,26 @@ func determineSchema(kind string, apiVersion string) string {
148127 return fmt .Sprintf ("%s/%s-standalone%s/%s%s.json" , baseURL , normalisedVersion , strictSuffix , strings .ToLower (kind ), kindSuffix )
149128}
150129
151- func determineKind (body interface {}) (string , error ) {
152- cast , _ := body .(map [string ]interface {})
153- if _ , ok := cast ["kind" ]; ! ok {
154- return "" , errors .New ("Missing a kind key" )
155- }
156- if cast ["kind" ] == nil {
157- return "" , errors .New ("Missing a kind value" )
158- }
159- return cast ["kind" ].(string ), nil
160- }
161-
162- func determineAPIVersion (body interface {}) (string , error ) {
163- cast , _ := body .(map [string ]interface {})
164- if _ , ok := cast ["apiVersion" ]; ! ok {
165- return "" , errors .New ("Missing a apiVersion key" )
166- }
167- if cast ["apiVersion" ] == nil {
168- return "" , errors .New ("Missing a apiVersion value" )
169- }
170- return cast ["apiVersion" ].(string ), nil
171- }
172-
173130// validateResource validates a single Kubernetes resource against
174131// the relevant schema, detecting the type of resource automatically
175132func validateResource (data []byte , fileName string , schemaCache map [string ]* gojsonschema.Schema ) (ValidationResult , error ) {
176- var spec interface {}
177133 result := ValidationResult {}
178134 result .FileName = fileName
179- err := yaml .Unmarshal (data , & spec )
135+ var body map [string ]interface {}
136+ err := yaml .Unmarshal (data , & body )
180137 if err != nil {
181- return result , errors .New ("Failed to decode YAML from " + fileName )
182- }
183-
184- body := convertToStringKeys (spec )
185-
186- if body == nil {
138+ return result , fmt .Errorf ("Failed to decode YAML from %s: %s" , fileName , err .Error ())
139+ } else if body == nil {
187140 return result , nil
188141 }
189142
190- cast , _ := body .(map [string ]interface {})
191- if len (cast ) == 0 {
192- return result , nil
193- }
194-
195- kind , err := determineKind (body )
143+ kind , err := getString (body , "kind" )
196144 if err != nil {
197145 return result , err
198146 }
199147 result .Kind = kind
200148
201- apiVersion , err := determineAPIVersion (body )
149+ apiVersion , err := getString (body , "apiVersion" )
202150 if err != nil {
203151 return result , err
204152 }
@@ -216,7 +164,6 @@ func validateResource(data []byte, fileName string, schemaCache map[string]*gojs
216164 return result , nil
217165}
218166
219-
220167func validateAgainstSchema (body interface {}, resource * ValidationResult , schemaCache map [string ]* gojsonschema.Schema ) ([]gojsonschema.ResultError , error ) {
221168 if IgnoreMissingSchemas {
222169 log .Warn ("Warning: Set to ignore missing schemas" )
0 commit comments