rework caching - less leak of goschema everywhere

This commit is contained in:
Yann Hamon 2020-05-31 03:12:28 +02:00
parent 8eb297d4c4
commit b918da9c59
5 changed files with 69 additions and 65 deletions

View file

@ -2,7 +2,6 @@ package registry
import (
"fmt"
"github.com/xeipuuv/gojsonschema"
"io/ioutil"
"net/http"
"strings"
@ -53,7 +52,7 @@ 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) (*gojsonschema.Schema, error) {
func (r KubernetesRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error) {
url := r.schemaURL(resourceKind, resourceAPIVersion, k8sVersion)
resp, err := http.Get(url)
@ -75,5 +74,5 @@ func (r KubernetesRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8s
return nil, fmt.Errorf("failed downloading schema at %s: %s", url, err)
}
return gojsonschema.NewSchema(gojsonschema.NewBytesLoader(body))
return body, nil
}

View file

@ -2,20 +2,18 @@ package registry
import (
"fmt"
"github.com/xeipuuv/gojsonschema"
"io/ioutil"
"os"
"sigs.k8s.io/yaml"
"strings"
)
type LocalSchemas struct {
schemas map[string]*gojsonschema.Schema
schemas map[string]string
}
func NewLocalSchemas(schemaFiles []string) (*LocalSchemas, error) {
schemas := &LocalSchemas{
schemas: map[string]*gojsonschema.Schema{},
schemas: map[string]string{},
}
for _, schemaFile := range schemaFiles {
@ -36,32 +34,29 @@ func NewLocalSchemas(schemaFiles []string) (*LocalSchemas, error) {
} `json:"Names"`
} `json:"Spec"`
}
err = yaml.Unmarshal(content, &parsedSchema)
err = yaml.Unmarshal(content, &parsedSchema) // Index Schemas by kind
if err != nil {
return nil, fmt.Errorf("failed parsing schema %s", schemaFile)
}
if strings.HasSuffix(schemaFile, ".yml") || strings.HasSuffix(schemaFile, ".yaml") {
asJSON, err := yaml.YAMLToJSON(content)
if err != nil {
return nil, fmt.Errorf("error converting manifest %s to JSON: %s", schemaFile, err)
}
schema, err := gojsonschema.NewSchema(gojsonschema.NewBytesLoader(asJSON))
if err != nil {
return nil, err
}
schemas.schemas[parsedSchema.Spec.Names.Kind] = schema
}
schemas.schemas[parsedSchema.Spec.Names.Kind] = schemaFile
}
return schemas, nil
}
func (r LocalSchemas) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) (*gojsonschema.Schema, error) {
schema, ok := r.schemas[resourceKind]
func (r LocalSchemas) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error) {
schemaFile, ok := r.schemas[resourceKind]
if !ok {
return nil, fmt.Errorf("no local schema for Kind %s", resourceKind)
}
return schema, nil
f, err := os.Open(schemaFile)
if err != nil {
return nil, fmt.Errorf("failed to open schema %s", schemaFile)
}
defer f.Close()
content, err := ioutil.ReadAll(f)
return content, nil
}

View file

@ -1,13 +1,11 @@
package registry
import "github.com/xeipuuv/gojsonschema"
type Manifest struct {
Kind, Version string
}
type Registry interface {
DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) (*gojsonschema.Schema, error)
DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error)
}
type Retryable interface {