Class: HelmWrapper::Shared::Variables

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/helm-wrapper/shared/variables.rb

Constant Summary collapse

@@variable_files_name =
"helmvars"
@@variable_files_exts =
[ ".yaml", ".yml" ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, logger_for

Constructor Details

#initialize(chart:, config:, namespace:, release:, identifiers: Hash.new, sort: false) ⇒ Variables



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/helm-wrapper/shared/variables.rb', line 31

def initialize(chart:, config:, namespace:, release:, identifiers: Hash.new, sort: false)
  logger.fatal("Identifiers provided must be a hash!") unless identifiers.kind_of?(Hash)

  core             = Hash.new()
  core[:chart]     = chart
  core[:config]    = config
  core[:namespace] = nil
  core[:release]   = release

  user = cleanse(variables: identifiers, reserved: core.keys, downcase: true)

  begin
    core[:namespace] = namespace % user
  rescue
    logger.fatal("Provided namespace includes identifiers that are not included in the configuration file!")
  end

  merged = core.merge(user)

  @core        = core
  @identifiers = sort ? merged.sort.to_h : merged
  @values      = Hash.new
  @files       = Array.new
end

Instance Attribute Details

#coreObject (readonly)

Returns the value of attribute core.



24
25
26
# File 'lib/helm-wrapper/shared/variables.rb', line 24

def core
  @core
end

#filesObject (readonly)

Returns the value of attribute files.



25
26
27
# File 'lib/helm-wrapper/shared/variables.rb', line 25

def files
  @files
end

#identifiersObject (readonly)

Returns the value of attribute identifiers.



26
27
28
# File 'lib/helm-wrapper/shared/variables.rb', line 26

def identifiers
  @identifiers
end

#valuesObject (readonly)

Returns the value of attribute values.



27
28
29
# File 'lib/helm-wrapper/shared/variables.rb', line 27

def values
  @values
end

Instance Method Details

#add_files(base:, files:) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/helm-wrapper/shared/variables.rb', line 58

def add_files(base:, files:)
  logger.fatal("Variable files provided must be an array!") unless files.kind_of?(Array)

  files.each do |file|
    logger.fatal("All provided variable file names must be strings!") unless file.kind_of?(String)
    logger.fatal("All provided variable file names must not be blank!") if file.strip.empty?

    path = ::HelmWrapper.find(base: File.join(base, @@variable_files_name), name: file.strip, exts: @@variable_files_exts, description: "Helm values file")

    if @files.include?(path) then
      logger.warn("Helm variables file is included more than once: #{file.strip}")
    else
      @files.append(path)
    end
  end
end

#add_variables(variables:, sort: false) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/helm-wrapper/shared/variables.rb', line 77

def add_variables(variables:, sort: false)
  logger.fatal("Variables provided must be a hash!") unless variables.kind_of?(Hash)

  cleansed = cleanse(variables: variables, reserved: @values.keys)

  begin
    cleansed = cleansed.map{ |key, value| [ key, value % @identifiers ] }.to_h
  rescue
    logger.fatal("Variables contain identifiers that are not included in the configuration file!")
  end

  merged  = @values.merge(cleansed)
  @values = sort ? merged.sort.to_h : merged
end

#clear_filesObject



94
95
96
# File 'lib/helm-wrapper/shared/variables.rb', line 94

def clear_files()
  @files = Array.new
end

#clear_variablesObject



100
101
102
# File 'lib/helm-wrapper/shared/variables.rb', line 100

def clear_variables()
 @values = Hash.new
end