kucodiff 0.3.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ee341354f388e8f3ec330e0a6bdfb8932f930cc22002abeba9231f907ec2757
4
- data.tar.gz: 95373625d0a01aaebc3f2b4ad8a0993f1dfc05069def44e478d566f037221b4c
3
+ metadata.gz: 76444ee014253e7f3336e78ef6ddb39a5b6eb6052964644af058bff8e3fd3531
4
+ data.tar.gz: a0410e79490633c00e68e3ea263b25dc012154ab3e52b980ab4ce746807c72de
5
5
  SHA512:
6
- metadata.gz: 246489f2d7f29fee218a1b6924143b736945092e343f91a09e2bcd3a6cdadebb4de875102c74f7c44c9343ee9b58d0b8a8474068604b7c8db99e787bd4584686
7
- data.tar.gz: 3ada56f41b856de3ffc0a0e260e5881d53d41c08d77826313ef1ddfd48273fe3ac061cff0d3be0c72ea1c7d81af5cce51c7506a0106e8f23bf463d54ed74d58c
6
+ metadata.gz: c3223cb54dc70464911e6f01ea8a745a208180a7f659b1ef7397c7091bc1dddd272364495494c67d10dfceba429d301dee185e342e1796bf354f1ddaa7aed089
7
+ data.tar.gz: ca69382adba297961dc1ccc6c9aa662670e58fd32d64e2bdc584832383680377247cc5f74242c5fbe650821835c3f0c447bd88cace1aa3cc27104085db57652d
@@ -1,3 +1,3 @@
1
1
  module Kucodiff
2
- VERSION = "0.3.0"
2
+ VERSION = "0.6.0"
3
3
  end
data/lib/kucodiff.rb CHANGED
@@ -2,7 +2,7 @@ require 'yaml'
2
2
 
3
3
  module Kucodiff
4
4
  class << self
5
- def diff(files, ignore: false, indent_pod: false, expected: {})
5
+ def diff(files, ignore: false, indent_pod: true, expected: {})
6
6
  raise ArgumentError, "Need 2+ files" if files.size < 2
7
7
 
8
8
  base = files.shift
@@ -10,12 +10,8 @@ module Kucodiff
10
10
 
11
11
  diff = files.each_with_object({}) do |other, all|
12
12
  other_template = read(other)
13
- result =
14
- if indent_pod
15
- different_keys_pod_indented(base_template, other_template)
16
- else
17
- different_keys(base_template, other_template)
18
- end
13
+ pod_indented!(base_template, other_template) if indent_pod
14
+ result = different_keys(base_template, other_template)
19
15
  result.reject! { |k| k =~ ignore } if ignore
20
16
  all["#{base}-#{other}"] = result.sort
21
17
  end
@@ -42,21 +38,28 @@ module Kucodiff
42
38
  raise ArgumentError, "unknown file format in #{file}"
43
39
  end.first
44
40
 
45
- hashify_container_env!(content)
41
+ template = template(content)
42
+ template.dig("spec", "containers")&.each do |container|
43
+ hashify_named_array!(container, "env", first: true)
44
+ hashify_named_array!(container, "volumeMounts", first: false)
45
+ end
46
+ hashify_named_array!(template["spec"], "volumes", first: false)
46
47
  hashify_required_env!(content)
47
48
 
48
49
  flat_hash(content)
49
50
  end
50
51
 
51
- # make env comparable
52
- def hashify_container_env!(content)
53
- containers = template(content).fetch('spec', {}).fetch('containers', [])
54
- containers.each do |container|
55
- next unless container['env']
56
- container['env'] = container['env'].each_with_object({}) do |v, h|
57
- value_key = (v.keys - ['name']).first
58
- h[v.fetch('name')] = v.fetch(value_key)
59
- end
52
+ def hashify_named_array!(object, key, first:)
53
+ return if !object || !(array = object[key])
54
+ object[key] = array.to_h do |v|
55
+ keep = (v.keys - ['name'])
56
+ value =
57
+ if first
58
+ v.fetch(keep.first)
59
+ else
60
+ v.slice(*keep)
61
+ end
62
+ [v.fetch('name'), value]
60
63
  end
61
64
  end
62
65
 
@@ -66,22 +69,23 @@ module Kucodiff
66
69
  annotations[key] = Hash[annotations[key].strip.split(/[\s,]/).map { |k| [k, true] }] if annotations[key]
67
70
  end
68
71
 
69
- def different_keys_pod_indented(*templates)
70
- ignore_unindented = false
71
- prefix = "spec.template."
72
-
73
- templates.map! do |template|
74
- if template["kind"] == "Pod"
75
- ignore_unindented = true
76
- Hash[template.map { |k,v| [prefix + k, v] }]
72
+ # templates are flat hashes already
73
+ def pod_indented!(*templates)
74
+ kinds = templates.map { |t| t["kind"] }
75
+ return if (kinds & ["Pod", "PodTemplate"]).empty? || kinds.uniq.size == 1
76
+
77
+ templates.each do |template|
78
+ case template["kind"]
79
+ when "Pod"
80
+ # all good
81
+ when "PodTemplate"
82
+ template.select! { |k, _| k.start_with?("template.") }
83
+ template.transform_keys! { |k| k.sub("template.", "") }
77
84
  else
78
- template
85
+ template.select! { |k, _| k.start_with?("spec.template.") }
86
+ template.transform_keys! { |k| k.sub("spec.template.", "") }
79
87
  end
80
88
  end
81
-
82
- diff = different_keys(*templates)
83
- diff.select! { |k| k.start_with?(prefix) } if ignore_unindented
84
- diff
85
89
  end
86
90
 
87
91
  def different_keys(a, b)
@@ -93,7 +97,11 @@ module Kucodiff
93
97
  end
94
98
 
95
99
  def template(content)
96
- content['kind'] == "Pod" ? content : content.fetch('spec', {}).fetch('template', {})
100
+ case content['kind']
101
+ when "Pod" then content
102
+ when "PodTemplate" then content.fetch('template')
103
+ else content.dig('spec', 'template') || {}
104
+ end
97
105
  end
98
106
 
99
107
  # http://stackoverflow.com/questions/9647997/converting-a-nested-hash-into-a-flat-hash
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kucodiff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-29 00:00:00.000000000 Z
11
+ date: 2022-05-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: michael@grosser.it
@@ -31,14 +31,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
31
31
  requirements:
32
32
  - - ">="
33
33
  - !ruby/object:Gem::Version
34
- version: 2.5.0
34
+ version: 2.6.0
35
35
  required_rubygems_version: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
39
  version: '0'
40
40
  requirements: []
41
- rubygems_version: 3.1.3
41
+ rubygems_version: 3.1.6
42
42
  signing_key:
43
43
  specification_version: 4
44
44
  summary: Smart diff for kubernetes configs to ensure symmetric configuration