summaryrefslogtreecommitdiff
path: root/lib/gitlab/kubernetes/helm/v2/patch_command.rb
blob: 2855e6444b118acf819fd4855a18a796087e9779 (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
# frozen_string_literal: true

# PatchCommand is for updating values in installed charts without overwriting
# existing values.
module Gitlab
  module Kubernetes
    module Helm
      module V2
        class PatchCommand < BaseCommand
          include ClientCommand

          attr_reader :chart, :repository
          attr_accessor :version

          def initialize(chart:, version:, repository: nil, **args)
            super(**args)

            # version is mandatory to prevent chart mismatches
            # we do not want our values interpreted in the context of the wrong version
            raise ArgumentError, 'version is required' if version.blank?

            @chart = chart
            @version = version
            @repository = repository
          end

          def generate_script
            super + [
              init_command,
              repository_command,
              repository_update_command,
              upgrade_command
            ].compact.join("\n")
          end

          private

          def upgrade_command
            command = ['helm', 'upgrade', name, chart] +
                      reuse_values_flag +
                      version_flag +
                      namespace_flag +
                      value_flag

            command.shelljoin
          end

          def reuse_values_flag
            ['--reuse-values']
          end

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

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

          def version_flag
            ['--version', version]
          end
        end
      end
    end
  end
end