summaryrefslogtreecommitdiff
path: root/app/services/clusters/applications/check_upgrade_progress_service.rb
blob: 11c078d15b71875231b19795a6ad75c811509904 (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
# frozen_string_literal: true

module Clusters
  module Applications
    class CheckUpgradeProgressService < BaseHelmService
      def execute
        return unless app.updating?

        case phase
        when ::Gitlab::Kubernetes::Pod::SUCCEEDED
          on_success
        when ::Gitlab::Kubernetes::Pod::FAILED
          on_failed
        else
          check_timeout
        end
      rescue Kubeclient::HttpError => e
        log_error(e)
        app.make_update_errored!("Kubernetes error: #{e.error_code}") unless app.update_errored?
      end

      private

      def on_success
        app.make_updated!
      ensure
        remove_pod
      end

      def on_failed
        app.make_update_errored!("Update failed. Check pod logs for #{upgrade_command.pod_name} for more details.")
      end

      def check_timeout
        if timeouted?
          app.make_update_errored!("Update timed out. Check pod logs for #{upgrade_command.pod_name} for more details.")
        else
          ::ClusterWaitForAppUpdateWorker.perform_in(
            ::ClusterWaitForAppUpdateWorker::INTERVAL, app.name, app.id)
        end
      end

      def timeouted?
        Time.now.utc - app.updated_at.to_time.utc > ::ClusterWaitForAppUpdateWorker::TIMEOUT
      end

      def remove_pod
        helm_api.delete_pod!(upgrade_command.pod_name)
      rescue
        # no-op
      end

      def phase
        helm_api.status(upgrade_command.pod_name)
      end

      def errors
        helm_api.log(upgrade_command.pod_name)
      end
    end
  end
end