install_command.rb 2.9 KB
Newer Older
1 2 3
module Gitlab
  module Kubernetes
    module Helm
4 5
      class InstallCommand
        include BaseCommand
6

7
        attr_reader :name, :files, :chart, :version, :repository, :preinstall, :postinstall
8

9
        def initialize(name:, chart:, files:, rbac:, version: nil, repository: nil, preinstall: nil, postinstall: nil)
10 11
          @name = name
          @chart = chart
12
          @version = version
13
          @rbac = rbac
14
          @files = files
15
          @repository = repository
16 17
          @preinstall = preinstall
          @postinstall = postinstall
18 19
        end

20 21 22
        def generate_script
          super + [
            init_command,
23 24 25 26
            # Sleep is necessary to give Tiller time to restart after upgrade.
            # Ideally we'd be able to use --wait but cannot because of
            # https://github.com/helm/helm/issues/4855
            sleep_command,
27
            repository_command,
28
            repository_update_command,
29 30 31
            preinstall_command,
            install_command,
            postinstall_command
32
          ].compact.join("\n")
33 34
        end

35 36 37 38
        def rbac?
          @rbac
        end

39 40 41
        private

        def init_command
42 43 44 45 46
          'helm init --upgrade --tiller-namespace gitlab-managed-apps'
        end

        def sleep_command
          'sleep 30'
47 48
        end

49
        def repository_command
50
          ['helm', 'repo', 'add', name, repository].shelljoin if repository
51 52
        end

53
        def repository_update_command
54
          'helm repo update' if repository
55 56
        end

C
Chris Baumbauer 已提交
57
        def install_command
58 59
          command = ['helm', 'install', chart] + install_command_flags

60
          command.shelljoin
61 62
        end

63
        def preinstall_command
C
Chris Baumbauer 已提交
64
          preinstall.join("\n") if preinstall
65 66 67
        end

        def postinstall_command
C
Chris Baumbauer 已提交
68
          postinstall.join("\n") if postinstall
C
Chris Baumbauer 已提交
69 70
        end

71 72 73 74
        def install_command_flags
          name_flag      = ['--name', name]
          namespace_flag = ['--namespace', Gitlab::Kubernetes::Helm::NAMESPACE]
          value_flag     = ['-f', "/data/helm/#{name}/config/values.yaml"]
75

76 77 78 79 80
          name_flag +
            optional_tls_flags +
            optional_version_flag +
            optional_rbac_create_flag +
            namespace_flag +
C
Chris Baumbauer 已提交
81
            value_flag
82 83 84 85 86 87 88 89
        end

        def optional_rbac_create_flag
          return [] unless rbac?

          # jupyterhub helm chart is using rbac.enabled
          #   https://github.com/jupyterhub/zero-to-jupyterhub-k8s/tree/master/jupyterhub
          %w[--set rbac.create=true,rbac.enabled=true]
90
        end
91 92

        def optional_version_flag
93 94 95
          return [] unless version

          ['--version', version]
96
        end
97 98

        def optional_tls_flags
99
          return [] unless files.key?(:'ca.pem')
100

101 102 103 104 105 106
          [
            '--tls',
            '--tls-ca-cert', "#{files_dir}/ca.pem",
            '--tls-cert', "#{files_dir}/cert.pem",
            '--tls-key', "#{files_dir}/key.pem"
          ]
107
        end
108 109 110 111
      end
    end
  end
end