Update jsonschema library to v6 (#324)

This commit is contained in:
Yann Hamon 2025-05-11 02:05:01 +02:00 committed by GitHub
parent df26febc54
commit 31e9679c96
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
125 changed files with 35572 additions and 5135 deletions

4
pkg/cache/cache.go vendored
View file

@ -1,6 +1,6 @@
package cache
type Cache interface {
Get(resourceKind, resourceAPIVersion, k8sVersion string) (interface{}, error)
Set(resourceKind, resourceAPIVersion, k8sVersion string, schema interface{}) error
Get(key string) (any, error)
Set(key string, schema any) error
}

18
pkg/cache/inmemory.go vendored
View file

@ -10,26 +10,21 @@ import (
// - This cache caches the parsed Schemas
type inMemory struct {
sync.RWMutex
schemas map[string]interface{}
schemas map[string]any
}
// New creates a new cache for downloaded schemas
func NewInMemoryCache() Cache {
return &inMemory{
schemas: map[string]interface{}{},
schemas: make(map[string]any),
}
}
func key(resourceKind, resourceAPIVersion, k8sVersion string) string {
return fmt.Sprintf("%s-%s-%s", resourceKind, resourceAPIVersion, k8sVersion)
}
// Get retrieves the JSON schema given a resource signature
func (c *inMemory) Get(resourceKind, resourceAPIVersion, k8sVersion string) (interface{}, error) {
k := key(resourceKind, resourceAPIVersion, k8sVersion)
func (c *inMemory) Get(key string) (any, error) {
c.RLock()
defer c.RUnlock()
schema, ok := c.schemas[k]
schema, ok := c.schemas[key]
if !ok {
return nil, fmt.Errorf("schema not found in in-memory cache")
@ -39,11 +34,10 @@ func (c *inMemory) Get(resourceKind, resourceAPIVersion, k8sVersion string) (int
}
// Set adds a JSON schema to the schema cache
func (c *inMemory) Set(resourceKind, resourceAPIVersion, k8sVersion string, schema interface{}) error {
k := key(resourceKind, resourceAPIVersion, k8sVersion)
func (c *inMemory) Set(key string, schema any) error {
c.Lock()
defer c.Unlock()
c.schemas[k] = schema
c.schemas[key] = schema
return nil
}

17
pkg/cache/ondisk.go vendored
View file

@ -3,7 +3,6 @@ package cache
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"path"
@ -22,17 +21,17 @@ func NewOnDiskCache(cache string) Cache {
}
}
func cachePath(folder, resourceKind, resourceAPIVersion, k8sVersion string) string {
hash := sha256.Sum256([]byte(fmt.Sprintf("%s-%s-%s", resourceKind, resourceAPIVersion, k8sVersion)))
func cachePath(folder, key string) string {
hash := sha256.Sum256([]byte(key))
return path.Join(folder, hex.EncodeToString(hash[:]))
}
// Get retrieves the JSON schema given a resource signature
func (c *onDisk) Get(resourceKind, resourceAPIVersion, k8sVersion string) (interface{}, error) {
func (c *onDisk) Get(key string) (any, error) {
c.RLock()
defer c.RUnlock()
f, err := os.Open(cachePath(c.folder, resourceKind, resourceAPIVersion, k8sVersion))
f, err := os.Open(cachePath(c.folder, key))
if err != nil {
return nil, err
}
@ -42,8 +41,12 @@ func (c *onDisk) Get(resourceKind, resourceAPIVersion, k8sVersion string) (inter
}
// Set adds a JSON schema to the schema cache
func (c *onDisk) Set(resourceKind, resourceAPIVersion, k8sVersion string, schema interface{}) error {
func (c *onDisk) Set(key string, schema any) error {
c.Lock()
defer c.Unlock()
return os.WriteFile(cachePath(c.folder, resourceKind, resourceAPIVersion, k8sVersion), schema.([]byte), 0644)
if _, err := os.Stat(cachePath(c.folder, key)); os.IsNotExist(err) {
return os.WriteFile(cachePath(c.folder, key), schema.([]byte), 0644)
}
return nil
}