class SubSpec
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]
Nested
Definitions
def self.schema
Gets overridden by the factory in Kube::Schema::Instance
Implementation
def self.schema
raise "Kube::Schema::SubSpec should NOT be instantiated directly"
end
def valid!
Like #valid? but raises Kube::ValidationError with details on failure.
Implementation
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
def to_h
Returns the sub-spec data as a plain Hash.
Implementation
def to_h
data = deep_compact(@data)
data.reject { |_, v| v.is_a?(Hash) && v.empty? }
end