kucodiff 0.2.1 → 0.5.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
- SHA1:
3
- metadata.gz: cc7a9fee577353c9b0d12160b0a00cc6f8b6325a
4
- data.tar.gz: 81cda8035f50ca59908f88064fe3b7f273128679
2
+ SHA256:
3
+ metadata.gz: 626cbfd398f264adaf284a4842ea250babe4d8bcb506f1700cb1629c2cfb3e3e
4
+ data.tar.gz: d3d33300480d745da896131cb1dba9881358b3d478e91ff516a3446661e33313
5
5
  SHA512:
6
- metadata.gz: f18b565c6e64ccc715de6a87b24bd48592cff258b234f0b829c2906bc010759dfc39c9f1ac27c3ed7807f229159b47606ca82811dd8ed5301aa2b539dfbb2888
7
- data.tar.gz: 4f1a87766ba6afe954bd774266d9b26217efa76e29622b174587f6793af1371d16f170ec28c18d4a1b91956ed581e66cacc158fe12235195ea19f067856a2ab9
6
+ metadata.gz: 2a87916da814015b02fcb1e5fa9b8027702eeab15a6104e3c9af2bb682daf98763d4260416962b70dda6b5b23f5e97bdfb7bc0877fb3989c1acf871f72b0c9fd
7
+ data.tar.gz: a4f29440331c0eae9a2447751e6c3b6653019d1f3f0198e591915ea8888495bf8667aa961097c5b23bd3a31c63c9f3b69a4313677c837376d8b1098f16b36256
@@ -1,3 +1,3 @@
1
1
  module Kucodiff
2
- VERSION = "0.2.1"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/kucodiff.rb CHANGED
@@ -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
@@ -31,26 +27,39 @@ module Kucodiff
31
27
  private
32
28
 
33
29
  def read(file)
34
- content = if file.end_with?('.yml', '.yaml')
35
- YAML.load_stream(File.read(file), file) # TODO: test need for stream
36
- else raise ArgumentError, "unknown file format in #{file}"
37
- end.first
30
+ content =
31
+ if file.end_with?('.yml', '.yaml')
32
+ if RUBY_VERSION >= "2.6.0"
33
+ YAML.load_stream(File.read(file), filename: file) # uncovered
34
+ else
35
+ YAML.load_stream(File.read(file), file) # uncovered
36
+ end
37
+ else
38
+ raise ArgumentError, "unknown file format in #{file}"
39
+ end.first
38
40
 
39
- 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)
40
47
  hashify_required_env!(content)
41
48
 
42
49
  flat_hash(content)
43
50
  end
44
51
 
45
- # make env comparable
46
- def hashify_container_env!(content)
47
- containers = template(content).fetch('spec', {}).fetch('containers', [])
48
- containers.each do |container|
49
- next unless container['env']
50
- container['env'] = container['env'].each_with_object({}) do |v, h|
51
- value_key = (v.keys - ['name']).first
52
- h[v.fetch('name')] = v.fetch(value_key)
53
- 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]
54
63
  end
55
64
  end
56
65
 
@@ -60,22 +69,23 @@ module Kucodiff
60
69
  annotations[key] = Hash[annotations[key].strip.split(/[\s,]/).map { |k| [k, true] }] if annotations[key]
61
70
  end
62
71
 
63
- def different_keys_pod_indented(*templates)
64
- ignore_unindented = false
65
- prefix = "spec.template."
66
-
67
- templates.map! do |template|
68
- if template["kind"] == "Pod"
69
- ignore_unindented = true
70
- 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.", "") }
71
84
  else
72
- template
85
+ template.select! { |k, _| k.start_with?("spec.template.") }
86
+ template.transform_keys! { |k| k.sub("spec.template.", "") }
73
87
  end
74
88
  end
75
-
76
- diff = different_keys(*templates)
77
- diff.select! { |k| k.start_with?(prefix) } if ignore_unindented
78
- diff
79
89
  end
80
90
 
81
91
  def different_keys(a, b)
@@ -87,7 +97,11 @@ module Kucodiff
87
97
  end
88
98
 
89
99
  def template(content)
90
- 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
91
105
  end
92
106
 
93
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.2.1
4
+ version: 0.5.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: 2017-04-30 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,15 +31,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
31
31
  requirements:
32
32
  - - ">="
33
33
  - !ruby/object:Gem::Version
34
- version: 2.1.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
- rubyforge_project:
42
- rubygems_version: 2.5.1
41
+ rubygems_version: 3.1.6
43
42
  signing_key:
44
43
  specification_version: 4
45
44
  summary: Smart diff for kubernetes configs to ensure symmetric configuration