summaryrefslogtreecommitdiff
path: root/app/services/clusters/parse_cluster_applications_artifact_service.rb
blob: b8e1c80cfe7163b5a69b0462232242e09c3c7920 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# frozen_string_literal: true

module Clusters
  class ParseClusterApplicationsArtifactService < ::BaseService
    include Gitlab::Utils::StrongMemoize

    MAX_ACCEPTABLE_ARTIFACT_SIZE = 5.kilobytes
    RELEASE_NAMES = %w[prometheus].freeze

    def initialize(job, current_user)
      @job = job

      super(job.project, current_user)
    end

    def execute(artifact)
      return success unless Feature.enabled?(:cluster_applications_artifact, project)

      raise ArgumentError, 'Artifact is not cluster_applications file type' unless artifact&.cluster_applications?

      unless artifact.file.size < MAX_ACCEPTABLE_ARTIFACT_SIZE
        return error(too_big_error_message, :bad_request)
      end

      unless cluster
        return error(s_('ClusterIntegration|No deployment cluster found for this job'))
      end

      parse!(artifact)

      success
    rescue Gitlab::Kubernetes::Helm::Parsers::ListV2::ParserError, ActiveRecord::RecordInvalid => error
      Gitlab::ErrorTracking.track_exception(error, job_id: artifact.job_id)
      error(error.message, :bad_request)
    end

    private

    attr_reader :job

    def cluster
      strong_memoize(:cluster) do
        deployment_cluster = job.deployment&.cluster

        deployment_cluster if Ability.allowed?(current_user, :admin_cluster, deployment_cluster)
      end
    end

    def parse!(artifact)
      releases = []

      artifact.each_blob do |blob|
        releases.concat(Gitlab::Kubernetes::Helm::Parsers::ListV2.new(blob).releases)
      end

      update_cluster_application_statuses!(releases)
    end

    def update_cluster_application_statuses!(releases)
      release_by_name = releases.index_by { |release| release['Name'] }

      Clusters::Cluster.transaction do
        RELEASE_NAMES.each do |release_name|
          application = find_or_build_application(release_name)

          release = release_by_name[release_name]

          if release
            case release['Status']
            when 'DEPLOYED'
              application.make_externally_installed!
            when 'FAILED'
              application.make_errored!(s_('ClusterIntegration|Helm release failed to install'))
            end
          else
            # missing, so by definition, we consider this uninstalled
            application.make_externally_uninstalled! if application.persisted?
          end
        end
      end
    end

    def find_or_build_application(application_name)
      application_class = Clusters::Cluster::APPLICATIONS[application_name]

      cluster.find_or_build_application(application_class)
    end

    def too_big_error_message
      human_size = ActiveSupport::NumberHelper.number_to_human_size(MAX_ACCEPTABLE_ARTIFACT_SIZE)

      s_('ClusterIntegration|Cluster_applications artifact too big. Maximum allowable size: %{human_size}') % { human_size: human_size }
    end
  end
end