Module: KubeSchema::SchemaCache

Defined in:
lib/kube_schema/schema_cache.rb

Overview

Lazily downloads individual schema JSON files from the schemas branch on GitHub and caches them locally under Gem.cache_home (XDG_CACHE_HOME).

Usage:

KubeSchema::SchemaCache.fetch("cert-manager.io/certificate_v1")
# => "/home/user/.cache/kube_schema/schemas/cert-manager.io/certificate_v1.json"

KubeSchema::SchemaCache.read("v1.33.6/deployment")
# => "{...json contents...}"

Defined Under Namespace

Classes: DownloadError

Constant Summary collapse

BASE_URL =
'https://raw.githubusercontent.com/general-intelligence-systems/kube_schema/refs/heads/schemas'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cache_dirObject

Root directory for cached schemas. Defaults to ~/.cache/kube_schema/schemas (or $XDG_CACHE_HOME/kube_schema/schemas).



26
27
28
# File 'lib/kube_schema/schema_cache.rb', line 26

def cache_dir
  @cache_dir ||= File.join(Gem.cache_home, 'kube_schema', 'schemas')
end

Class Method Details

.cached?(file_path) ⇒ Boolean

Returns true if the schema is already cached locally.

Returns:

  • (Boolean)


58
59
60
# File 'lib/kube_schema/schema_cache.rb', line 58

def cached?(file_path)
  File.exist?(local_path(file_path))
end

.clear!Object

Removes the entire cache directory.



69
70
71
# File 'lib/kube_schema/schema_cache.rb', line 69

def clear!
  FileUtils.rm_rf(cache_dir)
end

.evict(file_path) ⇒ Object

Removes a single cached schema file.



63
64
65
66
# File 'lib/kube_schema/schema_cache.rb', line 63

def evict(file_path)
  path = local_path(file_path)
  File.delete(path) if File.exist?(path)
end

.fetch(file_path) ⇒ Object

Returns the local file path for a schema, downloading it if not cached. The file_path should NOT include the .json extension.

SchemaCache.fetch("cert-manager.io/certificate_v1")
# => "/home/user/.cache/kube_schema/schemas/cert-manager.io/certificate_v1.json"


39
40
41
42
43
44
45
# File 'lib/kube_schema/schema_cache.rb', line 39

def fetch(file_path)
  local = local_path(file_path)
  return local if File.exist?(local)

  download!(file_path, local)
  local
end

.local_path(file_path) ⇒ Object

Returns the local path where a schema would be cached (without downloading).



53
54
55
# File 'lib/kube_schema/schema_cache.rb', line 53

def local_path(file_path)
  File.join(cache_dir, "#{file_path}.json")
end

.read(file_path) ⇒ Object

Returns the JSON content as a String, downloading if necessary.



48
49
50
# File 'lib/kube_schema/schema_cache.rb', line 48

def read(file_path)
  File.read(fetch(file_path))
end