summaryrefslogtreecommitdiff
path: root/lib/quality/helm_client.rb
blob: 49d953da681a28b2e6a8583847584aae64839da4 (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
# frozen_string_literal: true

require 'time'
require_relative '../gitlab/popen' unless defined?(Gitlab::Popen)

module Quality
  class HelmClient
    attr_reader :namespace

    Release = Struct.new(:name, :revision, :last_update, :status, :chart, :namespace) do
      def revision
        @revision ||= self[:revision].to_i
      end

      def last_update
        @last_update ||= Time.parse(self[:last_update])
      end
    end

    def initialize(namespace: ENV['KUBE_NAMESPACE'])
      @namespace = namespace
    end

    def releases(args: [])
      command = ['list', %(--namespace "#{namespace}"), *args]

      run_command(command)
        .stdout
        .lines
        .select { |line| line.include?(namespace) }
        .map { |line| Release.new(*line.split(/\t/).map(&:strip)) }
    end

    def delete(release_name:)
      run_command(['delete', '--purge', release_name])
    end

    private

    def run_command(command)
      final_command = ['helm', *command].join(' ')
      puts "Running command: `#{final_command}`" # rubocop:disable Rails/Output

      Gitlab::Popen.popen_with_detail([final_command])
    end
  end
end