Class: Kube::Schema::Resource
- Inherits:
-
Object
- Object
- Kube::Schema::Resource
- Defined in:
- lib/kube/schema/resource.rb
Class Method Summary collapse
-
.defaults ⇒ Object
Gets overridden by the factory in Kube::Schema::Instance.
-
.schema ⇒ Object
Gets overridden by the factory in Kube::Schema::Instance.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #apiVersion ⇒ Object
-
#initialize(hash = {}, &block) ⇒ Resource
constructor
A new instance of Resource.
- #kind ⇒ Object
- #metadata ⇒ Object
- #spec ⇒ Object
-
#to_h ⇒ Object
Returns the resource data as a Hash.
-
#to_yaml ⇒ Object
Serializes to clean Kubernetes YAML.
-
#valid! ⇒ Object
Like #valid? but raises Kube::ValidationError with details on failure.
- #valid? ⇒ Boolean
Constructor Details
#initialize(hash = {}, &block) ⇒ Resource
Returns a new instance of Resource.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/kube/schema/resource.rb', line 9 def initialize(hash = {}, &block) deep_symbolize_keys(self.class.defaults.to_h).then do |defaults| deep_symbolize_keys(hash).then do |symbolized| config = defaults.merge({ metadata: symbolized.delete(:metadata), spec: symbolized.delete(:spec), }) @data = BlackHoleStruct.new(config) end end if block_given? @data.instance_exec(&block) end end |
Class Method Details
.defaults ⇒ Object
Gets overridden by the factory in Kube::Schema::Instance. Returns a frozen Hash like { “apiVersion” => “apps/v1”, “kind” => “Deployment” }
37 |
# File 'lib/kube/schema/resource.rb', line 37 def self.defaults = nil |
.schema ⇒ Object
Gets overridden by the factory in Kube::Schema::Instance
33 |
# File 'lib/kube/schema/resource.rb', line 33 def self.schema = nil |
Instance Method Details
#==(other) ⇒ Object
91 92 93 |
# File 'lib/kube/schema/resource.rb', line 91 def ==(other) other.is_a?(Resource) && to_h == other.to_h end |
#apiVersion ⇒ Object
27 |
# File 'lib/kube/schema/resource.rb', line 27 def apiVersion = @data.apiVersion |
#kind ⇒ Object
28 |
# File 'lib/kube/schema/resource.rb', line 28 def kind = @data.kind |
#metadata ⇒ Object
30 |
# File 'lib/kube/schema/resource.rb', line 30 def = @data. |
#spec ⇒ Object
29 |
# File 'lib/kube/schema/resource.rb', line 29 def spec = @data.spec |
#to_h ⇒ Object
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.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/kube/schema/resource.rb', line 69 def to_h defaults = self.class.defaults data = @data.to_h 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_yaml ⇒ Object
Serializes to clean Kubernetes YAML. Raises Kube::ValidationError if the resource is not valid.
85 86 87 88 89 |
# File 'lib/kube/schema/resource.rb', line 85 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.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/kube/schema/resource.rb', line 49 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
39 40 41 42 43 44 45 |
# File 'lib/kube/schema/resource.rb', line 39 def valid? if self.class.schema.nil? true else self.class.schema.valid?(deep_stringify_keys(to_h)) end end |