Class: Kube::Schema::SubSpec
- Inherits:
-
Object
- Object
- Kube::Schema::SubSpec
- Defined in:
- lib/kube/schema/sub_spec.rb
Overview
A lightweight, schema-validated wrapper for non-resource Kubernetes definitions — things like Container, ContainerPort, Volume, Probe, etc. that live inside resource specs but have no apiVersion/kind.
SubSpec validates against the OpenAPI JSON Schema definition and produces a plain Hash via #to_h, suitable for embedding directly inside Resource specs.
container = Kube::Schema::SubSpec["Container"].new {
name = "app"
image = "nginx:1.27"
ports = [{ containerPort: 80 }]
}
container.valid? # => true
container.to_h # => { name: "app", image: "nginx:1.27", ... }
SubSpec instances auto-coerce when placed inside a Resource —no explicit .to_h is needed:
spec.template.spec.containers = [container]
Class Method Summary collapse
- .[](name) ⇒ Object
- .definition_name ⇒ Object
-
.schema ⇒ Object
Gets overridden by the factory in Kube::Schema::Instance.
- .schema_properties ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(hash = {}, &block) ⇒ SubSpec
constructor
A new instance of SubSpec.
-
#to_h ⇒ Object
Returns the sub-spec data as a plain Hash.
-
#valid! ⇒ Object
Like #valid? but raises Kube::ValidationError with details on failure.
- #valid? ⇒ Boolean
Constructor Details
#initialize(hash = {}, &block) ⇒ SubSpec
Returns a new instance of SubSpec.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/kube/schema/sub_spec.rb', line 29 def initialize(hash = {}, &block) deep_symbolize_keys(hash).then do |symbolized| @data = {} self.class.schema_properties.each do |property| if symbolized.key?(property) @data[property] = symbolized.delete(property) end end end if block_given? @data.instance_exec(&block) end end |
Class Method Details
.[](name) ⇒ Object
102 103 104 105 106 |
# File 'lib/kube/schema/sub_spec.rb', line 102 def [](name) version = Schema.schema_version || Schema::DEFAULT_VERSION instance = Schema[version] instance.sub_spec(name) end |
.definition_name ⇒ Object
54 55 56 |
# File 'lib/kube/schema/sub_spec.rb', line 54 def self.definition_name raise "Kube::Schema::SubSpec should NOT be instantiated directly" end |
.schema ⇒ Object
Gets overridden by the factory in Kube::Schema::Instance
46 47 48 |
# File 'lib/kube/schema/sub_spec.rb', line 46 def self.schema raise "Kube::Schema::SubSpec should NOT be instantiated directly" end |
.schema_properties ⇒ Object
50 51 52 |
# File 'lib/kube/schema/sub_spec.rb', line 50 def self.schema_properties raise "Kube::Schema::SubSpec should NOT be instantiated directly" end |
Instance Method Details
#==(other) ⇒ Object
91 92 93 |
# File 'lib/kube/schema/sub_spec.rb', line 91 def ==(other) other.is_a?(SubSpec) && to_h == other.to_h end |
#to_h ⇒ Object
Returns the sub-spec data as a plain Hash.
86 87 88 89 |
# File 'lib/kube/schema/sub_spec.rb', line 86 def to_h data = deep_compact(@data) data.reject { |_, v| v.is_a?(Hash) && v.empty? } end |
#valid! ⇒ Object
Like #valid? but raises Kube::ValidationError with details on failure.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/kube/schema/sub_spec.rb', line 67 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? raise Kube::ValidationError.new(errors, kind: self.class.definition_name, manifest: data ) end true end end |
#valid? ⇒ Boolean
58 59 60 61 62 63 64 |
# File 'lib/kube/schema/sub_spec.rb', line 58 def valid? if self.class.schema.nil? true else self.class.schema.valid?(deep_stringify_keys(to_h)) end end |