mirror of
https://github.com/yannh/kubeconform.git
synced 2026-04-13 16:14:17 +00:00
add schema caching
This commit is contained in:
parent
79c9197f38
commit
d3ed871833
2 changed files with 40 additions and 1 deletions
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…
Add table
Add a link
Reference in a new issue