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.
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.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/kube/schema/instance.rb', line 42 def [](kind) @resource_classes[kind] ||= begin 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]}") defaults = entry[:defaults].freeze Class.new(::Kube::Schema::Resource) do @schema = ref_schema @defaults = defaults def self.schema @schema || superclass.schema end def self.defaults @defaults || superclass.defaults end end end end |
#list_resources ⇒ Array<String>
All available resource kinds for this version.
72 73 74 |
# File 'lib/kube/schema/instance.rb', line 72 def list_resources gvk_index.keys.sort end |