kustomizer 0.1.1 → 0.1.6

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: 961ff655cafb23672a01fae7147f08a28d4198a59f72a07c092cfd8907acb98f
4
- data.tar.gz: fdafe50e353de94b2105fa754a31ff6a40b347585f1d0359924410581ffa7b58
3
+ metadata.gz: 4521fc26d8bce0f3a4d5e0a540157bc4db6471ee052870122147d7c4c08e304e
4
+ data.tar.gz: 1a4826b18c76af8e8171ca7a1d46175b73f53f231d457860d1dfc39535a75fee
5
5
  SHA512:
6
- metadata.gz: 60136761417e2ddcdeef7af0e1fb8898ba1cdb95b62e638e0355fba04af252c9b14fce85801f924109ebbae4be01f19e83a3b9795dbb29a9ea49303e71f62bfa
7
- data.tar.gz: 1bce76a9ccb5549dc71e3bf1acb0a8c26f0ded8c0a785332f6d72c77d81fffc71ec9629fa593462312fe67bee80ffb19c5b78d792b5e6e3b3f02b0e39d805e93
6
+ metadata.gz: 5e4ee25d6000af75bcd3062feea249af2f56a3245f6e341f0c85e4f3b7c6bed66803753ea5adbd45e377b2ee553039cc9ddbb09ef1bbcde70cc8c2256e5aa015
7
+ data.tar.gz: 7e3060f016cb2852e05cd564fef1093442fb68536babd435c5d0057b17aeaa3da726b0127bacf47eb5c83290e49494563d57e848a2249240ac33a1b59fc177cc
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kustomizer (0.1.1)
4
+ kustomizer (0.1.6)
5
5
  accessory (~> 0.1.9)
6
6
  base32-multi
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- accessory (0.1.9)
11
+ accessory (0.1.10)
12
12
  base32-multi (0.1.1)
13
13
  diff-lcs (1.4.4)
14
14
  rake (13.0.3)
@@ -35,4 +35,4 @@ DEPENDENCIES
35
35
  rspec (~> 3.0)
36
36
 
37
37
  BUNDLED WITH
38
- 2.2.5
38
+ 2.2.7
@@ -5,18 +5,22 @@ require 'kustomize/session'
5
5
 
6
6
  require 'kustomize/emitter/file_emitter'
7
7
  require 'kustomize/emitter/directory_emitter'
8
+ require 'kustomize/emitter/finalizer_emitter'
8
9
  require 'kustomize/emitter/document_emitter/kustomization_document_emitter'
9
10
 
10
11
  module Kustomize
11
12
  def self.load(rel_path_or_rc, session: Kustomize::Session.new, source_path: nil)
12
- case rel_path_or_rc
13
- when String, Pathname
14
- load_path(rel_path_or_rc, session: session)
15
- when Hash
16
- load_doc(rel_path_or_rc, session: session, source_path: source_path)
17
- else
18
- raise ArgumentError, "must be a kustomization document or a path to one, instead got: #{rel_path_or_rc.inspect}"
19
- end
13
+ base_emitter =
14
+ case rel_path_or_rc
15
+ when String, Pathname
16
+ load_path(rel_path_or_rc, session: session)
17
+ when Hash
18
+ load_doc(rel_path_or_rc, session: session, source_path: source_path)
19
+ else
20
+ raise ArgumentError, "must be a kustomization document or a path to one, instead got: #{rel_path_or_rc.inspect}"
21
+ end
22
+
23
+ Kustomize::Emitter::FinalizerEmitter.new(base_emitter)
20
24
  end
21
25
 
22
26
  def self.load_doc(rc, session: Kustomize::Session.new, source_path:)
@@ -0,0 +1,15 @@
1
+ require 'kustomize/generator_plugin'
2
+
3
+ module CovalentKustomizer; end
4
+
5
+ class CovalentKustomizer::TestGenerator < Kustomize::GeneratorPlugin
6
+ match_on api_version: 'kustomizer.covalenthq.com/v1'
7
+
8
+ def initialize(rc)
9
+ @docs = rc['emit'] || []
10
+ end
11
+
12
+ def emit
13
+ @docs
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require 'kustomize/transformer_plugin'
2
+
3
+ module CovalentKustomizer; end
4
+
5
+ class CovalentKustomizer::TestTransformer < Kustomize::TransformerPlugin
6
+ match_on api_version: 'kustomizer.covalenthq.com/v1'
7
+
8
+ def initialize(rc)
9
+ @insertions = rc['insert'] || {}
10
+ end
11
+
12
+ def rewrite(rc)
13
+ rc.merge(@insertions)
14
+ end
15
+ end
@@ -14,4 +14,8 @@ class Kustomize::Emitter
14
14
  def to_yaml_stream
15
15
  self.emit.map(&:to_yaml).join("")
16
16
  end
17
+
18
+ def inspect
19
+ "#<#{self.class}>"
20
+ end
17
21
  end
@@ -1,12 +1,15 @@
1
1
  require 'kustomize/emitter/document_emitter'
2
2
  require 'kustomize/emitter/file_emitter'
3
3
  require 'kustomize/emitter/directory_emitter'
4
- require 'kustomize/emitter/plugin_emitter'
4
+ require 'kustomize/emitter/generator_plugins_emitter'
5
5
 
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/name_digest_autosuffix_transform'
9
+ require 'kustomize/transform/fingerprint_suffix_transform'
10
+ require 'kustomize/transform/ref_fixup_transform'
11
+ require 'kustomize/transform/purge_internal_annotations_transform'
12
+ require 'kustomize/transform/transformer_plugins_transform'
10
13
 
11
14
  class Kustomize::Emitter::DocumentEmitter::KustomizationDocumentEmitter < Kustomize::Emitter::DocumentEmitter
12
15
  def source_directory
@@ -20,18 +23,24 @@ class Kustomize::Emitter::DocumentEmitter::KustomizationDocumentEmitter < Kustom
20
23
  (@doc['bases'] || []) +
21
24
  (@doc['resources'] || [])
22
25
 
23
- gen_pathspecs =
26
+ gen_plugin_pathspecs =
24
27
  (@doc['generators'] || [])
25
28
 
26
29
  input_emitters = rc_pathspecs.map do |rel_path|
27
30
  build_input_emitter(rel_path)
28
31
  end
29
32
 
30
- input_emitters += gen_pathspecs.map do |rel_path|
31
- Kustomize::Emitter::PluginEmitter.new(
32
- build_input_emitter(rel_path),
33
+ gen_plugin_rc_emitters = gen_plugin_pathspecs.map do |rel_path|
34
+ build_input_emitter(rel_path)
35
+ end
36
+
37
+ unless gen_plugin_rc_emitters.empty?
38
+ gen_plugins_emitter = Kustomize::Emitter::GeneratorPluginsEmitter.new(
39
+ gen_plugin_rc_emitters,
33
40
  session: @session
34
41
  )
42
+
43
+ input_emitters.push(gen_plugins_emitter)
35
44
  end
36
45
 
37
46
  @input_emitters = input_emitters
@@ -66,6 +75,24 @@ class Kustomize::Emitter::DocumentEmitter::KustomizationDocumentEmitter < Kustom
66
75
  end
67
76
  end
68
77
 
78
+ def transformer_plugin_transforms
79
+ xformer_plugin_rc_emitters =
80
+ (@doc['transformers'] || []).map do |rel_path|
81
+ build_input_emitter(rel_path)
82
+ end
83
+
84
+ if xformer_plugin_rc_emitters.length > 0
85
+ xform = Kustomize::Transform::TransformerPluginsTransform.create(
86
+ xformer_plugin_rc_emitters,
87
+ session: @session
88
+ )
89
+
90
+ [xform]
91
+ else
92
+ []
93
+ end
94
+ end
95
+
69
96
  def namespace_transforms
70
97
  if new_ns = @doc['namespace']
71
98
  [Kustomize::Transform::NamespaceTransform.create(new_ns)]
@@ -74,24 +101,20 @@ class Kustomize::Emitter::DocumentEmitter::KustomizationDocumentEmitter < Kustom
74
101
  end
75
102
  end
76
103
 
77
- def name_digest_autosuffix_transforms
78
- [Kustomize::Transform::NameDigestAutosuffixTransform.create(self)]
79
- end
80
-
81
104
  def transforms
82
105
  return @transforms if @transforms
83
106
 
84
107
  @transforms = [
85
108
  self.namespace_transforms,
86
109
  self.image_transforms,
87
- self.name_digest_autosuffix_transforms,
88
- self.json_6902_patch_transforms
110
+ self.json_6902_patch_transforms,
111
+ self.transformer_plugin_transforms
89
112
  ].flatten
90
113
  end
91
114
 
92
115
  def emit
93
- self.input_resources.map do |rc|
94
- self.transforms.inject(rc){ |doc, xform| xform.apply(doc) }
116
+ self.transforms.inject(self.input_resources) do |rcs, xform|
117
+ xform.rewrite_all(rcs)
95
118
  end
96
119
  end
97
120
  end
@@ -0,0 +1,31 @@
1
+ require 'kustomize/emitter'
2
+
3
+ require 'kustomize/transform/fingerprint_suffix_transform'
4
+ require 'kustomize/transform/ref_fixup_transform'
5
+ require 'kustomize/transform/purge_internal_annotations_transform'
6
+
7
+ class Kustomize::Emitter::FinalizerEmitter < Kustomize::Emitter
8
+ def initialize(input_emitter)
9
+ @input_emitter = input_emitter
10
+ end
11
+
12
+ def input_emitters
13
+ [@input_emitter]
14
+ end
15
+
16
+ def transforms
17
+ return @transforms if @transforms
18
+
19
+ @transforms = [
20
+ Kustomize::Transform::FingerprintSuffixTransform.instance,
21
+ Kustomize::Transform::RefFixupTransform.instance,
22
+ Kustomize::Transform::PurgeInternalAnnotationsTransform.instance
23
+ ].flatten
24
+ end
25
+
26
+ def emit
27
+ self.transforms.inject(self.input_resources) do |rcs, xform|
28
+ xform.rewrite_all(rcs)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,26 @@
1
+ require 'kustomize/emitter'
2
+
3
+ class Kustomize::Emitter::GeneratorPluginsEmitter < Kustomize::Emitter
4
+ def initialize(plugin_rc_emitters, session:)
5
+ @session = session
6
+ @plugin_rc_emitters = plugin_rc_emitters
7
+ end
8
+
9
+ def plugin_rcs
10
+ @plugin_rc_emitters.flat_map(&:emit)
11
+ end
12
+
13
+ def plugin_instances
14
+ return @plugin_instances if @plugin_instances
15
+
16
+ @plugin_instances =
17
+ self.plugin_rcs.map do |rc|
18
+ plugin_klass = @session.plugin_manager.get(api_version: rc['apiVersion'], kind: rc['kind'])
19
+ plugin_klass.create(rc, session: @session)
20
+ end
21
+ end
22
+
23
+ def emit
24
+ self.plugin_instances.flat_map(&:emit)
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ require 'kustomize/plugin'
2
+
3
+ class Kustomize::GeneratorPlugin < Kustomize::Plugin
4
+ def self.inherited(subklass)
5
+ reg = Kustomize::PluginRegistry.instance
6
+ reg.probe_queue.push(subklass)
7
+ end
8
+
9
+ def emit
10
+ []
11
+ end
12
+
13
+ def to_yaml_stream
14
+ self.emit.map(&:to_yaml).join("")
15
+ end
16
+ end
@@ -1,11 +1,36 @@
1
- module Kustomize; end
1
+ require 'pathname'
2
2
 
3
3
  class Kustomize::Plugin
4
- def initialize(rc)
5
- @rc = rc
4
+ def self.create(rc, session:)
5
+ inst = new(rc)
6
+ inst.session = session
7
+ inst
6
8
  end
7
9
 
8
- def emit
9
- raise NotImplementedError, "Kustomize plugins must implement #emit"
10
+ class << self
11
+ private :new
12
+ end
13
+
14
+ attr_accessor :session
15
+
16
+ def self.match_on(kind: nil, api_version: nil)
17
+ if kind
18
+ @kustomize_plugin_match_kind = kind
19
+ end
20
+
21
+ if api_version
22
+ @kustomize_plugin_match_api_version = api_version
23
+ end
24
+ end
25
+
26
+ def self.kustomize_plugin_match_kind
27
+ return @kustomize_plugin_match_kind if @kustomize_plugin_match_kind
28
+ self.name.split('::').last
29
+ end
30
+
31
+ def self.kustomize_plugin_match_api_version
32
+ return @kustomize_plugin_match_api_version if @kustomize_plugin_match_api_version
33
+ api_dir = Pathname.new(__FILE__).parent
34
+ api_dir.relative_path_from(api_dir.parent.parent).to_s
10
35
  end
11
36
  end
@@ -1,31 +1,46 @@
1
- require 'kustomize/plugin'
1
+ require 'singleton'
2
+
3
+ require 'kustomize/plugin_registry'
2
4
 
3
5
  class Kustomize::PluginManager
4
6
  def initialize(session:)
5
7
  @session = session
6
- @instances = {}
8
+ @registry = Kustomize::PluginRegistry.instance
7
9
  end
8
10
 
9
- def get(api_version, kind)
10
- cache_key = [api_version, kind]
11
- cached_inst = @instances[cache_key]
12
- return cached_inst if cached_inst
11
+ def get(api_version:, kind:)
12
+ plugin_klass = @registry.get(api_version: api_version, kind: kind)
13
+ return plugin_klass if plugin_klass
13
14
 
14
- @instances[cache_key] = self.load(api_version, kind)
15
+ try_loading(api_version, kind)
15
16
  end
16
17
 
17
- def load(api_version, kind)
18
- @session.load_paths
19
- .each{ |prefix| puts(prefix / api_version / "#{kind.downcase}.rb") }
18
+ private
19
+ def try_loading(api_version, kind)
20
+ rel_load_path = Pathname.new(api_version) / "#{underscore(kind)}.rb"
20
21
 
21
- load_path =
22
- @session.load_paths
23
- .map{ |prefix| prefix / api_version / "#{kind.downcase}.rb" }
22
+ abs_load_path =
23
+ @session.effective_load_paths
24
+ .map{ |prefix| prefix / rel_load_path }
24
25
  .find{ |f| f.file? }
25
26
 
26
- raise ArgumentError, "unknown kustomize plugin #{kind}" unless load_path
27
+ raise LoadError, "could not find kustomize plugin to load" unless abs_load_path
28
+
29
+ Kernel.require(abs_load_path)
30
+
31
+ plugin_klass = @registry.get(api_version: api_version, kind: kind)
32
+
33
+ raise LoadError, "#{abs_load_path} did not define expected plugin" unless plugin_klass
34
+
35
+ plugin_klass
36
+ end
27
37
 
28
- Class.new(Kustomize::Plugin)
29
- .tap{ |klass| klass.class_eval(load_path.read) }
38
+ private
39
+ def underscore(str)
40
+ str
41
+ .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
42
+ .gsub(/([a-z\d])([A-Z])/,'\1_\2')
43
+ .tr("-", "_")
44
+ .downcase
30
45
  end
31
46
  end
@@ -0,0 +1,39 @@
1
+ require 'singleton'
2
+
3
+ module Kustomize; end
4
+
5
+ class Kustomize::PluginRegistry
6
+ include Singleton
7
+
8
+ def initialize
9
+ @klasses = {}
10
+ @probe_queue = []
11
+ end
12
+
13
+ attr_reader :probe_queue
14
+
15
+ def get(api_version:, kind:)
16
+ drain_probe_queue!
17
+
18
+ rc_target_id = make_rc_target_id(api_version, kind)
19
+ @klasses[rc_target_id]
20
+ end
21
+
22
+ private
23
+ def drain_probe_queue!
24
+ return if @probe_queue.empty?
25
+
26
+ while plugin_klass = @probe_queue.shift
27
+ rc_target_id = make_rc_target_id(
28
+ plugin_klass.kustomize_plugin_match_api_version,
29
+ plugin_klass.kustomize_plugin_match_kind
30
+ )
31
+ @klasses[rc_target_id] = plugin_klass
32
+ end
33
+ end
34
+
35
+ private
36
+ def make_rc_target_id(api_version, kind)
37
+ [api_version, kind].join('/').to_s.intern
38
+ end
39
+ end
@@ -3,15 +3,18 @@ module Kustomize; end
3
3
  require 'kustomize/plugin_manager'
4
4
 
5
5
  class Kustomize::Session
6
- def plugin_manager
7
- return @plugin_manager if @plugin_manager
6
+ def initialize(load_paths: [])
7
+ @load_paths = load_paths
8
8
  @plugin_manager = Kustomize::PluginManager.new(session: self)
9
9
  end
10
10
 
11
- def load_paths
12
- return @load_paths if @load_paths
13
- @load_paths = [
14
- Pathname.new(__FILE__).expand_path.parent / 'plugin'
15
- ]
11
+ attr_reader :plugin_manager
12
+
13
+ def builtin_load_paths
14
+ [Pathname.new(__FILE__).expand_path.parent / 'builtin_plugins']
15
+ end
16
+
17
+ def effective_load_paths
18
+ @load_paths + self.builtin_load_paths
16
19
  end
17
20
  end
@@ -22,11 +22,11 @@ class Kustomize::TargetSpec
22
22
  end
23
23
 
24
24
  def get_name(rc)
25
- rc.dig('spec', 'name')
25
+ rc.dig('metadata', 'name')
26
26
  end
27
27
 
28
28
  def get_namespace(rc)
29
- rc.dig('spec', 'namespace') || 'default'
29
+ rc.dig('metadata', 'namespace') || 'default'
30
30
  end
31
31
 
32
32
  def match?(rc)
@@ -37,8 +37,8 @@ class Kustomize::TargetSpec
37
37
  end
38
38
 
39
39
  return false if @match_kind and (rc['kind'] != @match_kind)
40
- return false if @match_name and get_name(resource_doc) != @match_name
41
- return false if @match_namespace and get_namespace(resource_doc) != @match_namespace
40
+ return false if @match_name and get_name(rc) != @match_name
41
+ return false if @match_namespace and get_namespace(rc) != @match_namespace
42
42
 
43
43
  true
44
44
  end
@@ -9,7 +9,15 @@ class Kustomize::Transform
9
9
  private :new
10
10
  end
11
11
 
12
- def apply(rc)
12
+ def rewrite_all(rcs)
13
+ rcs.map{ |rc| rewrite(rc) }
14
+ end
15
+
16
+ def rewrite(rc)
13
17
  rc
14
18
  end
19
+
20
+ def inspect
21
+ "#<#{self.class}>"
22
+ end
15
23
  end
@@ -0,0 +1,57 @@
1
+ require 'json'
2
+ require 'digest'
3
+ require 'set'
4
+ require 'singleton'
5
+
6
+ require 'accessory'
7
+ require 'digest/base32'
8
+
9
+ require 'kustomize/transform'
10
+
11
+ class Kustomize::Transform::FingerprintSuffixTransform < Kustomize::Transform
12
+ include Accessory
13
+ include Singleton
14
+
15
+ SUFFIX_JOINER = "-"
16
+
17
+ APPLICABLE_KINDS = Set[
18
+ 'Secret',
19
+ 'SealedSecret',
20
+ 'ConfigMap'
21
+ ]
22
+
23
+ NAME_LENS = Lens["metadata", "name"]
24
+
25
+ CONTENT_LENS_BY_KIND = {
26
+ "Secret" => Lens["data"],
27
+ "ConfigMap" => Lens["data"],
28
+ "SealedSecret" => Lens["spec", "encryptedData"]
29
+ }
30
+
31
+ FINGERPRINT_LENS = Lens['metadata', 'annotations', 'kustomizer.covalenthq.com/effective-fingerprint']
32
+
33
+ def rewrite(rc)
34
+ rc_kind = rc['kind']
35
+ return rc unless APPLICABLE_KINDS.member?(rc_kind)
36
+
37
+ FINGERPRINT_LENS.update_in(rc) do |orig_value|
38
+ next(:keep) if orig_value
39
+
40
+ content_part = CONTENT_LENS_BY_KIND[rc_kind].get_in(rc)
41
+ content_ser = content_part.to_json
42
+ fingerprint = Digest::SHA256.base32digest(content_ser, :zbase32)[0, 6]
43
+
44
+ [:set, fingerprint]
45
+ end
46
+
47
+ base_name = NAME_LENS.get_in(rc)
48
+ fingerprint = FINGERPRINT_LENS.get_in(rc)
49
+
50
+ NAME_LENS.update_in(rc) do |base_name|
51
+ suffixed_name = [base_name, fingerprint].join(SUFFIX_JOINER)
52
+ [:set, suffixed_name]
53
+ end
54
+
55
+ rc
56
+ end
57
+ end
@@ -27,7 +27,7 @@ class Kustomize::Transform::ImageTransform < Kustomize::Transform
27
27
  "Deployment" => Lens["spec", "template", "spec", "containers", Access.all, "image"]
28
28
  }
29
29
 
30
- def apply(rc_doc)
30
+ def rewrite(rc_doc)
31
31
  lens = LENS_BY_KIND[rc_doc['kind']]
32
32
  return rc_doc unless lens
33
33
 
@@ -44,7 +44,7 @@ class Kustomize::Transform::Json6902PatchTransform < Kustomize::Transform
44
44
  attr_reader :target
45
45
  attr_reader :patches
46
46
 
47
- def apply(resource_doc)
47
+ def rewrite(resource_doc)
48
48
  if @target.match?(resource_doc)
49
49
  @patches.inject(resource_doc){ |doc, patch| patch.apply(doc) }
50
50
  else
@@ -29,7 +29,7 @@ class Kustomize::Transform::NamespaceTransform < Kustomize::Transform
29
29
  ]
30
30
  }
31
31
 
32
- def apply(rc_doc)
32
+ def rewrite(rc_doc)
33
33
  rc_kind = rc_doc['kind']
34
34
  use_lenses = []
35
35
 
@@ -0,0 +1,31 @@
1
+ require 'singleton'
2
+
3
+ require 'accessory'
4
+
5
+ require 'kustomize/transform'
6
+
7
+ class Kustomize::Transform::PurgeInternalAnnotationsTransform < Kustomize::Transform
8
+ include Accessory
9
+ include Singleton
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]
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,59 @@
1
+ require 'singleton'
2
+
3
+ require 'accessory'
4
+
5
+ require 'kustomize/transform'
6
+
7
+ class Kustomize::Transform::RefFixupTransform < Kustomize::Transform
8
+ include Accessory
9
+ include Singleton
10
+
11
+ SUFFIX_JOINER = "-"
12
+
13
+ NAME_LENS = Lens["metadata", "name"]
14
+
15
+ FINGERPRINT_LENS = Lens['metadata', 'annotations', 'kustomizer.covalenthq.com/effective-fingerprint']
16
+
17
+ POD_TEMPLATE_LENSES = [
18
+ Lens["spec", "template", "spec", "containers", Access.all, "envFrom", Access.all, "configMapRef", "name"],
19
+ Lens["spec", "template", "spec", "containers", Access.all, "env", Access.all, "valueFrom", "configMapKeyRef", "name"],
20
+ Lens["spec", "template", "spec", "volumes", Access.all, "configMap", "name"],
21
+
22
+ Lens["spec", "template", "spec", "containers", Access.all, "env", Access.all, "valueFrom", "secretKeyRef", "name"],
23
+ Lens["spec", "template", "spec", "volumes", Access.all, "secret", "name"],
24
+ Lens["spec", "template", "spec", "volumes", Access.all, "secret", "secretName"]
25
+ ]
26
+
27
+ KEY_REF_LENSES_BY_KIND = {
28
+ "Deployment" => POD_TEMPLATE_LENSES,
29
+ "StatefulSet" => POD_TEMPLATE_LENSES,
30
+ "DaemonSet" => POD_TEMPLATE_LENSES
31
+ }
32
+
33
+ def rewrite_all(rcs)
34
+ ref_fixups =
35
+ rcs.flat_map do |rc|
36
+ fingerprint = FINGERPRINT_LENS.get_in(rc)
37
+ next([]) unless fingerprint
38
+
39
+ suffixed_name = NAME_LENS.get_in(rc)
40
+ base_name = suffixed_name.gsub(/-#{fingerprint}$/, '')
41
+ [[base_name, suffixed_name]]
42
+ end.to_h
43
+
44
+ rcs.map do |rc|
45
+ key_ref_lenses = KEY_REF_LENSES_BY_KIND[rc['kind']]
46
+ next(rc) unless key_ref_lenses
47
+
48
+ key_ref_lenses.inject(rc) do |doc, lens|
49
+ lens.update_in(doc) do |base_name|
50
+ if suffixed_name = ref_fixups[base_name]
51
+ [:set, suffixed_name]
52
+ else
53
+ :keep
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,28 @@
1
+ require 'kustomize/transform'
2
+
3
+ class Kustomize::Transform::TransformerPluginsTransform < Kustomize::Transform
4
+ def initialize(plugin_rc_emitters, session:)
5
+ @session = session
6
+ @plugin_rc_emitters = plugin_rc_emitters
7
+ end
8
+
9
+ def plugin_rcs
10
+ @plugin_rc_emitters.flat_map(&:emit)
11
+ end
12
+
13
+ def plugin_instances
14
+ return @plugin_instances if @plugin_instances
15
+
16
+ @plugin_instances =
17
+ self.plugin_rcs.map do |rc|
18
+ plugin_klass = @session.plugin_manager.get(api_version: rc['apiVersion'], kind: rc['kind'])
19
+ plugin_klass.create(rc, session: @session)
20
+ end
21
+ end
22
+
23
+ def rewrite_all(rcs)
24
+ self.plugin_instances.inject(rcs) do |docs, plugin_inst|
25
+ plugin_inst.rewrite_all(rcs)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ require 'kustomize/plugin'
2
+
3
+ class Kustomize::TransformerPlugin < Kustomize::Plugin
4
+ def self.inherited(subklass)
5
+ reg = Kustomize::PluginRegistry.instance
6
+ reg.probe_queue.push(subklass)
7
+ end
8
+
9
+ def rewrite_all(rcs)
10
+ rcs.map{ |rc| rewrite(rc) }
11
+ end
12
+
13
+ def rewrite(rc)
14
+ rc
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module Kustomize
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.6"
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.1
4
+ version: 0.1.6
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-01-16 00:00:00.000000000 Z
11
+ date: 2021-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: accessory
@@ -56,12 +56,16 @@ files:
56
56
  - bin/setup
57
57
  - kustomizer.gemspec
58
58
  - lib/kustomize.rb
59
+ - lib/kustomize/builtin_plugins/kustomizer.covalenthq.com/v1/test_generator.rb
60
+ - lib/kustomize/builtin_plugins/kustomizer.covalenthq.com/v1/test_transformer.rb
59
61
  - lib/kustomize/emitter.rb
60
62
  - lib/kustomize/emitter/directory_emitter.rb
61
63
  - lib/kustomize/emitter/document_emitter.rb
62
64
  - lib/kustomize/emitter/document_emitter/kustomization_document_emitter.rb
63
65
  - lib/kustomize/emitter/file_emitter.rb
64
- - lib/kustomize/emitter/plugin_emitter.rb
66
+ - lib/kustomize/emitter/finalizer_emitter.rb
67
+ - lib/kustomize/emitter/generator_plugins_emitter.rb
68
+ - lib/kustomize/generator_plugin.rb
65
69
  - lib/kustomize/json_6902_patch.rb
66
70
  - lib/kustomize/json_6902_patch/add_op.rb
67
71
  - lib/kustomize/json_6902_patch/gsub_op.rb
@@ -70,13 +74,18 @@ files:
70
74
  - lib/kustomize/pathname_refinements.rb
71
75
  - lib/kustomize/plugin.rb
72
76
  - lib/kustomize/plugin_manager.rb
77
+ - lib/kustomize/plugin_registry.rb
73
78
  - lib/kustomize/session.rb
74
79
  - lib/kustomize/target_spec.rb
75
80
  - lib/kustomize/transform.rb
81
+ - lib/kustomize/transform/fingerprint_suffix_transform.rb
76
82
  - lib/kustomize/transform/image_transform.rb
77
83
  - lib/kustomize/transform/json_6902_patch_transform.rb
78
- - lib/kustomize/transform/name_digest_autosuffix_transform.rb
79
84
  - lib/kustomize/transform/namespace_transform.rb
85
+ - lib/kustomize/transform/purge_internal_annotations_transform.rb
86
+ - lib/kustomize/transform/ref_fixup_transform.rb
87
+ - lib/kustomize/transform/transformer_plugins_transform.rb
88
+ - lib/kustomize/transformer_plugin.rb
80
89
  - lib/kustomize/version.rb
81
90
  homepage: https://github.com/tsutsu/kustomize
82
91
  licenses:
@@ -1,30 +0,0 @@
1
- require 'kustomize/emitter'
2
-
3
- class Kustomize::Emitter::PluginEmitter < Kustomize::Emitter
4
- def initialize(input_emitter, session:)
5
- @session = session
6
- @input_emitter = input_emitter
7
- end
8
-
9
- def source_directory
10
- @source_path.parent
11
- end
12
-
13
- def input_emitters
14
- [@input_emitter]
15
- end
16
-
17
- def plugin_instances
18
- return @plugin_instances if @plugin_instances
19
-
20
- @plugin_instances =
21
- self.input_resources.map do |rc|
22
- plugin_klass = @session.plugin_manager.get(rc['apiVersion'], rc['kind'])
23
- plugin_klass.new(rc)
24
- end
25
- end
26
-
27
- def emit
28
- self.plugin_instances.flat_map(&:emit)
29
- end
30
- end
@@ -1,83 +0,0 @@
1
- require 'json'
2
- require 'digest'
3
- require 'set'
4
-
5
- require 'accessory'
6
- require 'digest/base32'
7
-
8
- require 'kustomize/transform'
9
- require 'kustomize/emitter/document_emitter/kustomization_document_emitter'
10
-
11
- class Kustomize::Transform::NameDigestAutosuffixTransform < Kustomize::Transform
12
- include Accessory
13
-
14
- SUFFIX_JOINER = "-"
15
-
16
- def self.create(kustomize_doc)
17
- raise ArgumentError unless kustomize_doc.kind_of?(Kustomize::Emitter::DocumentEmitter::KustomizationDocumentEmitter)
18
- self.new(kustomize_doc)
19
- end
20
-
21
- def initialize(kustomize_doc)
22
- @kustomize_doc = kustomize_doc
23
- end
24
-
25
- SECRET_KINDS = Set[
26
- 'Secret',
27
- 'SealedSecret'
28
- ]
29
-
30
- def suffixes
31
- return @suffixes if @suffixes
32
-
33
- secret_docs =
34
- @kustomize_doc.input_resources
35
- .filter{ |rc| SECRET_KINDS.member?(rc['kind']) }
36
-
37
- @suffixes =
38
- secret_docs.map do |rc|
39
- rc_kind = rc['kind']
40
-
41
- secret_name = NAME_LENSES_BY_KIND[rc_kind].first.get_in(rc)
42
- secret_content = CONTENT_LENSES_BY_KIND[rc_kind].get_in(rc)
43
- content_hash_suffix = Digest::SHA256.base32digest(secret_content.to_json, :zbase32)[0, 8]
44
-
45
- [secret_name, content_hash_suffix]
46
- end.to_h
47
- end
48
-
49
- CONTENT_LENSES_BY_KIND = {
50
- "Secret" => Lens["data"],
51
- "SealedSecret" => Lens["spec", "encryptedData"]
52
- }
53
-
54
- NAME_LENSES_BY_KIND = {
55
- "Deployment" => [
56
- Lens["spec", "template", "spec", "containers", Access.all, "env", Access.all, "valueFrom", "secretKeyRef", "name"]
57
- ],
58
-
59
- "Secret" => [
60
- Lens["metadata", "name"]
61
- ],
62
-
63
- "SealedSecret" => [
64
- Lens["spec", "template", "metadata", "name"]
65
- ]
66
- }
67
-
68
- def apply(rc_doc)
69
- name_lenses = NAME_LENSES_BY_KIND[rc_doc['kind']]
70
- return rc_doc unless name_lenses
71
-
72
- name_lenses.inject(rc_doc) do |doc, lens|
73
- lens.update_in(doc) do |orig_name|
74
- if self.suffixes.has_key?(orig_name)
75
- new_name = [orig_name, self.suffixes[orig_name]].join('-')
76
- [:set, new_name]
77
- else
78
- :keep
79
- end
80
- end
81
- end
82
- end
83
- end