Class: K8s::Resource
- Inherits:
-
RecursiveOpenStruct
- Object
- RecursiveOpenStruct
- K8s::Resource
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/k8s/resource.rb
Overview
generic untyped resource
Class Method Summary collapse
- .from_file(filename) ⇒ K8s::Resource
- .from_files(path) ⇒ Array<K8s::Resource>
- .from_json(data) ⇒ self
Instance Method Summary collapse
- #<=>(other) ⇒ Boolean
- #can_patch?(config_annotation) ⇒ Boolean
- #checksum ⇒ Object
-
#current_config(config_annotation) ⇒ Hash
Gets the existing resources (on kube api) configuration, an empty hash if not present.
-
#initialize(hash, recurse_over_arrays: true, **options) ⇒ Resource
constructor
A new instance of Resource.
-
#merge(attrs) ⇒ K8s::Resource
merge in fields.
- #merge_patch_ops(attrs, config_annotation) ⇒ Object
- #stringify_hash(hash) ⇒ Object
- #to_json(**options) ⇒ String
Constructor Details
#initialize(hash, recurse_over_arrays: true, **options) ⇒ Resource
Returns a new instance of Resource.
37 38 39 40 41 42 |
# File 'lib/k8s/resource.rb', line 37 def initialize(hash, recurse_over_arrays: true, **) super(hash, recurse_over_arrays: recurse_over_arrays, ** ) end |
Class Method Details
.from_file(filename) ⇒ K8s::Resource
19 20 21 |
# File 'lib/k8s/resource.rb', line 19 def self.from_file(filename) new(YAML.load_file(filename)) end |
.from_files(path) ⇒ Array<K8s::Resource>
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/k8s/resource.rb', line 25 def self.from_files(path) stat = File.stat(path) if stat.directory? # recurse Dir.glob("#{path}/*.{yml,yaml}").sort.map { |dir| self.from_files(dir) }.flatten else ::YAML.load_stream(File.read(path), path).map{|doc| new(doc) } end end |
.from_json(data) ⇒ self
13 14 15 |
# File 'lib/k8s/resource.rb', line 13 def self.from_json(data) return new(data) end |
Instance Method Details
#<=>(other) ⇒ Boolean
51 52 53 |
# File 'lib/k8s/resource.rb', line 51 def <=>(other) to_hash <=> other.to_hash end |
#can_patch?(config_annotation) ⇒ Boolean
91 92 93 |
# File 'lib/k8s/resource.rb', line 91 def can_patch?(config_annotation) !!self..annotations&.dig(config_annotation) end |
#checksum ⇒ Object
69 70 71 |
# File 'lib/k8s/resource.rb', line 69 def checksum @checksum ||= Digest::MD5.hexdigest(Marshal::dump(to_hash)) end |
#current_config(config_annotation) ⇒ Hash
Gets the existing resources (on kube api) configuration, an empty hash if not present
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/k8s/resource.rb', line 80 def current_config(config_annotation) current_cfg = self..annotations&.dig(config_annotation) return {} unless current_cfg current_hash = JSON.parse(current_cfg) # kubectl adds empty metadata.namespace, let's fix it current_hash['metadata'].delete('namespace') if current_hash.dig('metadata', 'namespace').to_s.empty? current_hash end |
#merge(attrs) ⇒ K8s::Resource
merge in fields
59 60 61 62 63 64 65 66 67 |
# File 'lib/k8s/resource.rb', line 59 def merge(attrs) # deep clone of attrs h = to_hash # merge in-place h.deep_merge!(attrs.to_hash, overwrite_arrays: true, merge_nil_values: true) self.class.new(h) end |
#merge_patch_ops(attrs, config_annotation) ⇒ Object
73 74 75 |
# File 'lib/k8s/resource.rb', line 73 def merge_patch_ops(attrs, config_annotation) Util.json_patch(current_config(config_annotation), stringify_hash(attrs)) end |
#stringify_hash(hash) ⇒ Object
95 96 97 |
# File 'lib/k8s/resource.rb', line 95 def stringify_hash(hash) JSON.load(JSON.dump(hash)) end |
#to_json(**options) ⇒ String
45 46 47 |
# File 'lib/k8s/resource.rb', line 45 def to_json(**) to_hash.to_json(**) end |