Class: Kube::Cluster::Resource

Inherits:
Schema::Resource
  • Object
show all
Includes:
DirtyTracking, Persistence
Defined in:
lib/kube/cluster/resource.rb,
lib/kube/cluster/resource/persistence.rb,
lib/kube/cluster/resource/dirty_tracking.rb

Defined Under Namespace

Modules: DirtyTracking, Persistence

Constant Summary collapse

POD_BEARING_KINDS =
%w[
  Deployment
  StatefulSet
  DaemonSet
  Job
  CronJob
  ReplicaSet
].freeze
CLUSTER_SCOPED_KINDS =
%w[
  Namespace
  ClusterRole
  ClusterRoleBinding
  PersistentVolume
  StorageClass
  IngressClass
  CustomResourceDefinition
  PriorityClass
  RuntimeClass
  VolumeAttachment
  CSIDriver
  CSINode
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Persistence

#apply, #delete, #patch, #reload

Methods included from DirtyTracking

#changed, #changed?, #changes, #changes_applied, #method_missing, #patch_data, #respond_to_missing?

Constructor Details

#initialize(hash = {}, &block) ⇒ Resource

Returns a new instance of Resource.



38
39
40
41
42
# File 'lib/kube/cluster/resource.rb', line 38

def initialize(hash = {}, &block)
  @cluster = hash.delete(:cluster)
  super
  snapshot!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Kube::Cluster::Resource::DirtyTracking

Instance Attribute Details

#clusterObject

Returns the value of attribute cluster.



12
13
14
# File 'lib/kube/cluster/resource.rb', line 12

def cluster
  @cluster
end

Instance Method Details

#annotation(key) ⇒ Object

Read an annotation value from the resource.



56
57
58
59
# File 'lib/kube/cluster/resource.rb', line 56

def annotation(key)
  annotations = to_h.dig(:metadata, :annotations) || {}
  annotations[key.to_sym] || annotations[key.to_s]
end

#cluster_scoped?Boolean

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

Returns:

  • (Boolean)


73
74
75
# File 'lib/kube/cluster/resource.rb', line 73

def cluster_scoped?
  CLUSTER_SCOPED_KINDS.include?(kind)
end

#each_container(pod_spec, &block) ⇒ Object

Walk every container list in a pod spec (containers, initContainers) and yield each container hash.



89
90
91
92
93
94
95
# File 'lib/kube/cluster/resource.rb', line 89

def each_container(pod_spec, &block)
  return unless pod_spec

  [:containers, :initContainers].each do |key|
    Array(pod_spec[key]).each(&block)
  end
end

#kindObject

The resource kind as a String (e.g. “Deployment”).



62
63
64
65
# File 'lib/kube/cluster/resource.rb', line 62

def kind
  h = to_h
  (h[:kind] || h["kind"]).to_s
end

#label(key) ⇒ Object

Read a label value from the resource.



50
51
52
53
# File 'lib/kube/cluster/resource.rb', line 50

def label(key)
  labels = to_h.dig(:metadata, :labels) || {}
  labels[key.to_sym] || labels[key.to_s]
end

#pod_bearing?Boolean

Is this a resource that contains a pod template?

Returns:

  • (Boolean)


68
69
70
# File 'lib/kube/cluster/resource.rb', line 68

def pod_bearing?
  POD_BEARING_KINDS.include?(kind)
end

#pod_template(hash) ⇒ Object

Returns the pod template spec path from a resource hash, accounting for CronJob’s extra nesting.



79
80
81
82
83
84
85
# File 'lib/kube/cluster/resource.rb', line 79

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

#rebuild(hash) ⇒ Object

Build a new resource of the same schema subclass from a hash.



45
46
47
# File 'lib/kube/cluster/resource.rb', line 45

def rebuild(hash)
  self.class.new(hash)
end