class Resource
Nested
Definitions
def rebuild(hash = {})
Build a new resource of the same schema subclass from a hash.
Implementation
def rebuild(hash = {})
# self.class.new(**hash) would throw an error if you do something like this:
#
# class ExampleServiceSubclass < Kube::Cluster["Service"]
# def initialize(name:, port:, **options, &block)
# super() {
# metadata.name = name
# metadata.labels = { "app" => name }
# spec.selector = { "app" => name }
# spec.ports = [{ port: port, targetPort: port, protocol: "TCP" }]
# instance_exec(&block) if block_given?
# }
# end
# end
#
# Therefore we must make sure that we're rebuilding from the
# initial Kube::Cluster object instead... NOT Kube::Schema...
#
Kube::Cluster[hash.delete(:kind).to_s].new(**hash)
end
def label(key)
Read a label value from the resource.
Implementation
def label(key)
labels = to_h.dig(:metadata, :labels) || {}
labels[key.to_sym] || labels[key.to_s]
end
def annotation(key)
Read an annotation value from the resource.
Implementation
def annotation(key)
annotations = to_h.dig(:metadata, :annotations) || {}
annotations[key.to_sym] || annotations[key.to_s]
end
def kind
The resource kind as a String (e.g. "Deployment").
Implementation
def kind
h = to_h
(h[:kind] || h["kind"]).to_s
end
def pod_bearing?
Is this a resource that contains a pod template?
Implementation
def pod_bearing?
POD_BEARING_KINDS.include?(kind)
end
def cluster_scoped?
Is this a cluster-scoped resource (no namespace)?
Implementation
def cluster_scoped?
CLUSTER_SCOPED_KINDS.include?(kind)
end
def pod_template(hash)
Returns the pod template spec path from a resource hash, accounting for CronJob's extra nesting.
Implementation
def pod_template(hash)
if (hash[:kind] || hash["kind"]).to_s == "CronJob"
hash.dig(:spec, :jobTemplate, :spec, :template, :spec)
else
hash.dig(:spec, :template, :spec)
end
end
def each_container(pod_spec, &block)
Walk every container list in a pod spec (containers, initContainers) and yield each container hash.
Implementation
def each_container(pod_spec, &block)
return unless pod_spec
[:containers, :initContainers].each do |key|
Array(pod_spec[key]).each(&block)
end
end