Class: Kerbi::Mixer

Inherits:
Object
  • Object
show all
Includes:
Kerbi::Mixins::Mixer
Defined in:
lib/main/mixer.rb

Direct Known Subclasses

State::ConfigMapMixer, State::NamespaceMixer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Kerbi::Mixins::Mixer

#b64dec, #b64enc, #b64enc_file, #embed, #embed_array, #http_descriptor_to_url

Constructor Details

#initialize(values, opts = {}) ⇒ Mixer

Constructor

Parameters:

  • values (Hash)

    the values tree that will be accessible to the subclass



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

#namespaceString (readonly)

Namespace (defaults to release_name) from CLI options

Returns:

  • (String)

    symbol-keyed hash



18
19
20
# File 'lib/main/mixer.rb', line 18

def namespace
  @namespace
end

#outputArray<Hash> (readonly)

Array of res-hashes being aggregated

Returns:

  • (Array<Hash>)

    list of hashes



23
24
25
# File 'lib/main/mixer.rb', line 23

def output
  @output
end

#patch_stackArray<Hash>

Array of patches to be applied to results

Returns:

  • (Array<Hash>)

    list of hashes



28
29
30
# File 'lib/main/mixer.rb', line 28

def patch_stack
  @patch_stack
end

#release_nameString (readonly)

Release name available for templating

Returns:

  • (String)

    symbol-keyed hash



13
14
15
# File 'lib/main/mixer.rb', line 13

def release_name
  @release_name
end

#valuesImmutable::Hash (readonly)

Values hash available to subclasses

Returns:

  • (Immutable::Hash)

    symbol-keyed hash



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.

Parameters:

  • values_root (Hash)

    dict from which to extract subtree

  • deep_key (String)

    key in dict in “x.y.z” format

Returns:

  • (Hash)

    frozen and deep-cloned subtree



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__”

Parameters:

  • dirname (String)

    absolute path of the directory



232
233
234
# File 'lib/main/mixer.rb', line 232

def locate_self(dirname)
  @dir_location = dirname
end

.pwdString

Returns the value set by locate_self

Returns:

  • (String)

    the subclass’ pwd as defined by the user



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.

Parameters:

  • fname_expr (String)

    e.g ‘pod’, ‘pod.yaml’

Returns:

  • (?String)


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.

Parameters:

  • dict (Hash | Array<Hash>)

    the hash to be added



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

Parameters:

  • fname (String)
  • opts (Hash)

    filtering and other options for #dicts

Returns:

  • (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.

Parameters:

  • fname (String)

    with or without extension, relative to self

  • opts (Hash)

    filtering and other options for #dicts

Returns:

  • (Array<Hash>)

    processed dicts read from file



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.

Parameters:

  • chart_id (String)

    using format ‘jetstack/cert-manager’

  • opts (Hash)

    filtering and other options for #dicts

Returns:

  • (Array<Hash>)

    processed and filtered 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

#mixObject



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.

Parameters:

  • klass (Class<Kerbi::Mixer>)

    other mixer’s class

  • opts (Hash)

    filtering and other options for #dicts

Returns:

  • (Array<Hash>)

    processed and filtered 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.

Parameters:

  • dict (Array<Hash>|Hash)
  • block (Proc)

Yields:

  • (block)

Returns:

  • (Array<Hash>, Hash)


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>.

Parameters:

  • dict (Hash | Array<Hash>)

    the hash to be added



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

Yields:

  • (bucket)

    Exec context in which hashes are collected into one bucket

Yield Parameters:

  • g (Kerbi::ResBucket)

    Bucket object with essential methods

Yield Returns:

  • (Array<Hash>)

    array of hashes representing Kubernetes resources

Returns:

  • (Array<Hash>)

    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