Module: Kubes::Compiler::Shared::RuntimeHelpers

Includes:
DslEvaluator, Helpers, Util::Pretty
Included in:
Dsl::Core::Base, Kubes::Compiler::Strategy::Erb
Defined in:
lib/kubes/compiler/shared/runtime_helpers.rb

Constant Summary collapse

@@custom_helpers_loaded =
false
@@plugin_helpers_loaded =

Load plugin helper methods from project

false
@@custom_variables_loaded =

Load custom variables from project

false
@@header_shown =

key is main @path

{}
@@shown =
{}

Instance Method Summary collapse

Methods included from Util::Pretty

#pretty_path

Methods included from Helpers::SecretHelper

#decode64, #encode64, #generic_secret_data, #parse_env_like_line

Methods included from Helpers::ExtraHelper

#extra, #with_extra

Methods included from Helpers::DockerHelper

#built_image, #built_image_helper, #docker_image

Methods included from Helpers::ConfigMapHelper

#config_map_files

Instance Method Details

#base_class_for_helperObject



37
38
39
40
41
42
43
# File 'lib/kubes/compiler/shared/runtime_helpers.rb', line 37

def base_class_for_helper
  if self.is_a?(Kubes::Compiler::Strategy::Erb)
    Kubes::Compiler::Strategy::Erb
  else
    Kubes::Compiler::Dsl::Core::Base
  end
end

#load_custom_helpersObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kubes/compiler/shared/runtime_helpers.rb', line 13

def load_custom_helpers
  return if @@custom_helpers_loaded
  paths = Dir.glob("#{Kubes.root}/.kubes/helpers/**/*.rb")
  paths.sort_by! { |p| p.size } # so namespaces are loaded first

  paths.each do |path|
    filename = path.sub(%r{.*.kubes/helpers/},'').sub('.rb','')
    module_name = filename.camelize
    base_class_for_helper.send :include, module_name.constantize
  end
  @@custom_helpers_loaded = true
end

#load_custom_variablesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kubes/compiler/shared/runtime_helpers.rb', line 48

def load_custom_variables
  return if Kubes.kustomize?

  ext = File.extname(@path)
  role = @path.sub(%r{.*\.kubes/resources/},'').sub(ext,'').split('/').first # IE: web
  kind = File.basename(@path).sub(ext,'') # IE: deployment
  all = "all"
  if @block_form
    kind = kind.pluralize
    all = all.pluralize
  end

  layers = [
    "base.rb",
    "#{Kubes.env}.rb",
    "#{Kubes.env}-#{Kubes.extra}.rb",
    "base/all.rb",
    "base/all/#{Kubes.env}.rb",
    "base/all/#{Kubes.env}-#{Kubes.extra}.rb",
    "base/#{kind}.rb",
    "base/#{kind}/base.rb",
    "base/#{kind}/#{Kubes.env}.rb",
    "base/#{kind}/#{Kubes.env}-#{Kubes.extra}.rb",
    "#{role}/#{kind}.rb",
    "#{role}/#{kind}/base.rb",
    "#{role}/#{kind}/#{Kubes.env}.rb",
    "#{role}/#{kind}/#{Kubes.env}-#{Kubes.extra}.rb",
  ]
  # filter out the -.rb layers when Kubes.extra is not set
  # IE: .kubes/variables/dev-.rb
  layers.reject! { |layer| layer.ends_with?('-.rb') }

  if Kubes.app
    app_layers = ["#{Kubes.app}.rb"]
    app_layers += layers.map do |path|
      "#{Kubes.app}/#{path}"
    end
    layers += app_layers
  end

  layer_paths = layers.map { |layer| "#{Kubes.root}/.kubes/variables/#{layer}" }
  layer_paths = layer_paths.select { |path| File.exist?(path) } unless ENV['KUBES_LAYERING_SHOW_ALL']
  layer_paths.each do |path|
    show_variable_layer_path(path)
    evaluate_file(path)
  end
end

#load_plugin_helpersObject



28
29
30
31
32
33
34
35
# File 'lib/kubes/compiler/shared/runtime_helpers.rb', line 28

def load_plugin_helpers
  return if @@plugin_helpers_loaded
  Kubes::Plugin.plugins.each do |klass|
    helpers_class = "#{klass}::Helpers".constantize # IE: KubesAws::Helpers
    base_class_for_helper.send :include, helpers_class
  end
  @@plugin_helpers_loaded = true
end

#load_runtime_helpersObject



6
7
8
9
10
# File 'lib/kubes/compiler/shared/runtime_helpers.rb', line 6

def load_runtime_helpers
  load_plugin_helpers
  load_custom_helpers
  load_custom_variables # also load custom variables
end

#show_variable_layer_path(path) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/kubes/compiler/shared/runtime_helpers.rb', line 98

def show_variable_layer_path(path)
  main_path = @path # main path is main resource file

  show_header = !@@header_shown.dig(main_path)
  if show_header && Kubes.config.layering.show
    logger.info "    Variables layers:"
    @@header_shown[main_path] = true
  end
  # Examples:
  #   .kubes/resources/web/deployment.yaml
  #   .kubes/resources/shared/namespace.yaml
  show_once = !@@shown.dig(main_path, path)
  show_layer = Kubes.config.layering.show && show_once
  if show_layer
    logger.info "    #{pretty_path(path)}"
    @@shown[main_path] ||= {}
    @@shown[main_path][path] = true
  end
end