Module: Kube::Schema
- Defined in:
- lib/kube/schema.rb,
lib/kube/schema/version.rb,
lib/kube/schema/instance.rb,
lib/kube/schema/manifest.rb,
lib/kube/schema/resource.rb
Defined Under Namespace
Classes: Instance, Manifest, Resource
Constant Summary collapse
- GEM_ROOT =
File.("../..", __dir__).freeze
- SCHEMAS_DIR =
File.join(GEM_ROOT, "schemas").freeze
- DEFAULT_VERSION =
"1.34"- VERSION =
"1.3.2"
Class Attribute Summary collapse
-
.custom_schemas ⇒ Object
readonly
Custom schemas registered via Kube::Schema.register.
-
.schema_version ⇒ Object
Set a default Kubernetes version for bare lookups like Kube::Schema.
Class Method Summary collapse
-
.[](key) ⇒ Object
Kube::Schema => cached Instance (supports [“Deployment”] chaining) Kube::Schema => Resource via the default version.
- .has_version?(version) ⇒ Boolean
-
.latest_version ⇒ Object
The latest Kubernetes version available in the schemas directory.
-
.parse(hash) ⇒ Object
Build a Resource from a hash.
-
.register(kind, schema:, api_version:) ⇒ Object
Register a standalone JSON Schema for a custom resource kind.
-
.reset_custom_schemas! ⇒ Object
Remove all custom schema registrations.
-
.schema_versions ⇒ Array<String>
Available Kubernetes versions, read from the local schemas directory.
Class Attribute Details
.custom_schemas ⇒ Object (readonly)
Custom schemas registered via Kube::Schema.register. Keys are kind strings, values are { schema:, defaults: } hashes.
32 33 34 |
# File 'lib/kube/schema.rb', line 32 def custom_schemas @custom_schemas end |
.schema_version ⇒ Object
Set a default Kubernetes version for bare lookups like Kube::Schema. When nil, the DEFAULT_VERSION is used.
28 29 30 |
# File 'lib/kube/schema.rb', line 28 def schema_version @schema_version end |
Class Method Details
.[](key) ⇒ Object
Kube::Schema => cached Instance (supports [“Deployment”] chaining) Kube::Schema => Resource via the default version
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/kube/schema.rb', line 97 def [](key) if key.start_with?("v") && Gem::Version.correct?(key.sub("v", "")) raise Kube::IncorrectVersionFormat, "\nDon't preface the version with a \"v\"." \ "\nUse Kube::Schema[\"#{key.sub("v", "")}\"] instead." end if Gem::Version.correct?(key) if has_version?(key) @instances[key] ||= Instance.new(key) else raise Kube::UnknownVersionError.new( "\n#{key} is an unknown version..." + "\nAvailable: #{schema_versions.join(", ")}" ) end else version = schema_version || DEFAULT_VERSION @instances[version] ||= Instance.new(version) @instances[version][key] end end |
.has_version?(version) ⇒ Boolean
141 142 143 |
# File 'lib/kube/schema.rb', line 141 def has_version?(version) schema_versions.include?(version) end |
.latest_version ⇒ Object
The latest Kubernetes version available in the schemas directory.
137 138 139 |
# File 'lib/kube/schema.rb', line 137 def latest_version schema_versions.last end |
.parse(hash) ⇒ Object
122 123 124 |
# File 'lib/kube/schema.rb', line 122 def parse(hash) raise NotImplementedError end |
.register(kind, schema:, api_version:) ⇒ Object
Register a standalone JSON Schema for a custom resource kind.
This lets users add CRD schemas from any source — for example, the datreeio/CRDs-catalog, operator repos, or their own CRDs. Registered kinds take precedence over built-in definitions.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/kube/schema.rb', line 57 def register(kind, schema:, api_version:) require "json" require "json_schemer" parsed = case schema when Hash schema when String, Pathname path = schema.to_s if File.exist?(path) JSON.parse(File.read(path)) else JSON.parse(path) end else raise ArgumentError, "schema must be a Hash, a JSON string, or a file path — got #{schema.class}" end @custom_schemas[kind] = { schema: JSONSchemer.schema(parsed), defaults: { "apiVersion" => api_version, "kind" => kind }.freeze } # Invalidate cached resource classes on all instances so the # new registration takes effect immediately. @instances.each_value { |inst| inst.send(:clear_resource_cache!) } kind end |
.reset_custom_schemas! ⇒ Object
Remove all custom schema registrations. Useful for test teardown or resetting state.
90 91 92 93 |
# File 'lib/kube/schema.rb', line 90 def reset_custom_schemas! @custom_schemas.clear @instances.each_value { |inst| inst.send(:clear_resource_cache!) } end |
.schema_versions ⇒ Array<String>
Available Kubernetes versions, read from the local schemas directory.
129 130 131 132 133 134 |
# File 'lib/kube/schema.rb', line 129 def schema_versions @schema_versions ||= Dir.glob(File.join(SCHEMAS_DIR, "v*.json")).map do |file_path| File.basename(file_path, ".json").sub(/\Av/, "") end.sort_by { Gem::Version.new(_1) } end |