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
-
#[](input) ⇒ Object
Look up a resource by kind or full GVK string.
-
#initialize(version) ⇒ Instance
constructor
A new instance of Instance.
-
#list_definitions ⇒ Array<String>
All available definition short names for this version.
-
#list_resources ⇒ Array<String>
All available resource kinds for this version, including any custom schemas registered via Kube::Schema.register.
-
#sub_spec(name) ⇒ Object
Look up a sub-spec definition by short name (e.g. “Container”, “ContainerPort”, “Volume”, “Probe”).
Constructor Details
#initialize(version) ⇒ Instance
Returns a new instance of Instance.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/kube/schema/instance.rb', line 28 def initialize(version) @resource_classes = {} @sub_spec_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
#[](input) ⇒ Object
Look up a resource by kind or full GVK string.
Accepts:
instance["Deployment"] — kind-only lookup
instance["apps/v1/Deployment"] — group/version/kind
instance["v1/Pod"] — version/kind (core, empty group)
instance["networking.k8s.io/v1/Ingress"] — fully qualified
Returns a class that inherits from Kube::Schema::Resource.
Custom schemas registered via Kube::Schema.register take precedence over built-in definitions (kind-only lookups only).
53 54 55 56 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 87 88 89 90 91 92 93 94 |
# File 'lib/kube/schema/instance.rb', line 53 def [](input) @resource_classes[input] ||= begin if input.include?("/") parts = input.split("/") case parts.length when 3 group, version, kind = parts when 2 group = "" version, kind = parts else raise "Invalid GVK format: #{input.inspect}." \ "\nExpected \"group/version/kind\" or \"version/kind\"." end # Custom schemas take precedence on full-GVK lookups too. custom = find_custom_entry_by_gvk(group, version, kind) if custom return build_resource_class(custom[:schema], custom[:defaults]) end entry = find_gvk_entry_by_full_gvk(group, version, kind) else # Kind-only lookup — custom schemas take precedence. custom = find_custom_entry(input) if custom return build_resource_class(custom[:schema], custom[:defaults]) end entry = find_gvk_entry(input) end if entry.nil? raise "No resource schema found for #{input.inspect}!" \ "\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 |
#list_definitions ⇒ Array<String>
All available definition short names for this version.
132 133 134 135 136 |
# File 'lib/kube/schema/instance.rb', line 132 def list_definitions schemer.value.fetch("definitions", {}).keys .map { |k| k.split(".").last } .uniq.sort end |
#list_resources ⇒ Array<String>
All available resource kinds for this version, including any custom schemas registered via Kube::Schema.register.
100 101 102 103 |
# File 'lib/kube/schema/instance.rb', line 100 def list_resources custom_kinds = Schema.custom_schemas.values.map { |v| v[:defaults]["kind"] } (gvk_index.keys + custom_kinds).uniq.sort end |
#sub_spec(name) ⇒ Object
Look up a sub-spec definition by short name (e.g. “Container”, “ContainerPort”, “Volume”, “Probe”). Returns a class that inherits from Kube::Schema::SubSpec.
Also accepts a full definition key like “io.k8s.api.core.v1.Container” for disambiguation.
instance.sub_spec("Container") # => SubSpec subclass
instance.sub_spec("ContainerPort") # => SubSpec subclass
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/kube/schema/instance.rb', line 115 def sub_spec(name) @sub_spec_classes[name] ||= begin definition_key = find_definition_key(name) if definition_key.nil? raise "No definition found for #{name}!" \ "\nUse #list_definitions to see available definitions for v#{version}." end ref_schema = schemer.ref("#/definitions/#{definition_key}") build_sub_spec_class(ref_schema, name) end end |