From 5f3504724f0c57ff5fbae136e693e0d0dc674c66 Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Sat, 30 May 2020 18:28:04 +0200 Subject: [PATCH] add support for local schemas --- Readme.md | 2 ++ main.go | 17 ++++++++--- pkg/registry/registry.go | 64 +--------------------------------------- 3 files changed, 16 insertions(+), 67 deletions(-) diff --git a/Readme.md b/Readme.md index 9fda4d7..21b5537 100644 --- a/Readme.md +++ b/Readme.md @@ -13,6 +13,8 @@ Usage of ./bin/kubeconform: version of Kubernetes to test against (default "1.18.0") -output string output format - text, json (default "text") + -schema value + file containing an additional Schema (can be specified multiple times) -skipKinds string comma-separated list of kinds to ignore ``` diff --git a/main.go b/main.go index 14df454..6fb8be6 100644 --- a/main.go +++ b/main.go @@ -24,7 +24,7 @@ type validationResult struct { // filter returns true if the file should be skipped // Returning an array, this Reader might container multiple resources -func validateFile(f io.Reader, regs []*registry.KubernetesRegistry, k8sVersion string, skip func(signature resource.Signature)bool) []validationResult { +func validateFile(f io.Reader, regs []registry.Registry, k8sVersion string, skip func(signature resource.Signature)bool) []validationResult { rawResource, err := ioutil.ReadAll(f) if err != nil { return []validationResult{{err: fmt.Errorf("failed reading file: %s", err)}} @@ -74,10 +74,11 @@ func (i *arrayFiles) Set(value string) error { func realMain() int { - var files, dirs arrayFiles + var files, dirs, schemas arrayFiles var skipKinds, k8sVersion, outputFormat string flag.Var(&files, "file", "file to validate (can be specified multiple times)") flag.Var(&dirs, "dir", "directory to validate (can be specified multiple times)") + flag.Var(&schemas, "schema", "file containing an additional Schema") flag.StringVar(&k8sVersion, "k8sversion", "1.18.0", "version of Kubernetes to test against") flag.StringVar(&skipKinds, "skipKinds", "", "comma-separated list of kinds to ignore") flag.StringVar(&outputFormat, "output", "text", "output format - text, json") @@ -119,7 +120,15 @@ func realMain() int { close(fileBatches) }() - r := registry.NewKubernetesRegistry(false) + registries := []registry.Registry{} + registries = append(registries, registry.NewKubernetesRegistry(false)) + if len(schemas) > 0 { + localRegistry, err := registry.NewLocalSchemas(schemas) + if err != nil { + log.Fatalf("%s", err) + } + registries = append(registries, localRegistry) + } for fileBatch := range fileBatches { for _, filename := range fileBatch { @@ -129,7 +138,7 @@ func realMain() int { continue } - res := validateFile(f, []*registry.KubernetesRegistry{r}, k8sVersion, filter) + res := validateFile(f, registries, k8sVersion, filter) f.Close() diff --git a/pkg/registry/registry.go b/pkg/registry/registry.go index 4476c67..ad75a5f 100644 --- a/pkg/registry/registry.go +++ b/pkg/registry/registry.go @@ -1,71 +1,9 @@ package registry -import ( - "fmt" - "io/ioutil" - "net/http" - "strings" -) - type Manifest struct { Kind, Version string } type Registry interface { - DownloadSchema(kind, apiVersion string) error + DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error) } - -type KubernetesRegistry struct { - baseURL string - strict bool -} - -func NewKubernetesRegistry(strict bool) *KubernetesRegistry { - return &KubernetesRegistry{ - baseURL: "https://kubernetesjsonschema.dev", - strict: strict, - } -} - - -func (r KubernetesRegistry) schemaURL(resourceKind, resourceAPIVersion, k8sVersion string) string { - normalisedVersion := k8sVersion - if normalisedVersion != "master" { - normalisedVersion = "v" + normalisedVersion - } - - strictSuffix := "" - if r.strict { - strictSuffix = "-strict" - } - - groupParts := strings.Split(resourceAPIVersion, "/") - versionParts := strings.Split(groupParts[0], ".") - - kindSuffix := "-" + strings.ToLower(versionParts[0]) - if len(groupParts) > 1 { - kindSuffix += "-" + strings.ToLower(groupParts[1]) - } - - 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) ([]byte, error) { - url := r.schemaURL(resourceKind, resourceAPIVersion, k8sVersion) - - resp, err := http.Get(url) - if err != nil { - 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 []byte{}, fmt.Errorf("failed downloading schema at %s: %s", url, err) - } - - fmt.Printf("downloaded %s\n", url) - - return body, nil -} -