install_command.rb 2.5 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5
module Gitlab
  module Kubernetes
    module Helm
6 7
      class InstallCommand
        include BaseCommand
8
        include ClientCommand
9

10
        attr_reader :name, :files, :chart, :version, :repository, :preinstall, :postinstall
11

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

23 24 25
        def generate_script
          super + [
            init_command,
26
            wait_for_tiller_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
        private

41
        def repository_update_command
42
          'helm repo update' if repository
43 44
        end

C
Chris Baumbauer 已提交
45
        def install_command
46 47
          command = ['helm', 'install', chart] + install_command_flags

48
          command.shelljoin
49 50
        end

51
        def preinstall_command
C
Chris Baumbauer 已提交
52
          preinstall.join("\n") if preinstall
53 54 55
        end

        def postinstall_command
C
Chris Baumbauer 已提交
56
          postinstall.join("\n") if postinstall
C
Chris Baumbauer 已提交
57 58
        end

59 60 61 62
        def install_command_flags
          name_flag      = ['--name', name]
          namespace_flag = ['--namespace', Gitlab::Kubernetes::Helm::NAMESPACE]
          value_flag     = ['-f', "/data/helm/#{name}/config/values.yaml"]
63

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

        def optional_version_flag
81 82 83
          return [] unless version

          ['--version', version]
84
        end
85 86

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

89 90 91 92 93 94
          [
            '--tls',
            '--tls-ca-cert', "#{files_dir}/ca.pem",
            '--tls-cert', "#{files_dir}/cert.pem",
            '--tls-key', "#{files_dir}/key.pem"
          ]
95
        end
96 97 98 99
      end
    end
  end
end