Class: K8s::Resource

Inherits:
RecursiveOpenStruct
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/k8s/resource.rb

Overview

generic untyped resource

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, options = {}) ⇒ Resource

Returns a new instance of Resource.

Parameters:

  • hash (Hash)
  • options (Hash) (defaults to: {})

    see RecursiveOpenStruct#initialize



44
45
46
47
# File 'lib/k8s/resource.rb', line 44

def initialize(hash, options = {})
  options_with_defaults = { recurse_over_arrays: true }.merge(options)
  super(hash, options_with_defaults)
end

Class Method Details

.from_file(filename) ⇒ K8s::Resource

Parameters:

  • filename (String)

    file path

Returns:



25
26
27
# File 'lib/k8s/resource.rb', line 25

def self.from_file(filename)
  new(YAML.safe_load(File.read(filename), permitted_classes: [], permitted_symbols: [], aliases: true, filename: filename))
end

.from_files(path) ⇒ Array<K8s::Resource>

Parameters:

  • path (String)

    file path

Returns:



31
32
33
34
35
36
37
38
39
40
# File 'lib/k8s/resource.rb', line 31

def self.from_files(path)
  stat = File.stat(path)

  if stat.directory?
    # recurse
    Dir.glob("#{path}/*.{yml,yaml}").sort.map { |dir| from_files(dir) }.flatten
  else
    YAML.safe_load_stream(File.read(path), path).map{ |doc| new(doc) }
  end
end

.from_json(data) ⇒ self

Parameters:

  • data (String)

Returns:

  • (self)


19
20
21
# File 'lib/k8s/resource.rb', line 19

def self.from_json(data)
  new(Yajl::Parser.parse(data))
end

Instance Method Details

#<=>(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


57
58
59
# File 'lib/k8s/resource.rb', line 57

def <=>(other)
  to_hash <=> other.to_hash
end

#can_patch?(config_annotation) ⇒ Boolean

Parameters:

  • config_annotation (String)

Returns:

  • (Boolean)


104
105
106
# File 'lib/k8s/resource.rb', line 104

def can_patch?(config_annotation)
  !!.annotations&.dig(config_annotation)
end

#checksumString

Returns:

  • (String)


76
77
78
# File 'lib/k8s/resource.rb', line 76

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

Parameters:

  • config_annotation (String)

Returns:

  • (Hash)


91
92
93
94
95
96
97
98
99
100
# File 'lib/k8s/resource.rb', line 91

def current_config(config_annotation)
  current_cfg = .annotations&.dig(config_annotation)
  return {} unless current_cfg

  current_hash = Yajl::Parser.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

Parameters:

Returns:



65
66
67
68
69
70
71
72
73
# File 'lib/k8s/resource.rb', line 65

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) ⇒ Hash

Parameters:

  • attrs (Hash)
  • config_annotation (String)

Returns:

  • (Hash)


83
84
85
# File 'lib/k8s/resource.rb', line 83

def merge_patch_ops(attrs, config_annotation)
  Util.json_patch(current_config(config_annotation), stringify_hash(attrs))
end

#stringify_hash(hash) ⇒ Hash

Parameters:

  • hash (Hash)

Returns:

  • (Hash)


110
111
112
# File 'lib/k8s/resource.rb', line 110

def stringify_hash(hash)
  Yajl::Parser.parse(JSON.dump(hash))
end

#to_json(*args, **options) ⇒ String

Parameters:

  • args (Array)

    see Hash#to_json

Returns:

  • (String)


51
52
53
# File 'lib/k8s/resource.rb', line 51

def to_json(*args, **options)
  to_hash.to_json(*args, **options)
end