mirror of
https://github.com/yannh/kubeconform.git
synced 2026-04-11 23:44:16 +00:00
cache Schema unmarshalling, cache schema download failures (WIP)
This commit is contained in:
parent
87f1a0531e
commit
30a6fe69b1
4 changed files with 22 additions and 25 deletions
14
pkg/cache/main.go
vendored
14
pkg/cache/main.go
vendored
|
|
@ -2,18 +2,19 @@ package cache
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/xeipuuv/gojsonschema"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var mu sync.Mutex
|
||||
var schemas map[string][]byte
|
||||
var schemas map[string]*gojsonschema.Schema
|
||||
|
||||
func init () {
|
||||
schemas = map[string][]byte {}
|
||||
schemas = map[string]*gojsonschema.Schema{}
|
||||
}
|
||||
|
||||
func WithCache(downloadSchema func(string, string, string) ([]byte, error)) (func (string, string, string) ([]byte, error)) {
|
||||
return func(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error) {
|
||||
func WithCache(downloadSchema func(string, string, string) (*gojsonschema.Schema, error)) (func (string, string, string) (*gojsonschema.Schema, error)) {
|
||||
return func(resourceKind, resourceAPIVersion, k8sVersion string) (*gojsonschema.Schema, error) {
|
||||
cacheKey := fmt.Sprintf("%s-%s-%s", resourceKind, resourceAPIVersion, k8sVersion)
|
||||
mu.Lock()
|
||||
cachedSchema, ok := schemas[cacheKey];
|
||||
|
|
@ -23,14 +24,11 @@ func WithCache(downloadSchema func(string, string, string) ([]byte, error)) (fun
|
|||
}
|
||||
|
||||
schema, err := downloadSchema(resourceKind, resourceAPIVersion, k8sVersion)
|
||||
if err != nil {
|
||||
return schema, err
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
schemas[cacheKey] = schema
|
||||
mu.Unlock()
|
||||
|
||||
return schema, nil
|
||||
return schema, err
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue