kube_schema 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "ostruct"
4
- require "black_hole_struct"
5
-
6
- module KubeSchema
7
- class Resource
8
-
9
- # Subclasses generated by Instance#[] have a class-level .schema.
10
- # When called directly via KubeSchema.parse(hash), the hash is passed in.
11
- def initialize(hash = nil, &block)
12
- @data = BlackHoleStruct.new(hash)
13
- @data.instance_exec(&block) if block
14
- end
15
-
16
- # Gets overridden by the factory in KubeSchema::Instance
17
- def self.schema
18
- nil
19
- end
20
-
21
- def to_h
22
- @data.to_h
23
- end
24
-
25
- def ==(other)
26
- other.is_a?(Resource) && to_h == other.to_h
27
- end
28
- end
29
- end
@@ -1,91 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'fileutils'
4
- require 'net/http'
5
- require 'uri'
6
-
7
- module KubeSchema
8
- # Lazily downloads individual schema JSON files from the schemas branch
9
- # on GitHub and caches them locally under Gem.cache_home (XDG_CACHE_HOME).
10
- #
11
- # Usage:
12
- # KubeSchema::SchemaCache.fetch("cert-manager.io/certificate_v1")
13
- # # => "/home/user/.cache/kube_schema/schemas/cert-manager.io/certificate_v1.json"
14
- #
15
- # KubeSchema::SchemaCache.read("v1.33.6/deployment")
16
- # # => "{...json contents...}"
17
- #
18
- module SchemaCache
19
- BASE_URL = 'https://raw.githubusercontent.com/general-intelligence-systems/kube_schema/refs/heads/schemas'
20
-
21
- class DownloadError < StandardError; end
22
-
23
- class << self
24
- # Root directory for cached schemas.
25
- # Defaults to ~/.cache/kube_schema/schemas (or $XDG_CACHE_HOME/kube_schema/schemas).
26
- def cache_dir
27
- @cache_dir ||= File.join(Gem.cache_home, 'kube_schema', 'schemas')
28
- end
29
-
30
- # Override the cache directory (useful for testing).
31
- attr_writer :cache_dir
32
-
33
- # Returns the local file path for a schema, downloading it if not cached.
34
- # The file_path should NOT include the .json extension.
35
- #
36
- # SchemaCache.fetch("cert-manager.io/certificate_v1")
37
- # # => "/home/user/.cache/kube_schema/schemas/cert-manager.io/certificate_v1.json"
38
- #
39
- def fetch(file_path)
40
- local = local_path(file_path)
41
- return local if File.exist?(local)
42
-
43
- download!(file_path, local)
44
- local
45
- end
46
-
47
- # Returns the JSON content as a String, downloading if necessary.
48
- def read(file_path)
49
- File.read(fetch(file_path))
50
- end
51
-
52
- # Returns the local path where a schema would be cached (without downloading).
53
- def local_path(file_path)
54
- File.join(cache_dir, "#{file_path}.json")
55
- end
56
-
57
- # Returns true if the schema is already cached locally.
58
- def cached?(file_path)
59
- File.exist?(local_path(file_path))
60
- end
61
-
62
- # Removes a single cached schema file.
63
- def evict(file_path)
64
- path = local_path(file_path)
65
- File.delete(path) if File.exist?(path)
66
- end
67
-
68
- # Removes the entire cache directory.
69
- def clear!
70
- FileUtils.rm_rf(cache_dir)
71
- end
72
-
73
- private
74
-
75
- def download!(file_path, local)
76
- url = "#{BASE_URL}/#{file_path}.json"
77
- uri = URI.parse(url)
78
-
79
- response = Net::HTTP.get_response(uri)
80
-
81
- unless response.is_a?(Net::HTTPSuccess)
82
- raise DownloadError,
83
- "Failed to download schema: #{url} (HTTP #{response.code})"
84
- end
85
-
86
- FileUtils.mkdir_p(File.dirname(local))
87
- File.write(local, response.body)
88
- end
89
- end
90
- end
91
- end
@@ -1,27 +0,0 @@
1
- module KubeSchema
2
- class SchemaIndex
3
- def initialize(version)
4
- @version = version
5
- end
6
-
7
- def find(query)
8
- all_paths.select { _1.include?(query.downcase) }.first
9
- end
10
-
11
- def all_paths
12
- custom_resource_paths + kubernetes_paths
13
- end
14
-
15
- def custom_resource_paths
16
- File.read(SCHEMA_INDEX + "/crds.txt").lines.map do |line|
17
- line.chomp.gsub(".json", "")
18
- end
19
- end
20
-
21
- def kubernetes_paths
22
- File.read(SCHEMA_INDEX + "/v#{@version}.txt").lines.map do |line|
23
- line.chomp.gsub(".json", "").split("/")[1..-1].join("/")
24
- end
25
- end
26
- end
27
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module KubeSchema
4
- VERSION = "1.0.0"
5
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module KubeSchema
4
- VERSION = "<%= version %>"
5
- end
data/lib/kube_schema.rb DELETED
@@ -1,67 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'kube_schema/version'
4
- require_relative 'kube_schema/resource'
5
- require_relative 'kube_schema/instance'
6
- require_relative 'kube_schema/schema_cache'
7
- require_relative 'kube_schema/schema_index'
8
-
9
- module KubeSchema
10
- class UnknownVersionError < StandardError; end
11
-
12
- @schema_version = nil
13
- @instances = {}
14
-
15
- GEM_ROOT = File.expand_path("..", __dir__).freeze
16
- SCHEMA_INDEX = File.join(GEM_ROOT, "data").freeze
17
- DEFAULT_VERSION = "1.34.4" # 2025-04-16
18
-
19
- class << self
20
- # Set a default Kubernetes version for bare lookups like KubeSchema["Deployment"].
21
- # When nil, the latest version found in the schemas directory is used.
22
- attr_accessor :schema_version
23
-
24
- # KubeSchema["1.33.6"] => cached Instance (supports ["Deployment"] chaining)
25
- # KubeSchema["Deployment"] => Resource via the default version
26
- # KubeSchema["apps/v1/Deployment"] => Resource via the default version
27
- def [](key)
28
- is_a_version = -> (key) { Gem::Version.correct?(key) }
29
-
30
- if is_a_version.(key)
31
- if has_version?(key)
32
- @instances[key] ||= Instance.new(key)
33
- else
34
- raise UnknownVersionError.new(
35
- "\n#{key} is an unknown version..." +
36
- "\nUse `KubeSchema.schema_versions` to get a list."
37
- )
38
- end
39
- else
40
- Instance.new(schema_version || DEFAULT_VERSION)[key]
41
- end
42
- end
43
-
44
- # Build a Resource from a hash.
45
- # KubeSchema.parse(KubeSchema["Deployment"].to_h) == KubeSchema["Deployment"]
46
- def parse(hash)
47
- raise NotImplementedError
48
- end
49
-
50
- def schema_versions
51
- @schema_versions ||=
52
- Dir.glob(SCHEMA_INDEX + "/v*.txt").map do |file_path|
53
- file_path.split("/").last.gsub(".txt", "")[1..-1]
54
- end.sort_by { Gem::Version.new(_1) }
55
- end
56
-
57
- # The latest Kubernetes version available in the schemas directory,
58
- # determined by sorting the filenames with Gem::Version.
59
- def latest_version
60
- schema_versions.last
61
- end
62
-
63
- def has_version?(version)
64
- schema_versions.include?(version)
65
- end
66
- end
67
- end