Module: Kube::Schema::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:

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

Kube::Schema::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).



27
28
29
# File 'lib/kube/schema/schema_cache.rb', line 27

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)


65
66
67
# File 'lib/kube/schema/schema_cache.rb', line 65

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

.clear!Object

Removes the entire cache directory.



79
80
81
# File 'lib/kube/schema/schema_cache.rb', line 79

def clear!
  FileUtils.rm_rf(cache_dir)
end

.evict(file_path) ⇒ Object

Removes a single cached schema file.



70
71
72
73
74
75
76
# File 'lib/kube/schema/schema_cache.rb', line 70

def evict(file_path)
  path = local_path(file_path)

  if File.exist?(path)
    File.delete(path)
  end
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"


40
41
42
43
44
45
46
47
48
# File 'lib/kube/schema/schema_cache.rb', line 40

def fetch(file_path)
  local = local_path(file_path)

  if File.exist?(local)
    local
  else
    download!(file_path, local)
  end
end

.local_path(file_path) ⇒ Object

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



56
57
58
59
60
61
62
# File 'lib/kube/schema/schema_cache.rb', line 56

def local_path(file_path)
  if file_path.end_with?(".json")
    raise "What are you doing???? don't put .json on the end...."
  else
    File.join(cache_dir, "#{file_path}.json")
  end
end

.read(file_path) ⇒ Object

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



51
52
53
# File 'lib/kube/schema/schema_cache.rb', line 51

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