Module: K8s::Util

Defined in:
lib/k8s/util.rb

Overview

Miscellaneous helpers

Constant Summary collapse

PATH_TR_MAP =
{ '~' => '~0', '/' => '~1' }.freeze
PATH_REGEX =
%r{(/|~(?!1))}.freeze

Class Method Summary collapse

Class Method Details

.compact_map(args) {|args| ... } ⇒ Array<nil, Object>

Yield with all non-nil args, returning matching array with corresponding return values or nils.

Args must be usable as hash keys. Duplicate args will all map to the same return value.

Parameters:

  • args (Array<nil, Object>)

Yields:

  • args

Yield Parameters:

  • args (Array<Object>)

    omitting any nil values

Returns:

  • (Array<nil, Object>)

    matching args array 1:1, containing yielded values for non-nil args



17
18
19
20
21
22
23
24
25
26
# File 'lib/k8s/util.rb', line 17

def self.compact_map(args)
  func_args = args.compact

  values = yield func_args

  # Hash{arg => value}
  value_map = Hash[func_args.zip(values)]

  args.map{ |arg| value_map[arg] }
end

.json_patch(patch_to, patch_from) ⇒ Object

Produces a set of json-patch operations so that applying the operations on a, gives you the results of b Used in correctly patching the Kube resources on stack updates

Parameters:

  • patch_to (Hash)

    Hash to compute patches against

  • patch_from (Hash)

    New Hash to compute patches “from”



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/k8s/util.rb', line 48

def self.json_patch(patch_to, patch_from)
  diffs = HashDiff.diff(patch_to, patch_from, array_path: true)
  ops = []
  # Each diff is like:
  # ["+", ["spec", "selector", "food"], "kebab"]
  # ["-", ["spec", "selector", "drink"], "pepsi"]
  # or
  # ["~", ["spec", "selector", "drink"], "old value", "new value"]
  # the path elements can be symbols too, depending on the input hashes
  diffs.each do |diff|
    operator = diff[0]
    # substitute '/' with '~1' and '~' with '~0'
    # according to RFC 6901
    path = diff[1].map { |p| p.to_s.gsub(PATH_REGEX, PATH_TR_MAP) }
    if operator == '-'
      ops << {
        op: "remove",
        path: "/" + path.join('/')
      }
    elsif operator == '+'
      ops << {
        op: "add",
        path: "/" + path.join('/'),
        value: diff[2]
      }
    elsif operator == '~'
      ops << {
        op: "replace",
        path: "/" + path.join('/'),
        value: diff[3]
      }
    else
      raise "Unknown diff operator: #{operator}!"
    end
  end

  ops
end

.recursive_compact(hash_or_array) ⇒ Hash, Array

Recursive compact for Hash/Array

Parameters:

  • hash_or_array (Hash, Array)

Returns:

  • (Hash, Array)


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

def self.recursive_compact(hash_or_array)
  p = proc do |*args|
    v = args.last
    v.delete_if(&p) if v.respond_to?(:delete_if) && !v.is_a?(Array)
    v.nil? || v.respond_to?(:empty?) && (v.empty? && (v.is_a?(Hash) || v.is_a?(Array)))
  end

  hash_or_array.delete_if(&p)
end