summaryrefslogtreecommitdiff
path: root/lib/gitlab/kubernetes/helm/upgrade_command.rb
blob: 7418804673947c30693244830d984f226ae1dc56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

module Gitlab
  module Kubernetes
    module Helm
      class UpgradeCommand
        include BaseCommand

        attr_reader :name, :chart, :version, :repository, :files

        def initialize(name, chart:, files:, rbac:, version: nil, repository: nil)
          @name = name
          @chart = chart
          @rbac = rbac
          @version = version
          @files = files
          @repository = repository
        end

        def generate_script
          super + [
            init_command,
            repository_command,
            script_command
          ].compact.join("\n")
        end

        def rbac?
          @rbac
        end

        def pod_name
          "upgrade-#{name}"
        end

        private

        def init_command
          'helm init --client-only >/dev/null'
        end

        def repository_command
          "helm repo add #{name} #{repository}" if repository
        end

        def script_command
          upgrade_flags = "#{optional_version_flag}#{optional_tls_flags}" \
            " --reset-values" \
            " --install" \
            " --namespace #{::Gitlab::Kubernetes::Helm::NAMESPACE}" \
            " -f /data/helm/#{name}/config/values.yaml"

          "helm upgrade #{name} #{chart}#{upgrade_flags} >/dev/null\n"
        end

        def optional_version_flag
          " --version #{version}" if version
        end

        def optional_tls_flags
          return unless files.key?(:'ca.pem')

          " --tls" \
            " --tls-ca-cert #{files_dir}/ca.pem" \
            " --tls-cert #{files_dir}/cert.pem" \
            " --tls-key #{files_dir}/key.pem"
        end
      end
    end
  end
end