› kube_cluster › Source › Kube › Cluster › Resource

class Resource

Nested

Definitions

def rebuild(hash = {})

Build a new resource of the same subclass.

Example: self.class.new(**hash) would raise for a subclass like this

class ExampleServiceSubclass < Kube::Cluster["Service"]
  def initialize(name:, port:, **options, &block)
    super() { metadata.name = name }
  end
end

Signature

parameter hash Hash

the resource data, with :kind and :apiVersion.

returns Resource

a new instance looked up via Kube::Cluster[].

Implementation

def rebuild(hash = {})
  kind = hash.delete(:kind).to_s
  api_version = hash.delete(:apiVersion)&.to_s
  if api_version
    lookup = "#{api_version}/#{kind}"
  else
    lookup = kind
  end
  Kube::Cluster[lookup].new(**hash)
end

def label(key)

Read a label value from the resource.

Signature

parameter key String, Symbol

the label key.

returns String, nil

the label value, if set.

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.

Signature

parameter key String, Symbol

the annotation key.

returns String, nil

the annotation value, if set.

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").

Signature

returns String

the resource kind.

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?

Signature

returns Boolean

true for Deployment, Job, CronJob, etc.

Implementation

def pod_bearing?
  POD_BEARING_KINDS.include?(kind)
end

def cluster_scoped?

Is this a cluster-scoped resource (no namespace)?

Signature

returns Boolean

true for Namespace, ClusterRole, etc.

Implementation

def cluster_scoped?
  CLUSTER_SCOPED_KINDS.include?(kind)
end

def pod_template(hash)

The pod template spec path, accounting for CronJob's nesting.

Signature

parameter hash Hash

a resource hash.

returns Hash, nil

the pod spec at that path.

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 containers and initContainers, yielding each container.

Signature

parameter pod_spec Hash, nil

a pod spec.

yields {|container| ...}

each container hash.

Implementation

def each_container(pod_spec, &block)
  if pod_spec
    [:containers, :initContainers].each do |key|
      Array(pod_spec[key]).each(&block)
    end
  else
    nil
  end
end