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
-
#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
-
#chart(chart_id, **opts) ⇒ Array<Hash>
Run ‘helm template’ on Helm project, parse the output into dicts, return processed and filtered list via #dicts.
-
#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.
-
#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, #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
28 29 30 31 32 33 34 35 36 |
# File 'lib/main/mixer.rb', line 28 def initialize(values, opts={}) @output = [] @release_name = opts[:release_name] || "default" @patch_stack = [] @values = self.class.compute_own_values_subtree( values, opts[:overwrite_values_root] ) end |
Instance Attribute Details
#output ⇒ Array<Hash> (readonly)
Array of res-hashes being aggregated
18 19 20 |
# File 'lib/main/mixer.rb', line 18 def output @output end |
#patch_stack ⇒ Array<Hash>
Array of patches to be applied to results
23 24 25 |
# File 'lib/main/mixer.rb', line 23 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
182 183 184 185 186 187 |
# File 'lib/main/mixer.rb', line 182 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.
196 197 198 199 200 201 202 203 |
# File 'lib/main/mixer.rb', line 196 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__”
226 227 228 |
# File 'lib/main/mixer.rb', line 226 def locate_self(dirname) @dir_location = dirname end |
.pwd ⇒ String
Returns the value set by locate_self
233 234 235 |
# File 'lib/main/mixer.rb', line 233 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.
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/main/mixer.rb', line 209 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’.
178 179 180 |
# File 'lib/main/mixer.rb', line 178 def values_root(deep_key) @vals_root_deep_key = deep_key end |
Instance Method Details
#chart(chart_id, **opts) ⇒ Array<Hash>
Run ‘helm template’ on Helm project, parse the output into dicts, return processed and filtered list via #dicts.
110 111 112 113 114 |
# File 'lib/main/mixer.rb', line 110 def chart(chart_id, **opts) release = opts[:release] || release_name helm_output = Utils::Helm.template(release, chart_id, **opts) dicts(helm_output) end |
#dicts(dict, **opts) ⇒ Object Also known as: dict
Normalizes, sanitizes and filters a dict or an array of dicts.
71 72 73 74 75 |
# File 'lib/main/mixer.rb', line 71 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
95 96 97 98 99 100 101 102 |
# File 'lib/main/mixer.rb', line 95 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.
84 85 86 87 88 89 90 |
# File 'lib/main/mixer.rb', line 84 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 |
#mix ⇒ Object
55 56 |
# File 'lib/main/mixer.rb', line 55 def mix end |
#mixer(klass, **opts) ⇒ Array<Hash>
Run another mixer given by klass, return processed and filtered list via #dicts.
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/main/mixer.rb', line 122 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, #chart) executed in the &block passed to this method will have their return values deep merged with the dict(s) passed.
140 141 142 143 144 145 |
# File 'lib/main/mixer.rb', line 140 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>.
62 63 64 65 |
# File 'lib/main/mixer.rb', line 62 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
45 46 47 48 49 50 51 52 53 |
# File 'lib/main/mixer.rb', line 45 def run begin self.mix rescue Error => e puts "Exception below caused by mixer #{self.class.name}" raise e end self.output end |