kustomizer 0.1.7 → 0.1.12

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: 044c4518958413a91c1a8bc0a112220145ecdde5ab3bb043e64afe8bcffdcf40
4
- data.tar.gz: a081394dfcd4f5e14055a93cb1561200ce2db26c7580e918a8ca31d30920d807
3
+ metadata.gz: 9d4194dfeac67756db2018524a29771521143059e7c6b01783e50ef454f5c710
4
+ data.tar.gz: cf815fc480191d91626c273f925eac81fff5487b859d3b261dddaab6b9b2c594
5
5
  SHA512:
6
- metadata.gz: 9bb48c3fd80b552860a9f4c3f7e88076678abd0b1bf5c23d94c18fb9b2013dd0e410cbb97201c9717bccda3086f34af3f7f094a7deead4d23b27e20ddf3b97ad
7
- data.tar.gz: 3556e7605c02544f12790ab150db65ba2a937216bffa61c93909dd0ca7e0186bc83bf4ef63e7a8d3faf628ea8ce14b228baf6976cb932380cb00fe3394a3f7af
6
+ metadata.gz: 0edca955dacb8cc04b4d3643008aa4fa9f7a884ff8aa9c3534d15bf8f38ba9a879cc0dceddca03d9c95a743971da98f9342c730f17b5882eaf63e37f6814aecc
7
+ data.tar.gz: 645696be6aa341742d34fd8f7099fe68d3ea55f0f5548c42641a3452cfb4dc41325fd5b3b59681e1631af4d98dcd8e399091d88e6b71dc53cebc3c58a2d63350
data/.gitignore CHANGED
@@ -6,6 +6,7 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /vendor/
9
10
 
10
11
  # rspec failure tracking
11
12
  .rspec_status
data/Gemfile.lock CHANGED
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kustomizer (0.1.7)
5
- accessory (~> 0.1.9)
4
+ kustomizer (0.1.12)
5
+ accessory (~> 0.1.11)
6
6
  base32-multi
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- accessory (0.1.10)
11
+ accessory (0.1.11)
12
12
  base32-multi (0.1.1)
13
13
  diff-lcs (1.4.4)
14
14
  rake (13.0.3)
@@ -21,18 +21,15 @@ GEM
21
21
  rspec-expectations (3.10.1)
22
22
  diff-lcs (>= 1.2.0, < 2.0)
23
23
  rspec-support (~> 3.10.0)
24
- rspec-mocks (3.10.1)
24
+ rspec-mocks (3.10.2)
25
25
  diff-lcs (>= 1.2.0, < 2.0)
26
26
  rspec-support (~> 3.10.0)
27
- rspec-support (3.10.1)
27
+ rspec-support (3.10.2)
28
28
 
29
29
  PLATFORMS
30
- x86_64-darwin-20
30
+ ruby
31
31
 
32
32
  DEPENDENCIES
33
33
  kustomizer!
34
34
  rake (~> 13.0)
35
35
  rspec (~> 3.0)
36
-
37
- BUNDLED WITH
38
- 2.2.7
data/README.md CHANGED
@@ -19,8 +19,8 @@ Enough Library to solve our own problems :wink:)
19
19
  Status of `kustomization.yaml` feature support:
20
20
 
21
21
  * [x] `bases`
22
- * [ ] `commonAnnotations`
23
- * [ ] `commonLabels`
22
+ * [x] `commonAnnotations`
23
+ * [x] `commonLabels`
24
24
  * [ ] `configMapGenerator` and `secretGenerator`
25
25
  * [ ] `crds`
26
26
  * [x] `generators` (see [Extending Kustomize](https://kubectl.docs.kubernetes.io/guides/extending_kustomize/))
data/kustomizer.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.description = "A pure-ruby implementation of the Kubernetes 'kustomize' command."
13
13
  spec.homepage = "https://github.com/tsutsu/kustomize"
14
14
  spec.license = "MIT"
15
- spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
16
16
 
17
17
  spec.metadata["homepage_uri"] = spec.homepage
18
18
  spec.metadata["source_code_uri"] = spec.homepage
@@ -24,6 +24,6 @@ Gem::Specification.new do |spec|
24
24
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
25
25
  spec.require_paths = ["lib"]
26
26
 
27
- spec.add_runtime_dependency "accessory", "~> 0.1.9"
27
+ spec.add_runtime_dependency "accessory", "~> 0.1.11"
28
28
  spec.add_runtime_dependency "base32-multi"
29
29
  end
data/lib/kustomize.rb CHANGED
@@ -20,7 +20,7 @@ module Kustomize
20
20
  raise ArgumentError, "must be a kustomization document or a path to one, instead got: #{rel_path_or_rc.inspect}"
21
21
  end
22
22
 
23
- Kustomize::Emitter::FinalizerEmitter.new(base_emitter)
23
+ Kustomize::Emitter::FinalizerEmitter.new(base_emitter, session: session)
24
24
  end
25
25
 
26
26
  def self.load_doc(rc, session: Kustomize::Session.new, source_path:)
@@ -6,6 +6,8 @@ require 'kustomize/emitter/generator_plugins_emitter'
6
6
  require 'kustomize/transform/json_6902_patch_transform'
7
7
  require 'kustomize/transform/image_transform'
8
8
  require 'kustomize/transform/namespace_transform'
9
+ require 'kustomize/transform/common_annotations_transform'
10
+ require 'kustomize/transform/common_labels_transform'
9
11
  require 'kustomize/transform/fingerprint_suffix_transform'
10
12
  require 'kustomize/transform/ref_fixup_transform'
11
13
  require 'kustomize/transform/purge_internal_annotations_transform'
@@ -101,12 +103,30 @@ class Kustomize::Emitter::DocumentEmitter::KustomizationDocumentEmitter < Kustom
101
103
  end
102
104
  end
103
105
 
106
+ def common_annotations_transforms
107
+ if new_annots = @doc['commonAnnotations']
108
+ [Kustomize::Transform::CommonAnnotationsTransform.create(new_annots)]
109
+ else
110
+ []
111
+ end
112
+ end
113
+
114
+ def common_labels_transforms
115
+ if new_labels = @doc['commonLabels']
116
+ [Kustomize::Transform::CommonLabelsTransform.create(new_labels)]
117
+ else
118
+ []
119
+ end
120
+ end
121
+
104
122
  def transforms
105
123
  return @transforms if @transforms
106
124
 
107
125
  @transforms = [
108
126
  self.namespace_transforms,
109
127
  self.image_transforms,
128
+ self.common_annotations_transforms,
129
+ self.common_labels_transforms,
110
130
  self.json_6902_patch_transforms,
111
131
  self.transformer_plugin_transforms
112
132
  ].flatten
@@ -2,11 +2,14 @@ require 'kustomize/emitter'
2
2
 
3
3
  require 'kustomize/transform/fingerprint_suffix_transform'
4
4
  require 'kustomize/transform/ref_fixup_transform'
5
+ require 'kustomize/transform/filter_for_session_specified_component_transform'
6
+ require 'kustomize/transform/drop_filtered_documents_transform'
5
7
  require 'kustomize/transform/purge_internal_annotations_transform'
6
8
 
7
9
  class Kustomize::Emitter::FinalizerEmitter < Kustomize::Emitter
8
- def initialize(input_emitter)
10
+ def initialize(input_emitter, session:)
9
11
  @input_emitter = input_emitter
12
+ @session = session
10
13
  end
11
14
 
12
15
  def input_emitters
@@ -16,11 +19,20 @@ class Kustomize::Emitter::FinalizerEmitter < Kustomize::Emitter
16
19
  def transforms
17
20
  return @transforms if @transforms
18
21
 
22
+ final_filters =
23
+ if comp = @session.only_emit_component
24
+ [Kustomize::Transform::FilterForSessionSpecifiedComponentTransform.create(comp)]
25
+ else
26
+ []
27
+ end
28
+
19
29
  @transforms = [
20
30
  Kustomize::Transform::FingerprintSuffixTransform.instance,
21
31
  Kustomize::Transform::RefFixupTransform.instance,
32
+ final_filters,
33
+ Kustomize::Transform::DropFilteredDocumentsTransform.instance,
22
34
  Kustomize::Transform::PurgeInternalAnnotationsTransform.instance
23
- ].flatten
35
+ ].flatten.compact
24
36
  end
25
37
 
26
38
  def emit
@@ -17,6 +17,10 @@ class Kustomize::Json6902Patch::Op
17
17
  e = e.gsub('~1', '/')
18
18
  if e == ":all"
19
19
  Accessory::Access.all
20
+ elsif e == ":first"
21
+ Accessory::Access.first
22
+ elsif e == ":last"
23
+ Accessory::Access.last
20
24
  elsif e.match?(/^\d+$/)
21
25
  e.to_i
22
26
  else
@@ -0,0 +1,36 @@
1
+ require 'kustomize/json_6902_patch'
2
+
3
+ class Kustomize::Json6902Patch::AppendOp < Kustomize::Json6902Patch::Op
4
+ def self.create(patch_spec)
5
+ elements =
6
+ if e = patch_spec['element']
7
+ [e]
8
+ elsif es = patch_spec['elements']
9
+ es
10
+ else
11
+ raise ArgumentError, "must specify one of 'element' or 'elements' in: #{patch_spec.inspect}"
12
+ end
13
+
14
+ new(
15
+ array_path: patch_spec['path'],
16
+ elements: elements
17
+ )
18
+ end
19
+
20
+ def initialize(array_path:, elements:)
21
+ @lens = parse_lens(array_path)
22
+ @new_elements = elements
23
+ end
24
+
25
+ def apply(rc)
26
+ @lens.update_in(rc) do |orig_arr|
27
+ new_arr = orig_arr.dup || []
28
+
29
+ @new_elements.each do |elem|
30
+ new_arr.push(elem)
31
+ end
32
+
33
+ [:set, new_arr]
34
+ end
35
+ end
36
+ end
@@ -18,6 +18,8 @@ class Kustomize::Json6902Patch::GsubOp < Kustomize::Json6902Patch::Op
18
18
  def apply(rc)
19
19
  @lenses.inject(rc) do |doc, lens|
20
20
  lens.update_in(doc) do |orig_value|
21
+ next(:keep) unless orig_value.kind_of?(String)
22
+
21
23
  new_value = orig_value.gsub(@pattern, @replacement)
22
24
 
23
25
  if new_value != orig_value
@@ -3,12 +3,14 @@ module Kustomize; end
3
3
  require 'kustomize/plugin_manager'
4
4
 
5
5
  class Kustomize::Session
6
- def initialize(load_paths: [])
6
+ def initialize(load_paths: [], only_emit_component: nil)
7
7
  @load_paths = load_paths
8
8
  @plugin_manager = Kustomize::PluginManager.new(session: self)
9
+ @only_emit_component = only_emit_component
9
10
  end
10
11
 
11
12
  attr_reader :plugin_manager
13
+ attr_accessor :only_emit_component
12
14
 
13
15
  def builtin_load_paths
14
16
  [Pathname.new(__FILE__).expand_path.parent / 'builtin_plugins']
@@ -0,0 +1,36 @@
1
+ require 'accessory'
2
+
3
+ require 'kustomize/transform'
4
+
5
+ class Kustomize::Transform::CommonAnnotationsTransform < Kustomize::Transform
6
+ include Accessory
7
+
8
+ def initialize(new_annots)
9
+ @new_annots = new_annots
10
+ end
11
+
12
+ BASE_LENS = Lens["metadata", "annotations"]
13
+
14
+ LENS_PREFIXES = [
15
+ Lens["spec", "template"],
16
+ Lens["spec", "jobTemplate", "spec", "template"]
17
+ ]
18
+
19
+ def rewrite(rc_doc)
20
+ rc_doc = BASE_LENS.update_in(rc_doc) do |annots|
21
+ [:set, (annots || {}).merge(@new_annots)]
22
+ end
23
+
24
+ LENS_PREFIXES.inject(rc_doc) do |doc, prefix|
25
+ prefix.update_in(rc_doc) do |node|
26
+ next(:keep) unless node.kind_of?(Hash)
27
+
28
+ new_node = BASE_LENS.update_in(node) do |annots|
29
+ [:set, (annots || {}).merge(@new_annots)]
30
+ end
31
+
32
+ [:set, new_node]
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,41 @@
1
+ require 'accessory'
2
+
3
+ require 'kustomize/transform'
4
+
5
+ class Kustomize::Transform::CommonLabelsTransform < Kustomize::Transform
6
+ include Accessory
7
+
8
+ def initialize(new_labels)
9
+ @new_labels = new_labels
10
+ end
11
+
12
+ LENSES_FOR_ALL = [
13
+ Lens["metadata", "labels"]
14
+ ]
15
+
16
+ LENSES_FOR_KIND = {
17
+ "Service" => [
18
+ Lens["spec", "selector"]
19
+ ],
20
+
21
+ "Deployment" => [
22
+ Lens["spec", "selector", "matchLabels"]
23
+ ],
24
+ }
25
+
26
+ def rewrite(rc_doc)
27
+ rc_kind = rc_doc['kind']
28
+ use_lenses = LENSES_FOR_ALL
29
+
30
+ if lenses_for_doc_kind = LENSES_FOR_KIND[rc_kind]
31
+ use_lenses += lenses_for_doc_kind
32
+ end
33
+
34
+ use_lenses.inject(rc_doc) do |doc, lens|
35
+ lens.update_in(doc) do |annots|
36
+ [:set, (annots || {}).merge(@new_labels)]
37
+ end
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,25 @@
1
+ require 'singleton'
2
+ require 'set'
3
+
4
+ require 'accessory'
5
+
6
+ require 'kustomize/transform'
7
+
8
+ class Kustomize::Transform::DropFilteredDocumentsTransform < Kustomize::Transform
9
+ include Accessory
10
+ include Singleton
11
+
12
+ ANNOTS_LENS = Lens['metadata', 'annotations']
13
+
14
+ DROP_ON_ANNOTS = Set[
15
+ 'config.kubernetes.io/local-config',
16
+ 'kustomizer.covalenthq.com/drop'
17
+ ]
18
+
19
+ def rewrite_all(rcs)
20
+ rcs.filter do |rc|
21
+ annot_keys = (ANNOTS_LENS.get_in(rc) || {}).keys
22
+ not(annot_keys.find{ |k| DROP_ON_ANNOTS.member?(k) })
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ require 'accessory'
2
+
3
+ require 'kustomize/transform'
4
+
5
+ class Kustomize::Transform::FilterForSessionSpecifiedComponentTransform < Kustomize::Transform
6
+ include Accessory
7
+
8
+ def initialize(component_name)
9
+ @component_name = component_name
10
+ end
11
+
12
+ ANNOTS_LENS = Lens['metadata', 'annotations']
13
+
14
+ COMPONENT_ANNOT_NAME = 'kustomizer.covalenthq.com/component-name'
15
+ DROP_ANNOT_NAME = 'kustomizer.covalenthq.com/drop'
16
+
17
+ def rewrite(rc)
18
+ ANNOTS_LENS.update_in(rc) do |orig_annots|
19
+ orig_annots ||= {}
20
+
21
+ if orig_annots[COMPONENT_ANNOT_NAME] == @component_name
22
+ :keep
23
+ else
24
+ new_annots = orig_annots.merge({DROP_ANNOT_NAME => 'true'})
25
+ [:set, new_annots]
26
+ end
27
+ end
28
+ end
29
+ end
@@ -35,7 +35,13 @@ class Kustomize::Transform::FingerprintSuffixTransform < Kustomize::Transform
35
35
  return rc unless APPLICABLE_KINDS.member?(rc_kind)
36
36
 
37
37
  FINGERPRINT_LENS.update_in(rc) do |orig_value|
38
- next(:keep) if orig_value
38
+ if orig_value
39
+ if orig_value == ''
40
+ next(:pop)
41
+ else
42
+ next(:keep)
43
+ end
44
+ end
39
45
 
40
46
  content_part = CONTENT_LENS_BY_KIND[rc_kind].get_in(rc)
41
47
  content_ser = content_part.to_json
@@ -47,9 +53,11 @@ class Kustomize::Transform::FingerprintSuffixTransform < Kustomize::Transform
47
53
  base_name = NAME_LENS.get_in(rc)
48
54
  fingerprint = FINGERPRINT_LENS.get_in(rc)
49
55
 
50
- NAME_LENS.update_in(rc) do |base_name|
51
- suffixed_name = [base_name, fingerprint].join(SUFFIX_JOINER)
52
- [:set, suffixed_name]
56
+ if fingerprint
57
+ NAME_LENS.update_in(rc) do |base_name|
58
+ suffixed_name = [base_name, fingerprint].join(SUFFIX_JOINER)
59
+ [:set, suffixed_name]
60
+ end
53
61
  end
54
62
 
55
63
  rc
@@ -7,6 +7,7 @@ require 'kustomize/json_6902_patch/add_op'
7
7
  require 'kustomize/json_6902_patch/replace_op'
8
8
  require 'kustomize/json_6902_patch/remove_op'
9
9
  require 'kustomize/json_6902_patch/gsub_op'
10
+ require 'kustomize/json_6902_patch/append_op'
10
11
 
11
12
  class Kustomize::Transform::Json6902PatchTransform < Kustomize::Transform
12
13
  def self.create(kustomization_file, op_spec)
@@ -8,23 +8,32 @@ class Kustomize::Transform::PurgeInternalAnnotationsTransform < Kustomize::Trans
8
8
  include Accessory
9
9
  include Singleton
10
10
 
11
- ANNOTS_LENS = Lens['metadata', 'annotations']
12
-
13
- INTERNAL_ANNOT_PATTERN = /^kustomizer\.covalenthq\.com\//
14
-
15
- def rewrite(rc)
16
- ANNOTS_LENS.update_in(rc) do |orig_annots|
17
- next(:keep) unless orig_annots and orig_annots.length.nonzero?
18
-
19
- new_annots =
20
- orig_annots.reject{ |k, v| INTERNAL_ANNOT_PATTERN.match?(k) }
21
-
22
- if new_annots.length == orig_annots.length
23
- :keep
24
- elsif new_annots.empty?
25
- :pop
26
- else
27
- [:set, new_annots]
11
+ LENSES = [
12
+ Lens['metadata', 'annotations'],
13
+ Lens['spec', 'template', 'metadata', 'annotations'],
14
+ Lens['spec', 'jobTemplate', 'spec', 'template', 'metadata', 'annotations']
15
+ ]
16
+
17
+ INTERNAL_ANNOT_PATTERNS = [
18
+ /^config\.kubernetes\.io\//,
19
+ /^kustomizer\.covalenthq\.com\//
20
+ ]
21
+
22
+ def rewrite(rc_doc)
23
+ LENSES.inject(rc_doc) do |doc, lens|
24
+ lens.update_in(rc_doc) do |orig_annots|
25
+ next(:keep) unless orig_annots and orig_annots.length.nonzero?
26
+
27
+ new_annots =
28
+ orig_annots.reject{ |k, v| INTERNAL_ANNOT_PATTERNS.any?{ |pat| pat.match?(k) } }
29
+
30
+ if new_annots.length == orig_annots.length
31
+ :keep
32
+ elsif new_annots.empty?
33
+ :pop
34
+ else
35
+ [:set, new_annots]
36
+ end
28
37
  end
29
38
  end
30
39
  end
@@ -1,3 +1,3 @@
1
1
  module Kustomize
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.12"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kustomizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Levi Aul
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-02 00:00:00.000000000 Z
11
+ date: 2021-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: accessory
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.9
19
+ version: 0.1.11
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.9
26
+ version: 0.1.11
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: base32-multi
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -68,6 +68,7 @@ files:
68
68
  - lib/kustomize/generator_plugin.rb
69
69
  - lib/kustomize/json_6902_patch.rb
70
70
  - lib/kustomize/json_6902_patch/add_op.rb
71
+ - lib/kustomize/json_6902_patch/append_op.rb
71
72
  - lib/kustomize/json_6902_patch/gsub_op.rb
72
73
  - lib/kustomize/json_6902_patch/remove_op.rb
73
74
  - lib/kustomize/json_6902_patch/replace_op.rb
@@ -78,6 +79,10 @@ files:
78
79
  - lib/kustomize/session.rb
79
80
  - lib/kustomize/target_spec.rb
80
81
  - lib/kustomize/transform.rb
82
+ - lib/kustomize/transform/common_annotations_transform.rb
83
+ - lib/kustomize/transform/common_labels_transform.rb
84
+ - lib/kustomize/transform/drop_filtered_documents_transform.rb
85
+ - lib/kustomize/transform/filter_for_session_specified_component_transform.rb
81
86
  - lib/kustomize/transform/fingerprint_suffix_transform.rb
82
87
  - lib/kustomize/transform/image_transform.rb
83
88
  - lib/kustomize/transform/json_6902_patch_transform.rb
@@ -101,7 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
106
  requirements:
102
107
  - - ">="
103
108
  - !ruby/object:Gem::Version
104
- version: 2.7.0
109
+ version: 2.6.0
105
110
  required_rubygems_version: !ruby/object:Gem::Requirement
106
111
  requirements:
107
112
  - - ">="