Class: Kube::Schema::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/kube/schema/instance.rb

Overview

Represents a single Kubernetes version’s OpenAPI schema. Lazily loads the JSON and builds an index of group/version/kind strings mapped back to definition keys.

instance = Kube::Schema::Instance.new("v1.33.6")
instance["Deployment"]
instance["apps/v1/Deployment"]
instance.list_resources  #=> sorted array of "group/version/kind" strings

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Instance

Returns a new instance of Instance.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kube/schema/instance.rb', line 19

def initialize(version)
  @resource_classes = {}

  is_a_version = -> (v) { Gem::Version.correct?(v) }

  if is_a_version.(version)
    @version = version
  else
    raise UnknownVersionError.new(
      "\n#{version} is an unknown version..." +
      "\nUse `Kube::Schema.schema_versions` to get a list."
    )
  end
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



17
18
19
# File 'lib/kube/schema/instance.rb', line 17

def version
  @version
end

Instance Method Details

#[](key) ⇒ Object

Look up a resource by full “group/version/kind” or by bare “Kind”. Returns a class that inherits from Kube::Schema::Resource, or nil.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kube/schema/instance.rb', line 36

def [](key)
  Kube::Schema::SchemaIndex.new(version).find(key.downcase).then do |path|
    if path.nil?
      raise "No resource schema found for #{key}!!!!!!!"
    else
      @resource_classes[key] ||= begin
        schema_hash = JSON.parse(Kube::Schema::SchemaCache.read(path))

        Class.new(::Kube::Schema::Resource) do
          @schema = schema_hash

          def self.schema
            @schema || superclass.schema
          end
        end
      end
    end
  end
end