install_command.rb 2.7 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 23
        def generate_script
          super + [
            init_command,
            repository_command,
24
            repository_update_command,
25 26 27
            preinstall_command,
            install_command,
            postinstall_command
28
          ].compact.join("\n")
29 30
        end

31 32 33 34
        def rbac?
          @rbac
        end

35 36 37
        private

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

41
        def repository_command
42
          ['helm', 'repo', 'add', name, repository].shelljoin if repository
43 44
        end

45 46 47 48
        def repository_update_command
          'helm repo update >/dev/null' if repository
        end

C
Chris Baumbauer 已提交
49
        def install_command
50 51 52 53 54
          command = ['helm', 'install', chart] + install_command_flags

          command.shelljoin + " >/dev/null\n"
        end

55
        def preinstall_command
56
          preinstall.join("\n") unless preinstall.nil?
57 58 59
        end

        def postinstall_command
60
          postinstall.join("\n") unless postinstall.nil?
C
Chris Baumbauer 已提交
61 62
        end

63 64 65 66
        def install_command_flags
          name_flag      = ['--name', name]
          namespace_flag = ['--namespace', Gitlab::Kubernetes::Helm::NAMESPACE]
          value_flag     = ['-f', "/data/helm/#{name}/config/values.yaml"]
67

68 69 70 71 72
          name_flag +
            optional_tls_flags +
            optional_version_flag +
            optional_rbac_create_flag +
            namespace_flag +
C
Chris Baumbauer 已提交
73
            value_flag
74 75 76 77 78 79 80 81
        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]
82
        end
83 84

        def optional_version_flag
85 86 87
          return [] unless version

          ['--version', version]
88
        end
89 90

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

93 94 95 96 97 98
          [
            '--tls',
            '--tls-ca-cert', "#{files_dir}/ca.pem",
            '--tls-cert', "#{files_dir}/cert.pem",
            '--tls-key', "#{files_dir}/key.pem"
          ]
99
        end
100 101 102 103
      end
    end
  end
end