kucodiff 0.0.1 → 0.3.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: da8aef200dbbf8a83f7772181b035549cc929450
4
- data.tar.gz: 529ad277ad8cc3dcfcdc4c433d62df20faa19c25
2
+ SHA256:
3
+ metadata.gz: 7ee341354f388e8f3ec330e0a6bdfb8932f930cc22002abeba9231f907ec2757
4
+ data.tar.gz: 95373625d0a01aaebc3f2b4ad8a0993f1dfc05069def44e478d566f037221b4c
5
5
  SHA512:
6
- metadata.gz: 70500ca39f992dfa03d986c9c9aa4ec01b9c3dda9fac3be0284b7c1943f12b966db39a4f19e9cce560c0b1f72e321ae5203304f817cffcc72fc8c07e280111c0
7
- data.tar.gz: bd7581ffeec22db708ec87e293f8331746b202744f3b27afea5c151977a065b9cf7d570f6a9be10640bdb47090e3c8b6eee66b993e75f36c59eb9d4f027a2218
6
+ metadata.gz: 246489f2d7f29fee218a1b6924143b736945092e343f91a09e2bcd3a6cdadebb4de875102c74f7c44c9343ee9b58d0b8a8474068604b7c8db99e787bd4584686
7
+ data.tar.gz: 3ada56f41b856de3ffc0a0e260e5881d53d41c08d77826313ef1ddfd48273fe3ac061cff0d3be0c72ea1c7d81af5cce51c7506a0106e8f23bf463d54ed74d58c
@@ -2,13 +2,20 @@ require 'yaml'
2
2
 
3
3
  module Kucodiff
4
4
  class << self
5
- def diff(files, ignore: false, expected: {})
5
+ def diff(files, ignore: false, indent_pod: false, expected: {})
6
6
  raise ArgumentError, "Need 2+ files" if files.size < 2
7
7
 
8
8
  base = files.shift
9
9
  base_template = read(base)
10
+
10
11
  diff = files.each_with_object({}) do |other, all|
11
- result = different_keys(base_template, read(other))
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
12
19
  result.reject! { |k| k =~ ignore } if ignore
13
20
  all["#{base}-#{other}"] = result.sort
14
21
  end
@@ -24,18 +31,26 @@ module Kucodiff
24
31
  private
25
32
 
26
33
  def read(file)
27
- content = if file.end_with?('.yml', '.yaml')
28
- YAML.load_stream(File.read(file)) # TODO: test need for stream
29
- else raise ArgumentError, "unknown file format in #{file}"
30
- end.first
34
+ content =
35
+ if file.end_with?('.yml', '.yaml')
36
+ if RUBY_VERSION >= "2.6.0"
37
+ YAML.load_stream(File.read(file), filename: file) # uncovered
38
+ else
39
+ YAML.load_stream(File.read(file), file) # uncovered
40
+ end
41
+ else
42
+ raise ArgumentError, "unknown file format in #{file}"
43
+ end.first
44
+
45
+ hashify_container_env!(content)
46
+ hashify_required_env!(content)
31
47
 
32
- hashify_container_env(content)
33
48
  flat_hash(content)
34
49
  end
35
50
 
36
- # make env compareable
37
- def hashify_container_env(content)
38
- containers = content.fetch('spec', {}).fetch('template', {}).fetch('spec', {}).fetch('containers', [])
51
+ # make env comparable
52
+ def hashify_container_env!(content)
53
+ containers = template(content).fetch('spec', {}).fetch('containers', [])
39
54
  containers.each do |container|
40
55
  next unless container['env']
41
56
  container['env'] = container['env'].each_with_object({}) do |v, h|
@@ -45,6 +60,30 @@ module Kucodiff
45
60
  end
46
61
  end
47
62
 
63
+ def hashify_required_env!(content)
64
+ key = 'samson/required_env'
65
+ annotations = template(content).fetch('metadata', {}).fetch('annotations', {})
66
+ annotations[key] = Hash[annotations[key].strip.split(/[\s,]/).map { |k| [k, true] }] if annotations[key]
67
+ end
68
+
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] }]
77
+ else
78
+ template
79
+ end
80
+ end
81
+
82
+ diff = different_keys(*templates)
83
+ diff.select! { |k| k.start_with?(prefix) } if ignore_unindented
84
+ diff
85
+ end
86
+
48
87
  def different_keys(a, b)
49
88
  (a.keys + b.keys).uniq.select { |k| a[k] != b[k] }
50
89
  end
@@ -53,6 +92,10 @@ module Kucodiff
53
92
  a + b - (a & b)
54
93
  end
55
94
 
95
+ def template(content)
96
+ content['kind'] == "Pod" ? content : content.fetch('spec', {}).fetch('template', {})
97
+ end
98
+
56
99
  # http://stackoverflow.com/questions/9647997/converting-a-nested-hash-into-a-flat-hash
57
100
  def flat_hash(input, base = nil, all = {})
58
101
  if input.is_a?(Array)
@@ -1,3 +1,3 @@
1
1
  module Kucodiff
2
- VERSION = "0.0.1"
2
+ VERSION = "0.3.0"
3
3
  end
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.0.1
4
+ version: 0.3.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-03-13 00:00:00.000000000 Z
11
+ date: 2020-06-29 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.5.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.3
43
42
  signing_key:
44
43
  specification_version: 4
45
44
  summary: Smart diff for kubernetes configs to ensure symmetric configuration