Class: Kube::Schema::Instance
- Inherits:
-
Object
- Object
- Kube::Schema::Instance
- Defined in:
- lib/kube/schema/instance.rb
Overview
Represents a single Kubernetes version’s OpenAPI schema. Lazily loads the Swagger JSON once per version (shared across instances) and builds resource classes by kind.
instance = Kube::Schema::Instance.new("1.34")
instance["Deployment"] # => Resource subclass
instance["NetworkPolicy"] # => Resource subclass
instance.list_resources # => sorted array of kind strings
Class Attribute Summary collapse
-
.schemers ⇒ Object
readonly
Returns the value of attribute schemers.
Instance Attribute Summary collapse
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#[](kind) ⇒ Object
Look up a resource by kind (e.g. “Deployment”, “NetworkPolicy”).
-
#initialize(version) ⇒ Instance
constructor
A new instance of Instance.
-
#list_resources ⇒ Array<String>
All available resource kinds for this version, including any custom schemas registered via Kube::Schema.register.
Constructor Details
#initialize(version) ⇒ Instance
Returns a new instance of Instance.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/kube/schema/instance.rb', line 28 def initialize(version) @resource_classes = {} unless Gem::Version.correct?(version) raise UnknownVersionError, "\n#{version} is not a valid version string." \ "\nUse `Kube::Schema.schema_versions` to get a list." end @version = version end |
Class Attribute Details
.schemers ⇒ Object (readonly)
Returns the value of attribute schemers.
25 26 27 |
# File 'lib/kube/schema/instance.rb', line 25 def schemers @schemers end |
Instance Attribute Details
#version ⇒ Object (readonly)
Returns the value of attribute version.
18 19 20 |
# File 'lib/kube/schema/instance.rb', line 18 def version @version end |
Instance Method Details
#[](kind) ⇒ Object
Look up a resource by kind (e.g. “Deployment”, “NetworkPolicy”). Returns a class that inherits from Kube::Schema::Resource.
Custom schemas registered via Kube::Schema.register take precedence over built-in definitions, allowing users to override or extend the schema for any kind.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/kube/schema/instance.rb', line 46 def [](kind) @resource_classes[kind] ||= begin # Custom schemas win over built-in definitions. custom = find_custom_entry(kind) if custom build_resource_class(custom[:schema], custom[:defaults]) else entry = find_gvk_entry(kind) if entry.nil? raise "No resource schema found for #{kind}!" \ "\nUse #list_resources to see available kinds for v#{version}." end ref_schema = schemer.ref("#/definitions/#{entry[:definition_key]}") build_resource_class(ref_schema, entry[:defaults].freeze) end end end |
#list_resources ⇒ Array<String>
All available resource kinds for this version, including any custom schemas registered via Kube::Schema.register.
70 71 72 |
# File 'lib/kube/schema/instance.rb', line 70 def list_resources (gvk_index.keys + Schema.custom_schemas.keys).uniq.sort end |