Module: Kerbi::Utils::Helm

Defined in:
lib/utils/helm.rb

Constant Summary collapse

HELM_EXEC =
"helm"
TMP_VALUES_PATH =
"/tmp/kerbi-helm-tmp.yaml"

Class Method Summary collapse

Class Method Details

.can_exec?Boolean

Tests whether Kerbi can invoke Helm commands

Returns:

  • (Boolean)

    true if helm commands succeed locally



11
12
13
# File 'lib/utils/helm.rb', line 11

def self.can_exec?
  !!system(HELM_EXEC, out: File::NULL, err: File::NULL)
end

.del_tmp_values_filevoid

This method returns an undefined value.

Deletes the temp file



28
29
30
31
32
# File 'lib/utils/helm.rb', line 28

def self.del_tmp_values_file
  if File.exists?(TMP_VALUES_PATH)
    File.delete(TMP_VALUES_PATH)
  end
end

.encode_inline_assigns(inline_assigns) ⇒ String

Joins assignments in flat hash into list of –set flags

Parameters:

  • inline_assigns (Hash)

    flat Hash of deep_key: val

Returns:

  • (String)

    corresponding space-separated –set flags



38
39
40
41
42
43
# File 'lib/utils/helm.rb', line 38

def self.encode_inline_assigns(inline_assigns)
  (inline_assigns || []).map do |key, value|
    raise "Assignments must be flat"  if value.is_a?(Hash)
    "--set #{key}=#{value}"
  end.join(" ")
end

.make_tmp_values_file(values) ⇒ String

Writes a hash of values to a YAML to a temp file

Parameters:

  • values (Hash)

    a hash of values

Returns:

  • (String)

    the path of the file



19
20
21
22
23
# File 'lib/utils/helm.rb', line 19

def self.make_tmp_values_file(values)
  content = YAML.dump((values || {}).deep_stringify_keys)
  File.write(TMP_VALUES_PATH, content)
  TMP_VALUES_PATH
end

.template(release, project, opts = {}) ⇒ Array<Hash>

Runs the helm template command

Parameters:

  • release (String)

    release name to pass to Helm

  • project (String)

    <org>/<chart> string identifying helm chart

Returns:

  • (Array<Hash>)


50
51
52
53
54
55
56
57
58
59
# File 'lib/utils/helm.rb', line 50

def self.template(release, project, opts={})
  raise "Helm executable not working" unless can_exec?
  tmp_file = make_tmp_values_file(opts[:values])
  inline_flags = encode_inline_assigns(opts[:inline_assigns])
  command = "#{HELM_EXEC} template #{release} #{project}"
  command += " -f #{tmp_file} #{inline_flags} #{opts[:cli_args]}"
  output = `#{command}`
  del_tmp_values_file
  YAML.load_stream(output)
end