class KY::EnvGeneration

Constants

ConflictingProjectError
OverlappingKeysInConfigmapAndSecretsError

Attributes

config_hsh[R]
configuration[R]
secret_hsh[R]

Public Class Methods

env(output, input1, input2) click to toggle source
# File lib/ky/env_generation.rb, line 14
def self.env(output, input1, input2)
  output << generate_env(input1, input2).to_plain_yaml
rescue ConflictingProjectError => e
  $stderr << "Error processing yml files, please provide a config and a secrets file from the same kubernetes project/name"
  exit(1)
end
generate_env(input1, input2) click to toggle source
# File lib/ky/env_generation.rb, line 21
def self.generate_env(input1, input2)
  new(input1, input2).to_h
end
new(input1, input2, configuration = Configuration.new) click to toggle source
# File lib/ky/env_generation.rb, line 26
def initialize(input1, input2, configuration = Configuration.new)
  input_hashes = YAML.load(input1.read).with_indifferent_access, YAML.load(input2.read).with_indifferent_access
  @configuration = configuration
  @config_hsh = input_hashes.find {|h| h[kind] == config_map }
  @secret_hsh = input_hashes.find {|h| h[kind] == secret }
  secret_project = secret_hsh[metadata][name]
  config_project = config_hsh[metadata][name]
  input_hashes.each do |env_hsh|
    env_hsh[:metadata][:namespace] = configuration[:namespace]
    env_hsh[:metadata][:name] = immutable_project_name
  end

  raise ConflictingProjectError.new("Config and Secret metadata names do not agree") unless secret_project == config_project
  intersection = config_hsh[data].keys & secret_hsh[data].keys
  raise OverlappingKeysInConfigmapAndSecretsError.new(intersection) unless intersection.empty?
end

Public Instance Methods

immutable_project_name() click to toggle source
# File lib/ky/env_generation.rb, line 51
def immutable_project_name
  project + env_suffix
end
project() click to toggle source
# File lib/ky/env_generation.rb, line 47
def project
   @name ||= config_hsh[metadata][name]
end
to_h() click to toggle source
# File lib/ky/env_generation.rb, line 43
def to_h
  output_hash(config_hsh[data].map {|key, value| config_env(key, value) } + secret_hsh[data].map {|key, value| secret_env(key, value) } + force_config)
end

Private Instance Methods

config_env(kebab_version, value) click to toggle source
# File lib/ky/env_generation.rb, line 78
def config_env(kebab_version, value)
  inline_config? ? inline_env_map(config_map_key_ref, kebab_version, value) : env_map(config_map_key_ref, kebab_version)
end
env_map(type, kebab_version) click to toggle source
# File lib/ky/env_generation.rb, line 100
def env_map(type, kebab_version)
  puts "WARNING: #{kebab_version} format appears incorrect, format as #{kebab_version.dasherize.downcase}" unless kebab_version == kebab_version.dasherize.downcase
  {name => kebab_version.underscore.upcase, value_from => { type => {name => immutable_project_name, key => kebab_version }}}
end
env_suffix() click to toggle source
# File lib/ky/env_generation.rb, line 57
def env_suffix
  config_word = RandomUsername.adjective(random: seed(config_hsh))
  secret_word = RandomUsername.noun(random: seed(secret_hsh))
  "-#{config_word}-#{secret_word}"
end
force_config() click to toggle source
# File lib/ky/env_generation.rb, line 73
def force_config
  return [] unless configuration[:force_configmap_apply]
  [inline_env_map(config_map_key_ref, "force-configmap-apply", SecureRandom.hex)]
end
inline_config?() click to toggle source
# File lib/ky/env_generation.rb, line 86
def inline_config?
  configuration[:inline_config]
end
inline_env_map(type, kebab_version, env_value) click to toggle source
# File lib/ky/env_generation.rb, line 95
def inline_env_map(type, kebab_version, env_value)
  puts "WARNING: #{kebab_version} format appears incorrect, format as #{kebab_version.dasherize.downcase}" unless kebab_version == kebab_version.dasherize.downcase
  {name => kebab_version.underscore.upcase, value => env_value }
end
inline_secret?() click to toggle source
# File lib/ky/env_generation.rb, line 91
def inline_secret?
  configuration[:inline_secret]
end
output_hash(env_array) click to toggle source
# File lib/ky/env_generation.rb, line 105
def output_hash(env_array)
  {spec => {template => {spec => { containers => [{env => env_array }]}}}}
end
secret_env(kebab_version, value) click to toggle source
# File lib/ky/env_generation.rb, line 82
def secret_env(kebab_version, value)
  inline_secret? ? inline_env_map(secret_key_ref, kebab_version, value) : env_map(secret_key_ref, kebab_version)
end
seed(hsh) click to toggle source
# File lib/ky/env_generation.rb, line 63
def seed(hsh)
  Random.new("0x#{sha(hsh).hexdigest}".to_i(16))
end
sha(hsh) click to toggle source
# File lib/ky/env_generation.rb, line 67
def sha(hsh)
  Digest::SHA1.new.tap do |clean_sha|
    clean_sha.update hsh[data].to_json
  end
end