mirror of
https://github.com/yannh/kubeconform.git
synced 2026-04-10 07:04:16 +00:00
try to validate
This commit is contained in:
parent
fe33d14dc4
commit
96e35cb926
6 changed files with 87 additions and 21 deletions
|
|
@ -50,22 +50,22 @@ func (r KubernetesRegistry) schemaURL(resourceKind, resourceAPIVersion, k8sVersi
|
|||
return fmt.Sprintf("%s/%s-standalone%s/%s%s.json", r.baseURL, normalisedVersion, strictSuffix, strings.ToLower(resourceKind), kindSuffix)
|
||||
}
|
||||
|
||||
func (r KubernetesRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) (string, error) {
|
||||
func (r KubernetesRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error) {
|
||||
url := r.schemaURL(resourceKind, resourceAPIVersion, k8sVersion)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed downloading schema at %s: %s", url, err)
|
||||
return []byte{}, fmt.Errorf("failed downloading schema at %s: %s", url, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed downloading schema at %s: %s", url, err)
|
||||
return []byte{}, fmt.Errorf("failed downloading schema at %s: %s", url, err)
|
||||
}
|
||||
|
||||
fmt.Printf("downloaded %s\n", url)
|
||||
|
||||
return string(body), nil
|
||||
return body, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,22 +1,15 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Resource struct {
|
||||
type Signature struct {
|
||||
Kind, Version, Namespace string
|
||||
}
|
||||
|
||||
// TODO: Support multi-resources yaml files
|
||||
func Read(r io.Reader) (Resource, error) {
|
||||
s, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return Resource{}, err
|
||||
}
|
||||
|
||||
func SignatureFromBytes(s []byte) (Signature, error) {
|
||||
resource := struct {
|
||||
APIVersion string `yaml:"apiVersion"`
|
||||
Kind string `yaml:"kind"`
|
||||
|
|
@ -24,7 +17,8 @@ func Read(r io.Reader) (Resource, error) {
|
|||
Namespace string `yaml:"Namespace"`
|
||||
} `yaml:"Metadata"`
|
||||
}{}
|
||||
err = yaml.Unmarshal(s, &resource)
|
||||
err := yaml.Unmarshal(s, &resource)
|
||||
|
||||
return Signature{Kind: resource.Kind, Version: resource.APIVersion, Namespace: resource.Metadata.Namespace}, err
|
||||
}
|
||||
|
||||
return Resource{Kind: resource.Kind, Version: resource.APIVersion, Namespace: resource.Metadata.Namespace}, err
|
||||
}
|
||||
42
pkg/validator/main.go
Normal file
42
pkg/validator/main.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package validator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/xeipuuv/gojsonschema"
|
||||
)
|
||||
|
||||
// ValidFormat is a type for quickly forcing
|
||||
// new formats on the gojsonschema loader
|
||||
type ValidFormat struct{}
|
||||
func (f ValidFormat) IsFormat(input interface{}) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func init () {
|
||||
gojsonschema.FormatCheckers.Add("int64", ValidFormat{})
|
||||
gojsonschema.FormatCheckers.Add("byte", ValidFormat{})
|
||||
gojsonschema.FormatCheckers.Add("int32", ValidFormat{})
|
||||
gojsonschema.FormatCheckers.Add("int-or-string", ValidFormat{})
|
||||
}
|
||||
|
||||
func Validate(resource interface{}, rawSchema []byte) error {
|
||||
schemaLoader := gojsonschema.NewBytesLoader(rawSchema)
|
||||
schema, err := gojsonschema.NewSchema(schemaLoader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
documentLoader := gojsonschema.NewGoLoader(resource)
|
||||
results, err := schema.Validate(documentLoader)
|
||||
if err != nil {
|
||||
// This error can only happen if the Object to validate is poorly formed. There's no hope of saving this one
|
||||
return fmt.Errorf("problem validating schema. Check JSON formatting: %s", err)
|
||||
}
|
||||
|
||||
if !results.Valid() {
|
||||
return fmt.Errorf("resource does not conform to schema")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue