This commit is contained in:
Yann Hamon 2025-05-10 23:12:14 +02:00
parent fd6904f2b4
commit ecc042d7f9
7 changed files with 107 additions and 46 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) (interface{}, error)
Set(key string, schema interface{}) error
}

14
pkg/cache/inmemory.go vendored
View file

@ -20,16 +20,11 @@ func NewInMemoryCache() Cache {
}
}
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) (interface{}, 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 interface{}) error {
c.Lock()
defer c.Unlock()
c.schemas[k] = schema
c.schemas[key] = schema
return nil
}

13
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) (interface{}, 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,8 @@ 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 interface{}) error {
c.Lock()
defer c.Unlock()
return os.WriteFile(cachePath(c.folder, resourceKind, resourceAPIVersion, k8sVersion), schema.([]byte), 0644)
return os.WriteFile(cachePath(c.folder, key), schema.([]byte), 0644)
}