class KubernetesHelper::Core
Attributes
config_values[RW]
@return [Hash]
Public Class Methods
new(_env_name)
click to toggle source
@param _env_name (String)
# File lib/kubernetes_helper/core.rb, line 24 def initialize(_env_name) @config_values = KubernetesHelper.load_settings end
Public Instance Methods
parse_yml_file(file_path, output_path)
click to toggle source
# File lib/kubernetes_helper/core.rb, line 28 def parse_yml_file(file_path, output_path) parsed_content = replace_config_variables(File.read(file_path)) File.open(output_path, 'w+') { |f| f << parsed_content } # save as draft to be reviewed if failed old_yaml = YAML.load_stream(parsed_content) json_data = old_yaml.to_json # fix to skip anchors yml_data = JSON.parse(json_data) export_documents(yml_data, output_path) end
replace_config_variables(text)
click to toggle source
@param text (String) Sample: replicas: '#{deployment.replicas}'
# File lib/kubernetes_helper/core.rb, line 39 def replace_config_variables(text) values = config_values.map do |key, value| # rubocop:disable Style/HashTransformValues [key, value.is_a?(Hash) ? OpenStruct.new(value) : value] end.to_h values[:render_template] = method(:render_template) bind = ErbBinding.new(values).get_binding template = ERB.new(text) template.result(bind) end
run_command(command)
click to toggle source
# File lib/kubernetes_helper/core.rb, line 49 def run_command(command) command = replace_config_variables(command) KubernetesHelper.run_cmd(command) end
run_script(script_path)
click to toggle source
# File lib/kubernetes_helper/core.rb, line 54 def run_script(script_path) content = replace_config_variables(File.read(script_path)) tmp_file = KubernetesHelper.settings_path('tmp_script.sh') File.write(tmp_file, content) KubernetesHelper.run_cmd("chmod +x #{tmp_file}") KubernetesHelper.run_cmd(tmp_file) # File.delete(tmp_file) # keep tmp script for analysis purpose end
Private Instance Methods
export_documents(yml_data, file_path)
click to toggle source
# File lib/kubernetes_helper/core.rb, line 105 def export_documents(yml_data, file_path) File.open(file_path, 'w+') do |f| parse_documents(yml_data).each do |document| parse_import_secrets(document) f.write(document.to_yaml) end end end
import_secrets(path, secrets_name)
click to toggle source
Format: import_secrets
: [secrets_yml_path, secrets_name] Sample: import_secrets
: ['./secrets.yml', 'packing-beta-secrets']
# File lib/kubernetes_helper/core.rb, line 67 def import_secrets(path, secrets_name) path = KubernetesHelper.settings_path(path) data = YAML.load(File.read(path)) # rubocop:disable Security/YAMLLoad data['data'].keys.map do |secret| { 'name' => secret.upcase, 'valueFrom' => { 'secretKeyRef' => { 'name' => secrets_name, 'key' => secret } } } end end
parse_documents(yml_data)
click to toggle source
@return [Array<Hash>]
# File lib/kubernetes_helper/core.rb, line 115 def parse_documents(yml_data) documents = [] Array(yml_data).each do |document| document['documents'] ? documents.push(*document['documents']) : documents.push(document) end documents end
parse_import_secrets(document)
click to toggle source
parse secrets auto importer
# File lib/kubernetes_helper/core.rb, line 94 def parse_import_secrets(document) containers = document.dig('spec', 'template', 'spec', 'containers') || [] containers.each do |container| container['env'] = (container['env'] || []) + static_env_vars if container['import_secrets'] container['env'] = container['env'] + import_secrets(*container['import_secrets']) container.delete('import_secrets') end end end
render_template(template_name)
click to toggle source
# File lib/kubernetes_helper/core.rb, line 78 def render_template(template_name) path = KubernetesHelper.settings_path(template_name, use_template: true) text = "\n#{File.read(path)}" replace_config_variables(text) end
static_env_vars()
click to toggle source
# File lib/kubernetes_helper/core.rb, line 84 def static_env_vars (config_values.dig(:deployment, :env_vars) || {}).map do |key, value| { 'name' => key.to_s, 'value' => value } end end