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 56 57 58 59 60 61 62 63
        def preinstall_command
          unless preinstall.nil?
            preinstall.join("\n")
          end
        end

        def postinstall_command
          unless postinstall.nil?
            postinstall.join("\n")
C
Chris Baumbauer 已提交
64 65 66
          end
        end

67 68 69 70
        def install_command_flags
          name_flag      = ['--name', name]
          namespace_flag = ['--namespace', Gitlab::Kubernetes::Helm::NAMESPACE]
          value_flag     = ['-f', "/data/helm/#{name}/config/values.yaml"]
71

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

        def optional_version_flag
89 90 91
          return [] unless version

          ['--version', version]
92
        end
93 94

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

97 98 99 100 101 102
          [
            '--tls',
            '--tls-ca-cert', "#{files_dir}/ca.pem",
            '--tls-cert', "#{files_dir}/cert.pem",
            '--tls-key', "#{files_dir}/key.pem"
          ]
103
        end
104 105 106 107
      end
    end
  end
end