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



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

#outputArray<Hash> (readonly)

Array of res-hashes being aggregated

Returns:

  • (Array<Hash>)

    list of hashes



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

def output
  @output
end

#patch_stackArray<Hash>

Array of patches to be applied to results

Returns:

  • (Array<Hash>)

    list of hashes



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

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



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.

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



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

Parameters:

  • dirname (String)

    absolute path of the directory



226
227
228
# File 'lib/main/mixer.rb', line 226

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



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.

Parameters:

  • fname_expr (String)

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

Returns:

  • (?String)


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.

Parameters:

  • chart_id (String)

    using format ‘jetstack/cert-manager’

  • opts (Hash)

    filtering and other options for #dicts

Returns:

  • (Array<Hash>)

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

Parameters:

  • dict (Hash | Array<Hash>)

    the hash to be added



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

Parameters:

  • fname (String)
  • opts (Hash)

    filtering and other options for #dicts

Returns:

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

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



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

#mixObject



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.

Parameters:

  • klass (Class<Kerbi::Mixer>)

    other mixer’s class

  • opts (Hash)

    filtering and other options for #dicts

Returns:

  • (Array<Hash>)

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

Parameters:

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

Yields:

  • (block)

Returns:

  • (Array<Hash>, Hash)


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

Parameters:

  • dict (Hash | Array<Hash>)

    the hash to be added



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

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



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