Class: Kerbi::Mixer
- Inherits:
-
Object
- Object
- Kerbi::Mixer
- Includes:
- Kerbi::Mixins::Mixer
- Defined in:
- lib/main/mixer.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#namespace ⇒ String
readonly
Namespace (defaults to release_name) from CLI options.
-
#output ⇒ Array<Hash>
readonly
Array of res-hashes being aggregated.
-
#patch_stack ⇒ Array<Hash>
Array of patches to be applied to results.
-
#release_name ⇒ String
readonly
Release name available for templating.
-
#values ⇒ Immutable::Hash
readonly
Values hash available to subclasses.
Class Method Summary collapse
- .compute_own_values_subtree(values_root, override) ⇒ Object
-
.compute_values_subtree(values_root, deep_key) ⇒ Hash
Given a values_root dict and a deep key (e.g “x.y.z”), outputs a frozen, deep-cloned, subtree corresponding to the deep key’s position in the values_root.
-
.locate_self(dirname) ⇒ void
Sets the absolute path of the directory where yamls used by this Gen can be found, usually “__dir__”.
-
.pwd ⇒ String
Returns the value set by locate_self.
-
.resolve_file_name(fname_expr) ⇒ ?String
Resolves a user-given short name for a file to interpolate, like ‘pod’, ‘pod.yaml’, into an absolute file path.
-
.values_root(deep_key) ⇒ Object
Pass a deep key that will be used to dig into the values dict the mixer gets upon initialization.
Instance Method Summary collapse
-
#dicts(dict, **opts) ⇒ Object
(also: #dict)
Normalizes, sanitizes and filters a dict or an array of dicts.
- #dir(fname, **opts) ⇒ Array
-
#file(fname, **opts) ⇒ Array<Hash>
Loads a YAML/JSON/ERB file, parses it, interpolates it, and returns processed and filtered list of dicts via #dicts.
-
#helm_chart(chart_id, **opts) ⇒ Array<Hash>
Run ‘helm template’ on Helm project, parse the output into dicts, return processed and filtered list via #dicts.
-
#initialize(values, opts = {}) ⇒ Mixer
constructor
Constructor.
- #mix ⇒ Object
-
#mixer(klass, **opts) ⇒ Array<Hash>
Run another mixer given by klass, return processed and filtered list via #dicts.
-
#patched_with(dict, &block) {|block| ... } ⇒ Array<Hash>, Hash
Any x-to-dict statements (e.g #dicts, #dir, #helm_chart) executed in the &block passed to this method will have their return values deep merged with the dict(s) passed.
-
#push(dicts) ⇒ Object
Registers a dict or an array of dicts that will part of the mixers’s final output, which is an Array<Hash>.
-
#run {|bucket| ... } ⇒ Array<Hash>
Where users should return a hash or an array of hashes representing Kubernetes resources.
Methods included from Kerbi::Mixins::Mixer
#b64dec, #b64enc, #b64enc_file, #embed, #embed_array, #http_descriptor_to_url
Constructor Details
#initialize(values, opts = {}) ⇒ Mixer
Constructor
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/main/mixer.rb', line 33 def initialize(values, opts={}) @output = [] @release_name = opts[:release_name] @namespace = opts[:namespace] || @release_name @patch_stack = [] @values = self.class.compute_own_values_subtree( values, opts[:overwrite_values_root] ).freeze end |
Instance Attribute Details
#namespace ⇒ String (readonly)
Namespace (defaults to release_name) from CLI options
18 19 20 |
# File 'lib/main/mixer.rb', line 18 def namespace @namespace end |
#output ⇒ Array<Hash> (readonly)
Array of res-hashes being aggregated
23 24 25 |
# File 'lib/main/mixer.rb', line 23 def output @output end |
#patch_stack ⇒ Array<Hash>
Array of patches to be applied to results
28 29 30 |
# File 'lib/main/mixer.rb', line 28 def patch_stack @patch_stack end |
#release_name ⇒ String (readonly)
Release name available for templating
13 14 15 |
# File 'lib/main/mixer.rb', line 13 def release_name @release_name end |
#values ⇒ Immutable::Hash (readonly)
Values hash available to subclasses
8 9 10 |
# File 'lib/main/mixer.rb', line 8 def values @values end |
Class Method Details
.compute_own_values_subtree(values_root, override) ⇒ Object
188 189 190 191 192 193 |
# File 'lib/main/mixer.rb', line 188 def compute_own_values_subtree(values_root, override) self.compute_values_subtree( values_root, override ? nil : @vals_root_deep_key ) end |
.compute_values_subtree(values_root, deep_key) ⇒ Hash
Given a values_root dict and a deep key (e.g “x.y.z”), outputs a frozen, deep-cloned, subtree corresponding to the deep key’s position in the values_root.
202 203 204 205 206 207 208 209 |
# File 'lib/main/mixer.rb', line 202 def compute_values_subtree(values_root, deep_key) subtree = values_root.deep_dup if deep_key.present? deep_key_parts = deep_key.split(".") subtree = subtree.dig(*deep_key_parts) end subtree.freeze end |
.locate_self(dirname) ⇒ void
This method returns an undefined value.
Sets the absolute path of the directory where yamls used by this Gen can be found, usually “__dir__”
232 233 234 |
# File 'lib/main/mixer.rb', line 232 def locate_self(dirname) @dir_location = dirname end |
.pwd ⇒ String
Returns the value set by locate_self
239 240 241 |
# File 'lib/main/mixer.rb', line 239 def pwd @dir_location end |
.resolve_file_name(fname_expr) ⇒ ?String
Resolves a user-given short name for a file to interpolate, like ‘pod’, ‘pod.yaml’, into an absolute file path.
215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/main/mixer.rb', line 215 def resolve_file_name(fname_expr) dir = self.pwd Kerbi::Utils::Misc.real_files_for( fname_expr, "#{fname_expr}.yaml", "#{fname_expr}.yaml.erb", "#{dir}/#{fname_expr}", "#{dir}/#{fname_expr}.yaml", "#{dir}/#{fname_expr}.yaml.erb" ).first end |
.values_root(deep_key) ⇒ Object
Pass a deep key that will be used to dig into the values dict the mixer gets upon initialization. For example if deep_key is “x”, then if the mixer is initialized with values as x: ‘z’, then its final values attribute will be ‘z’.
184 185 186 |
# File 'lib/main/mixer.rb', line 184 def values_root(deep_key) @vals_root_deep_key = deep_key end |
Instance Method Details
#dicts(dict, **opts) ⇒ Object Also known as: dict
Normalizes, sanitizes and filters a dict or an array of dicts.
77 78 79 80 81 |
# File 'lib/main/mixer.rb', line 77 def dicts(dict, **opts) output = Utils::Mixing.clean_and_filter_dicts(dict, **opts) should_patch = opts[:no_patch].blank? should_patch ? apply_patch_context(output) : output end |
#dir(fname, **opts) ⇒ Array
101 102 103 104 105 106 107 108 |
# File 'lib/main/mixer.rb', line 101 def dir(fname, **opts) output = Utils::Mixing.yamls_in_dir_to_dicts( self.class.pwd, resolve_file_name(fname), **opts ) dicts(output) end |
#file(fname, **opts) ⇒ Array<Hash>
Loads a YAML/JSON/ERB file, parses it, interpolates it, and returns processed and filtered list of dicts via #dicts.
90 91 92 93 94 95 96 |
# File 'lib/main/mixer.rb', line 90 def file(fname, **opts) output = Utils::Mixing.yaml_file_to_dicts( self.class.resolve_file_name(fname), **opts.merge({src_binding: binding}) ) dicts(output) end |
#helm_chart(chart_id, **opts) ⇒ Array<Hash>
Run ‘helm template’ on Helm project, parse the output into dicts, return processed and filtered list via #dicts.
116 117 118 119 120 |
# File 'lib/main/mixer.rb', line 116 def helm_chart(chart_id, **opts) release = opts[:release] || release_name helm_output = Utils::Helm.template(release, chart_id, **opts) dicts(helm_output) end |
#mix ⇒ Object
61 62 |
# File 'lib/main/mixer.rb', line 61 def mix end |
#mixer(klass, **opts) ⇒ Array<Hash>
Run another mixer given by klass, return processed and filtered list via #dicts.
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/main/mixer.rb', line 128 def mixer(klass, **opts) force_subtree = opts.delete(:values) mixer_inst = klass.new( force_subtree.nil? ? values : force_subtree, release_name: release_name, overwrite_values_subtree: !force_subtree.nil? ) output = mixer_inst.run dicts(output) end |
#patched_with(dict, &block) {|block| ... } ⇒ Array<Hash>, Hash
Any x-to-dict statements (e.g #dicts, #dir, #helm_chart) executed in the &block passed to this method will have their return values deep merged with the dict(s) passed.
146 147 148 149 150 151 |
# File 'lib/main/mixer.rb', line 146 def patched_with(dict, &block) new_patches = extract_patches(dict) patch_stack.push(new_patches) yield(block) patch_stack.pop end |
#push(dicts) ⇒ Object
Registers a dict or an array of dicts that will part of the mixers’s final output, which is an Array<Hash>.
68 69 70 71 |
# File 'lib/main/mixer.rb', line 68 def push(dicts) final_list = Utils::Mixing.sanitize_res_dict_list(dicts) self.output.append(*final_list) end |
#run {|bucket| ... } ⇒ Array<Hash>
Where users should return a hash or an array of hashes representing Kubernetes resources
51 52 53 54 55 56 57 58 59 |
# File 'lib/main/mixer.rb', line 51 def run begin self.mix rescue Error => e puts "Exception below caused by mixer #{self.class.name}" raise e end self.output end |