summaryrefslogtreecommitdiff
path: root/lib/gitlab/kubernetes/helm/install_command.rb
blob: e33ba9305ce19ff4e498b4058a69e41435ba3d0d (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# frozen_string_literal: true

module Gitlab
  module Kubernetes
    module Helm
      class InstallCommand
        include BaseCommand
        include ClientCommand

        attr_reader :name, :files, :chart, :repository, :preinstall, :postinstall
        attr_accessor :version

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

        def generate_script
          super + [
            init_command,
            wait_for_tiller_command,
            repository_command,
            repository_update_command,
            preinstall_command,
            install_command,
            postinstall_command
          ].compact.join("\n")
        end

        def rbac?
          @rbac
        end

        private

        def repository_update_command
          'helm repo update' if repository
        end

        # Uses `helm upgrade --install` which means we can use this for both
        # installation and uprade of applications
        def install_command
          command = ['helm', 'upgrade', name, chart] +
            install_flag +
            reset_values_flag +
            optional_tls_flags +
            optional_version_flag +
            rbac_create_flag +
            namespace_flag +
            value_flag

          command.shelljoin
        end

        def preinstall_command
          preinstall.join("\n") if preinstall
        end

        def postinstall_command
          postinstall.join("\n") if postinstall
        end

        def install_flag
          ['--install']
        end

        def reset_values_flag
          ['--reset-values']
        end

        def value_flag
          ['-f', "/data/helm/#{name}/config/values.yaml"]
        end

        def namespace_flag
          ['--namespace', Gitlab::Kubernetes::Helm::NAMESPACE]
        end

        def rbac_create_flag
          if rbac?
            %w[--set rbac.create=true,rbac.enabled=true]
          else
            %w[--set rbac.create=false,rbac.enabled=false]
          end
        end

        def optional_version_flag
          return [] unless version

          ['--version', 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