Class: K8s::Resource

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

Overview

generic untyped resource

Direct Known Subclasses

WatchEvent

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Resource.

Parameters:

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

    see RecursiveOpenStruct#initialize



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

def initialize hash, options = {}
  super(
    hash.is_a?(Hash) ? hash : hash.to_h,
    options
  )
end

Class Method Details

.default_optionsObject



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

def self.default_options
  {
    mutate_input_hash: false,
    recurse_over_arrays: true,
    preserve_original_keys: false
  }
end

.from_file(filename) ⇒ K8s::Resource

Parameters:

  • filename (String)

    file path

Returns:



23
24
25
# File 'lib/k8s/resource.rb', line 23

def self.from_file(filename)
  new(YAML.safe_load(File.read(filename)))
end

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

Parameters:

  • path (String)

    file path

Returns:



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

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 = File.read(path)
    hash = YAML.safe_load_stream(yaml, path)
    hash.map{|doc| new(doc) }
    #YAML.safe_load_stream(File.read(path), path).map{ |doc| new(doc) }
  end
end

.from_json(data) ⇒ self

Parameters:

  • data (String)

Returns:

  • (self)


17
18
19
# File 'lib/k8s/resource.rb', line 17

def self.from_json(data)
  new(K8s::JSONParser.parse(data))
end

Instance Method Details

#<=>(other) ⇒ Object



61
62
63
# File 'lib/k8s/resource.rb', line 61

def <=>(other)
  to_h <=> (other.is_a?(Hash) ? other : other.to_h)
end

#can_patch?(config_annotation) ⇒ Boolean

Parameters:

  • config_annotation (String)

Returns:

  • (Boolean)


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

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

#checksumString

Returns:

  • (String)


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

def checksum
  @checksum ||= Digest::MD5.hexdigest(Marshal.dump(to_h))
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)


97
98
99
100
101
102
103
104
105
106
# File 'lib/k8s/resource.rb', line 97

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

  current_hash = K8s::JSONParser.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:



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

def merge(attrs)
  self.class.new(
    Util.deep_merge(to_hash, attrs.to_hash, overwrite_arrays: true, merge_nil_values: true)
  )
end

#merge_patch_ops(attrs, config_annotation) ⇒ Hash

Parameters:

  • attrs (Hash)
  • config_annotation (String)

Returns:

  • (Hash)


89
90
91
# File 'lib/k8s/resource.rb', line 89

def merge_patch_ops(attrs, config_annotation)
  Util.json_patch(current_config(config_annotation), Util.deep_transform_keys(attrs, :to_s))
end

#to_json(options = {}) ⇒ String

Parameters:

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

    see Hash#to_json

Returns:

  • (String)


67
68
69
# File 'lib/k8s/resource.rb', line 67

def to_json(options = {})
  to_h.to_json(options)
end