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,
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

Instance Method Summary collapse

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.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/kube/cluster/resource.rb', line 41

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

#clusterObject

Returns the value of attribute cluster.



15
16
17
# File 'lib/kube/cluster/resource.rb', line 15

def cluster
  @cluster
end

Instance Method Details

#annotation(key) ⇒ Object

Read an annotation value from the resource.



84
85
86
87
# File 'lib/kube/cluster/resource.rb', line 84

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)


101
102
103
# File 'lib/kube/cluster/resource.rb', line 101

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.



117
118
119
120
121
122
123
# File 'lib/kube/cluster/resource.rb', line 117

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



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

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

#label(key) ⇒ Object

Read a label value from the resource.



78
79
80
81
# File 'lib/kube/cluster/resource.rb', line 78

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)


96
97
98
# File 'lib/kube/cluster/resource.rb', line 96

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.



107
108
109
110
111
112
113
# File 'lib/kube/cluster/resource.rb', line 107

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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kube/cluster/resource.rb', line 56

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