mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-11 05:59:22 +00:00
add schema caching
This commit is contained in:
parent
79c9197f38
commit
d3ed871833
2 changed files with 40 additions and 1 deletions
5
main.go
5
main.go
|
|
@ -7,6 +7,7 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/yannh/kubeconform/pkg/cache"
|
||||
"github.com/yannh/kubeconform/pkg/registry"
|
||||
"github.com/yannh/kubeconform/pkg/resource"
|
||||
"github.com/yannh/kubeconform/pkg/validator"
|
||||
|
|
@ -23,9 +24,11 @@ func validateFile(f io.Reader, regs []*registry.KubernetesRegistry, k8sVersion s
|
|||
return fmt.Errorf("error while parsing: %s", err)
|
||||
}
|
||||
|
||||
|
||||
var schema []byte
|
||||
for _, reg := range regs {
|
||||
schema, err = reg.DownloadSchema(sig.Kind, sig.Version, k8sVersion)
|
||||
downloadSchema := cache.WithCache(reg.DownloadSchema)
|
||||
schema, err = downloadSchema(sig.Kind, sig.Version, k8sVersion)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
|
|
|
|||
36
pkg/cache/main.go
vendored
Normal file
36
pkg/cache/main.go
vendored
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var mu sync.Mutex
|
||||
var schemas map[string][]byte
|
||||
|
||||
func init () {
|
||||
schemas = map[string][]byte {}
|
||||
}
|
||||
|
||||
func WithCache(downloadSchema func(string, string, string) ([]byte, error)) (func (string, string, string) ([]byte, error)) {
|
||||
return func(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error) {
|
||||
cacheKey := fmt.Sprintf("%s-%s-%s", resourceKind, resourceAPIVersion, k8sVersion)
|
||||
mu.Lock()
|
||||
cachedSchema, ok := schemas[cacheKey];
|
||||
mu.Unlock()
|
||||
if ok {
|
||||
return cachedSchema, nil
|
||||
}
|
||||
|
||||
schema, err := downloadSchema(resourceKind, resourceAPIVersion, k8sVersion)
|
||||
if err != nil {
|
||||
return schema, err
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
schemas[cacheKey] = schema
|
||||
mu.Unlock()
|
||||
|
||||
return schema, nil
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue