class Dapp::Kube::Helm::Release

Attributes

chart_path[R]
dapp[R]
deploy_timeout[R]
docker_tag[R]
kube_context[R]
name[R]
namespace[R]
repo[R]
set[R]
values[R]
without_registry[R]

Public Class Methods

new(dapp, name:, repo:, docker_tag:, namespace:, chart_path:, set: [], values: [], deploy_timeout: nil, without_registry: nil, kube_context: nil) click to toggle source
# File lib/dapp/kube/helm/release.rb, line 19
def initialize(dapp,
  name:, repo:, docker_tag:, namespace:, chart_path:,
  set: [], values: [], deploy_timeout: nil, without_registry: nil, kube_context: nil)
  @dapp = dapp

  @name = name
  @repo = repo
  @docker_tag = docker_tag
  @namespace = namespace
  @chart_path = chart_path
  @set = set
  @values = values
  @deploy_timeout = deploy_timeout
  @without_registry = (without_registry.nil? ? false : without_registry)
  @kube_context = kube_context
end

Public Instance Methods

deployments() click to toggle source
# File lib/dapp/kube/helm/release.rb, line 48
def deployments
  (resources_specs['Deployment'] || {}).map do |name, spec|
    [name, Kubernetes::Client::Resource::Deployment.new(spec)]
  end.to_h
end
hooks() click to toggle source
# File lib/dapp/kube/helm/release.rb, line 42
def hooks
  jobs.select do |_, spec|
    spec.annotations.key? "helm.sh/hook"
  end
end
install_helm_release() click to toggle source
# File lib/dapp/kube/helm/release.rb, line 54
def install_helm_release
  unless dapp.dry_run?
    dapp.kubernetes.create_namespace!(namespace) unless dapp.kubernetes.namespace?(namespace)
  end

  cmd = dapp.shellout([
    "helm install #{chart_path}",
    "--name #{name}",
    *helm_additional_values_options,
    *helm_set_options,
    *helm_install_options,
    ("--kube-context #{kube_context}" if kube_context),
  ].join(" "))

  return cmd
end
jobs() click to toggle source
# File lib/dapp/kube/helm/release.rb, line 36
def jobs
  (resources_specs['Job'] || {}).map do |name, spec|
    [name, Kubernetes::Client::Resource::Job.new(spec)]
  end.to_h
end
lint!() click to toggle source
# File lib/dapp/kube/helm/release.rb, line 106
def lint!
  dapp.shellout! [
    'helm',
    'lint',
    '--strict',
    *helm_additional_values_options,
    *helm_set_options(fake: true),
    *helm_common_options,
    ("--kube-context #{kube_context}" if kube_context),
    chart_path
  ].compact.join(' ')
end
templates() click to toggle source
# File lib/dapp/kube/helm/release.rb, line 83
def templates
  @templates ||= {}.tap do |t|
    current_template = nil
    spec = 0
    evaluation_output.lines.each do |l|
      if (match = l[/# Source: (.*)/, 1])
        spec = 0
        t[current_template = match] ||= []
      end

      if l[/^---$/]
        spec += 1
      elsif current_template
        (t[current_template][spec] ||= []) << l
      end
    end

    t.each do |template, specs|
      t[template] = "---\n#{specs.reject(&:nil?).map(&:join).join("---\n").strip}"
    end
  end
end
upgrade_helm_release() click to toggle source
# File lib/dapp/kube/helm/release.rb, line 71
def upgrade_helm_release
  cmd = dapp.shellout([
    "helm upgrade #{name} #{chart_path}",
    *helm_additional_values_options,
    *helm_set_options,
    *helm_install_options,
    ("--kube-context #{kube_context}" if kube_context),
  ].join(" "))

  return cmd
end

Protected Instance Methods

dimg_registry() click to toggle source
# File lib/dapp/kube/helm/release.rb, line 155
def dimg_registry
  @dimg_registry ||= dapp.dimg_registry(repo)
end
evaluation_output() click to toggle source
# File lib/dapp/kube/helm/release.rb, line 121
def evaluation_output
  @evaluation_output ||= begin
    cmd = dapp.shellout! [
      "helm",
      "template",
      chart_path,
      helm_additional_values_options,
      helm_set_options(without_registry: true),
      ("--namespace #{namespace}" if namespace),
      "--name #{name}",
      ("--kube-context #{kube_context}" if kube_context),
    ].compact.join(" ")

    cmd.stdout
  end
end
helm_additional_values_options() click to toggle source
# File lib/dapp/kube/helm/release.rb, line 149
def helm_additional_values_options
  [].tap do |options|
    options.concat(values.map { |p| "--values #{p}" })
  end
end
helm_common_options(dry_run: nil) click to toggle source
# File lib/dapp/kube/helm/release.rb, line 179
def helm_common_options(dry_run: nil)
  dry_run = dapp.dry_run? if dry_run.nil?

  [].tap do |options|
    options << "--namespace #{namespace}" if namespace
    options << '--debug'                  if dry_run
  end
end
helm_install_options(dry_run: nil) click to toggle source
# File lib/dapp/kube/helm/release.rb, line 170
def helm_install_options(dry_run: nil)
  dry_run = dapp.dry_run? if dry_run.nil?

  helm_common_options(dry_run: dry_run).tap do |options|
    options << '--dry-run' if dry_run
    options << "--timeout #{deploy_timeout}" if deploy_timeout
  end
end
helm_set_options(without_registry: false, fake: false) click to toggle source
# File lib/dapp/kube/helm/release.rb, line 159
def helm_set_options(without_registry: false, fake: false)
  [].tap do |options|
    options.concat set.map {|opt| "--set '#{opt}'"}

    service_values = Helm::Values.service_values(dapp, repo, namespace, docker_tag,
                                                 without_registry: self.without_registry || without_registry,
                                                 fake: fake)
    options.concat service_values.to_set_options
  end
end
resources_specs() click to toggle source
# File lib/dapp/kube/helm/release.rb, line 138
def resources_specs
  @resources_specs ||= {}.tap do |specs|
    evaluation_output.split(/^---$/)
        .reject {|chunk| chunk.lines.all? {|line| line.strip.empty? or line.strip.start_with? "#"}}
        .map {|chunk| yaml_load(chunk)}.each do |spec|
      specs[spec['kind']] ||= {}
      specs[spec['kind']][(spec['metadata'] || {})['name']] = spec
    end
  end
end