diff --git a/pkg/registry/http.go b/pkg/registry/http.go index db49f96..2af0c6c 100644 --- a/pkg/registry/http.go +++ b/pkg/registry/http.go @@ -61,15 +61,15 @@ func newHTTPRegistry(schemaPathTemplate string, cacheFolder string, strict bool, } // DownloadSchema downloads the schema for a particular resource from an HTTP server -func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error) { +func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) (string, []byte, error) { url, err := schemaPath(r.schemaPathTemplate, resourceKind, resourceAPIVersion, k8sVersion, r.strict) if err != nil { - return nil, err + return "", nil, err } if r.cache != nil { if b, err := r.cache.Get(resourceKind, resourceAPIVersion, k8sVersion); err == nil { - return b.([]byte), nil + return url, b.([]byte), nil } } @@ -79,7 +79,7 @@ func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVers if r.debug { log.Println(msg) } - return nil, errors.New(msg) + return url, nil, errors.New(msg) } defer resp.Body.Close() @@ -88,7 +88,7 @@ func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVers if r.debug { log.Print(msg) } - return nil, newNotFoundError(errors.New(msg)) + return url, nil, newNotFoundError(errors.New(msg)) } if resp.StatusCode != http.StatusOK { @@ -96,7 +96,7 @@ func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVers if r.debug { log.Print(msg) } - return nil, fmt.Errorf(msg) + return url, nil, fmt.Errorf(msg) } body, err := ioutil.ReadAll(resp.Body) @@ -105,7 +105,7 @@ func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVers if r.debug { log.Print(msg) } - return nil, errors.New(msg) + return url, nil, errors.New(msg) } if r.debug { @@ -114,9 +114,9 @@ func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVers if r.cache != nil { if err := r.cache.Set(resourceKind, resourceAPIVersion, k8sVersion, body); err != nil { - return nil, fmt.Errorf("failed writing schema to cache: %s", err) + return url, nil, fmt.Errorf("failed writing schema to cache: %s", err) } } - return body, nil + return url, body, nil } diff --git a/pkg/registry/local.go b/pkg/registry/local.go index 81d69e2..ec9e047 100644 --- a/pkg/registry/local.go +++ b/pkg/registry/local.go @@ -24,10 +24,10 @@ func newLocalRegistry(pathTemplate string, strict bool, debug bool) (*LocalRegis } // DownloadSchema retrieves the schema from a file for the resource -func (r LocalRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error) { +func (r LocalRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) (string, []byte, error) { schemaFile, err := schemaPath(r.pathTemplate, resourceKind, resourceAPIVersion, k8sVersion, r.strict) if err != nil { - return []byte{}, nil + return schemaFile, []byte{}, nil } f, err := os.Open(schemaFile) if err != nil { @@ -36,14 +36,14 @@ func (r LocalRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersi if r.debug { log.Print(msg) } - return nil, newNotFoundError(errors.New(msg)) + return schemaFile, nil, newNotFoundError(errors.New(msg)) } msg := fmt.Sprintf("failed to open schema at %s: %s", schemaFile, err) if r.debug { log.Print(msg) } - return nil, errors.New(msg) + return schemaFile, nil, errors.New(msg) } defer f.Close() @@ -53,11 +53,11 @@ func (r LocalRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersi if r.debug { log.Print(msg) } - return nil, err + return schemaFile, nil, err } if r.debug { log.Printf("using schema found at %s", schemaFile) } - return content, nil + return schemaFile, content, nil } diff --git a/pkg/registry/registry.go b/pkg/registry/registry.go index afde801..154f40c 100644 --- a/pkg/registry/registry.go +++ b/pkg/registry/registry.go @@ -13,7 +13,7 @@ type Manifest struct { // Registry is an interface that should be implemented by any source of Kubernetes schemas type Registry interface { - DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error) + DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) (string, []byte, error) } // Retryable indicates whether an error is a temporary or a permanent failure diff --git a/pkg/validator/validator.go b/pkg/validator/validator.go index 8eac3e5..2988de0 100644 --- a/pkg/validator/validator.go +++ b/pkg/validator/validator.go @@ -221,11 +221,12 @@ func (val *v) Validate(filename string, r io.ReadCloser) []Result { func downloadSchema(registries []registry.Registry, kind, version, k8sVersion string) (*jsonschema.Schema, error) { var err error var schemaBytes []byte + var path string for _, reg := range registries { - schemaBytes, err = reg.DownloadSchema(kind, version, k8sVersion) + path, schemaBytes, err = reg.DownloadSchema(kind, version, k8sVersion) if err == nil { - schema, err := jsonschema.CompileString(fmt.Sprintf("%s%s%s", kind, version, k8sVersion), string(schemaBytes)) + schema, err := jsonschema.CompileString(path, string(schemaBytes)) // If we got a non-parseable response, we try the next registry if err != nil { fmt.Printf("TOTO %s\n", err)