Class: Kube::Schema::Resource

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, &block) ⇒ Resource

Returns a new instance of Resource.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kube/schema/resource.rb', line 7

def initialize(hash = {}, &block)
  deep_symbolize_keys(self.class.defaults.to_h).then do |defaults|

    # You are NEVER allowed to change `apiVersion` or `kind`
    # Therefore, they are ONLY ever set from the self.defaults
    # property.
    deep_symbolize_keys(hash).then do |symbolized|
      config = defaults.merge({
        metadata: symbolized.delete(:metadata) || {},
        spec: symbolized.delete(:spec) || {},
      })

      @data = config
    end
  end

  if block_given?
    @data.instance_exec(&block)
  end
end

Class Method Details

.defaultsObject

Gets overridden by the factory in Kube::Schema::Instance. Returns a frozen Hash like { “apiVersion” => “apps/v1”, “kind” => “Deployment” }



38
# File 'lib/kube/schema/resource.rb', line 38

def self.defaults = nil

.schemaObject

Gets overridden by the factory in Kube::Schema::Instance



34
# File 'lib/kube/schema/resource.rb', line 34

def self.schema = nil

Instance Method Details

#==(other) ⇒ Object



92
93
94
# File 'lib/kube/schema/resource.rb', line 92

def ==(other)
  other.is_a?(Resource) && to_h == other.to_h
end

#apiVersionObject



28
# File 'lib/kube/schema/resource.rb', line 28

def apiVersion = @data.apiVersion

#kindObject



29
# File 'lib/kube/schema/resource.rb', line 29

def kind       = @data.kind

#metadataObject



31
# File 'lib/kube/schema/resource.rb', line 31

def    = @data.

#specObject



30
# File 'lib/kube/schema/resource.rb', line 30

def spec       = @data.spec

#to_hObject

Returns the resource data as a Hash. Defaults (apiVersion, kind) from the schema are authoritative and cannot be overridden – they are facts derived from the GVK metadata.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/kube/schema/resource.rb', line 70

def to_h
  defaults = self.class.defaults
  data = @data.reject { |_, v| v.is_a?(Hash) && v.empty? }

  if defaults
    symbolized = deep_symbolize_keys(defaults)
    # Defaults go first (for key ordering), then user data minus
    # any attempts to override the authoritative keys.
    symbolized.merge(data.reject { |k, _| symbolized.key?(k) })
  else
    data
  end
end

#to_yamlObject

Serializes to clean Kubernetes YAML. Raises Kube::ValidationError if the resource is not valid.



86
87
88
89
90
# File 'lib/kube/schema/resource.rb', line 86

def to_yaml
  if valid!
    deep_stringify_keys(to_h).to_yaml
  end
end

#valid!Object

Like #valid? but raises Kube::ValidationError with details on failure. The error message includes the resource kind and name for context.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kube/schema/resource.rb', line 50

def valid!
  if self.class.schema.nil?
    true
  else
    data = deep_stringify_keys(to_h)
    errors = self.class.schema.validate(data).to_a

    unless errors.empty?
      kind = self.class.defaults&.dig("kind")
      name = data.dig("metadata", "name")
      raise Kube::ValidationError.new(errors, kind: kind, name: name, manifest: data)
    end

    true
  end
end

#valid?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/kube/schema/resource.rb', line 40

def valid?
  if self.class.schema.nil?
    true
  else
    self.class.schema.valid?(deep_stringify_keys(to_h))
  end
end