Module: Puppet::Util::K8s

Defined in:
lib/puppet/util/k8s.rb

Overview

Utility methods for K8s

Class Method Summary collapse

Class Method Details

.content_diff(local, content) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/puppet/util/k8s.rb', line 6

def self.content_diff(local, content)
  delete_merge = proc do |hash1, hash2|
    hash2.each_pair do |key, value|
      if hash1[key] != hash1[key.to_s]
        hash1[key.to_s] = hash1.delete key
        key = key.to_s
      end

      target_value = hash1[key]
      next if hash1.key?(key) && target_value.nil?

      if target_value.is_a?(Hash) && value.is_a?(Hash) && value.any? && target_value.any?
        delete_merge.call(target_value, value)
      elsif value.is_a?(Array) && target_value.is_a?(Array) && value.any? && target_value.any?
        diff = value.size != target_value.size
        target_value.each do |v|
          break if diff
          next if value.include? v

          if v.is_a? Hash
            diff ||= value.select { |ov| ov.is_a? Hash }.
                     none? do |ov|
              v_copy = Marshal.load(Marshal.dump(v))
              delete_merge.call(v_copy, ov)

              v_copy.empty?
            end
          else
            diff = true
          end
        end

        hash1.delete(key) unless diff
      elsif hash1.key?(key) && target_value == value
        hash1.delete(key)
      end

      hash1.delete(key) if hash1.key?(key) && (hash1[key].nil? || (hash1[key].respond_to?(:empty?) && hash1[key].empty?))
    end
    hash1.dup.each_pair do |key, value|
      hash1.delete key if value.nil? && !hash2.key?(key)
    end

    hash1
  end

  # Verify that the intersection of upstream content and user-provided content is identical
  # This allows the upstream object to contain additional keys - such as those auto-generated by Kubernetes
  diff = Marshal.load(Marshal.dump(local))
  delete_merge.call(diff, content)

  diff
end