mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-22 03:07:01 +00:00
WIP
This commit is contained in:
parent
ecc042d7f9
commit
c954a22d7d
6 changed files with 1392 additions and 300 deletions
File diff suppressed because it is too large
Load diff
4
pkg/cache/cache.go
vendored
4
pkg/cache/cache.go
vendored
|
|
@ -1,6 +1,6 @@
|
||||||
package cache
|
package cache
|
||||||
|
|
||||||
type Cache interface {
|
type Cache interface {
|
||||||
Get(key string) (interface{}, error)
|
Get(key string) ([]byte, error)
|
||||||
Set(key string, schema interface{}) error
|
Set(key string, schema []byte) error
|
||||||
}
|
}
|
||||||
|
|
|
||||||
8
pkg/cache/inmemory.go
vendored
8
pkg/cache/inmemory.go
vendored
|
|
@ -10,18 +10,18 @@ import (
|
||||||
// - This cache caches the parsed Schemas
|
// - This cache caches the parsed Schemas
|
||||||
type inMemory struct {
|
type inMemory struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
schemas map[string]interface{}
|
schemas map[string][]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new cache for downloaded schemas
|
// New creates a new cache for downloaded schemas
|
||||||
func NewInMemoryCache() Cache {
|
func NewInMemoryCache() Cache {
|
||||||
return &inMemory{
|
return &inMemory{
|
||||||
schemas: map[string]interface{}{},
|
schemas: make(map[string][]byte),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get retrieves the JSON schema given a resource signature
|
// Get retrieves the JSON schema given a resource signature
|
||||||
func (c *inMemory) Get(key string) (interface{}, error) {
|
func (c *inMemory) Get(key string) ([]byte, error) {
|
||||||
c.RLock()
|
c.RLock()
|
||||||
defer c.RUnlock()
|
defer c.RUnlock()
|
||||||
schema, ok := c.schemas[key]
|
schema, ok := c.schemas[key]
|
||||||
|
|
@ -34,7 +34,7 @@ func (c *inMemory) Get(key string) (interface{}, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set adds a JSON schema to the schema cache
|
// Set adds a JSON schema to the schema cache
|
||||||
func (c *inMemory) Set(key string, schema interface{}) error {
|
func (c *inMemory) Set(key string, schema []byte) error {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
c.schemas[key] = schema
|
c.schemas[key] = schema
|
||||||
|
|
|
||||||
10
pkg/cache/ondisk.go
vendored
10
pkg/cache/ondisk.go
vendored
|
|
@ -27,7 +27,7 @@ func cachePath(folder, key string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get retrieves the JSON schema given a resource signature
|
// Get retrieves the JSON schema given a resource signature
|
||||||
func (c *onDisk) Get(key string) (interface{}, error) {
|
func (c *onDisk) Get(key string) ([]byte, error) {
|
||||||
c.RLock()
|
c.RLock()
|
||||||
defer c.RUnlock()
|
defer c.RUnlock()
|
||||||
|
|
||||||
|
|
@ -41,8 +41,12 @@ func (c *onDisk) Get(key string) (interface{}, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set adds a JSON schema to the schema cache
|
// Set adds a JSON schema to the schema cache
|
||||||
func (c *onDisk) Set(key string, schema interface{}) error {
|
func (c *onDisk) Set(key string, schema []byte) error {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
return os.WriteFile(cachePath(c.folder, key), schema.([]byte), 0644)
|
|
||||||
|
if _, err := os.Stat(cachePath(c.folder, key)); os.IsNotExist(err) {
|
||||||
|
return os.WriteFile(cachePath(c.folder, key), schema, 0644)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,12 @@
|
||||||
package loader
|
package loader
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/santhosh-tekuri/jsonschema/v6"
|
"github.com/santhosh-tekuri/jsonschema/v6"
|
||||||
"github.com/yannh/kubeconform/pkg/cache"
|
"github.com/yannh/kubeconform/pkg/cache"
|
||||||
|
"io"
|
||||||
gourl "net/url"
|
gourl "net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
@ -23,28 +26,32 @@ func (l FileLoader) Load(url string) (any, error) {
|
||||||
}
|
}
|
||||||
if l.cache != nil {
|
if l.cache != nil {
|
||||||
if cached, err := l.cache.Get(path); err == nil {
|
if cached, err := l.cache.Get(path); err == nil {
|
||||||
return cached, nil
|
return jsonschema.UnmarshalJSON(bytes.NewReader(cached))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := os.Open(path)
|
f, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
msg := fmt.Sprintf("could not open file %s", path)
|
||||||
|
return nil, NewNotFoundError(errors.New(msg))
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
s, err := jsonschema.UnmarshalJSON(f)
|
content, err := io.ReadAll(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.cache != nil {
|
if l.cache != nil {
|
||||||
if err = l.cache.Set(path, s); err != nil {
|
if err = l.cache.Set(path, content); err != nil {
|
||||||
return nil, fmt.Errorf("failed to write cache to disk: %s", err)
|
return nil, fmt.Errorf("failed to write cache to disk: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return s, nil
|
return jsonschema.UnmarshalJSON(bytes.NewReader(content))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToFile is helper method to convert file url to file path.
|
// ToFile is helper method to convert file url to file path.
|
||||||
|
|
@ -54,7 +61,7 @@ func (l FileLoader) ToFile(url string) (string, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if u.Scheme != "file" {
|
if u.Scheme != "file" {
|
||||||
return "", fmt.Errorf("invalid file url: %s", u)
|
return url, nil
|
||||||
}
|
}
|
||||||
path := u.Path
|
path := u.Path
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ type HTTPURLLoader struct {
|
||||||
func (l *HTTPURLLoader) Load(url string) (any, error) {
|
func (l *HTTPURLLoader) Load(url string) (any, error) {
|
||||||
if l.cache != nil {
|
if l.cache != nil {
|
||||||
if cached, err := l.cache.Get(url); err == nil {
|
if cached, err := l.cache.Get(url); err == nil {
|
||||||
return cached, nil
|
return jsonschema.UnmarshalJSON(bytes.NewReader(cached))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -47,17 +47,18 @@ func (l *HTTPURLLoader) Load(url string) (any, error) {
|
||||||
msg := fmt.Sprintf("failed parsing schema from %s: %s", url, err)
|
msg := fmt.Sprintf("failed parsing schema from %s: %s", url, err)
|
||||||
return nil, errors.New(msg)
|
return nil, errors.New(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if l.cache != nil {
|
||||||
|
if err = l.cache.Set(url, body); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to write cache to disk: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
s, err := jsonschema.UnmarshalJSON(bytes.NewReader(body))
|
s, err := jsonschema.UnmarshalJSON(bytes.NewReader(body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.cache != nil {
|
|
||||||
if err = l.cache.Set(url, s); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to write cache to disk: %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue