Class: Dapp::Kube::Helm::Release

Inherits:
Object
  • Object
show all
Includes:
Helper::YAML
Defined in:
lib/dapp/kube/helm/release.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::YAML

#yaml_load, #yaml_load_file

Constructor Details

#initialize(dapp, name:, repo:, docker_tag:, namespace:, chart_path:, set: [], values: [], deploy_timeout: nil, without_registry: nil, kube_context: nil) ⇒ Release

Returns a new instance of Release.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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

Instance Attribute Details

#chart_pathObject (readonly)

Returns the value of attribute chart_path.



12
13
14
# File 'lib/dapp/kube/helm/release.rb', line 12

def chart_path
  @chart_path
end

#dappObject (readonly)

Returns the value of attribute dapp.



6
7
8
# File 'lib/dapp/kube/helm/release.rb', line 6

def dapp
  @dapp
end

#deploy_timeoutObject (readonly)

Returns the value of attribute deploy_timeout.



15
16
17
# File 'lib/dapp/kube/helm/release.rb', line 15

def deploy_timeout
  @deploy_timeout
end

#docker_tagObject (readonly)

Returns the value of attribute docker_tag.



10
11
12
# File 'lib/dapp/kube/helm/release.rb', line 10

def docker_tag
  @docker_tag
end

#kube_contextObject (readonly)

Returns the value of attribute kube_context.



17
18
19
# File 'lib/dapp/kube/helm/release.rb', line 17

def kube_context
  @kube_context
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/dapp/kube/helm/release.rb', line 8

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



11
12
13
# File 'lib/dapp/kube/helm/release.rb', line 11

def namespace
  @namespace
end

#repoObject (readonly)

Returns the value of attribute repo.



9
10
11
# File 'lib/dapp/kube/helm/release.rb', line 9

def repo
  @repo
end

#setObject (readonly)

Returns the value of attribute set.



13
14
15
# File 'lib/dapp/kube/helm/release.rb', line 13

def set
  @set
end

#valuesObject (readonly)

Returns the value of attribute values.



14
15
16
# File 'lib/dapp/kube/helm/release.rb', line 14

def values
  @values
end

#without_registryObject (readonly)

Returns the value of attribute without_registry.



16
17
18
# File 'lib/dapp/kube/helm/release.rb', line 16

def without_registry
  @without_registry
end

Instance Method Details

#deploymentsObject



48
49
50
51
52
# 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

#hooksObject



42
43
44
45
46
# 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_releaseObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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

#jobsObject



36
37
38
39
40
# 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!Object



106
107
108
109
110
111
112
113
114
115
116
117
# 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

#templatesObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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_releaseObject



71
72
73
74
75
76
77
78
79
80
81
# 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