Class: Kube::Cluster::Resource
- Inherits:
-
Schema::Resource
- Object
- Schema::Resource
- Kube::Cluster::Resource
- Includes:
- DirtyTracking, Persistence
- Defined in:
- lib/kube/cluster/resource.rb,
lib/kube/cluster/resource/persistence.rb,
lib/kube/cluster/resource/dirty_tracking.rb,
lib/kube/cluster/resource/extensions/custom_resource_definition.rb
Defined Under Namespace
Modules: DirtyTracking, Extensions, 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
-
#cluster ⇒ Object
Returns the value of attribute cluster.
Instance Method Summary collapse
-
#annotation(key) ⇒ Object
Read an annotation value from the resource.
-
#cluster_scoped? ⇒ Boolean
Is this a cluster-scoped resource (no namespace)?.
-
#each_container(pod_spec, &block) ⇒ Object
Walk every container list in a pod spec (containers, initContainers) and yield each container hash.
-
#initialize(hash = {}, &block) ⇒ Resource
constructor
A new instance of Resource.
-
#kind ⇒ Object
The resource kind as a String (e.g. “Deployment”).
-
#label(key) ⇒ Object
Read a label value from the resource.
-
#pod_bearing? ⇒ Boolean
Is this a resource that contains a pod template?.
-
#pod_template(hash) ⇒ Object
Returns the pod template spec path from a resource hash, accounting for CronJob’s extra nesting.
-
#rebuild(hash) ⇒ Object
Build a new resource of the same schema subclass from a hash.
Methods included from Persistence
#apply, #delete, #name, #patch, #persisted?, #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.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/kube/cluster/resource.rb', line 39 def initialize(hash = {}, &block) @cluster = hash.delete(:cluster) super snapshot! begin extend Object.const_get( "Kube::Cluster::Resource::Extensions::#{kind}" ) rescue nil end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Kube::Cluster::Resource::DirtyTracking
Instance Attribute Details
#cluster ⇒ Object
Returns the value of attribute cluster.
13 14 15 |
# File 'lib/kube/cluster/resource.rb', line 13 def cluster @cluster end |
Instance Method Details
#annotation(key) ⇒ Object
Read an annotation value from the resource.
65 66 67 68 |
# File 'lib/kube/cluster/resource.rb', line 65 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)?
82 83 84 |
# File 'lib/kube/cluster/resource.rb', line 82 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.
98 99 100 101 102 103 104 |
# File 'lib/kube/cluster/resource.rb', line 98 def each_container(pod_spec, &block) return unless pod_spec [:containers, :initContainers].each do |key| Array(pod_spec[key]).each(&block) end end |
#kind ⇒ Object
The resource kind as a String (e.g. “Deployment”).
71 72 73 74 |
# File 'lib/kube/cluster/resource.rb', line 71 def kind h = to_h (h[:kind] || h["kind"]).to_s end |
#label(key) ⇒ Object
Read a label value from the resource.
59 60 61 62 |
# File 'lib/kube/cluster/resource.rb', line 59 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?
77 78 79 |
# File 'lib/kube/cluster/resource.rb', line 77 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.
88 89 90 91 92 93 94 |
# File 'lib/kube/cluster/resource.rb', line 88 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.
54 55 56 |
# File 'lib/kube/cluster/resource.rb', line 54 def rebuild(hash) self.class.new(hash) end |