Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rollout_status.rb « kubernetes « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e275303e650543bea4ab198439325dc5b094e79d (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
# frozen_string_literal: true

module Gitlab
  module Kubernetes
    # Calculates the rollout status for a set of kubernetes deployments.
    #
    # A GitLab environment may be composed of several Kubernetes deployments and
    # other resources. The rollout status sums the Kubernetes deployments
    # together.
    class RolloutStatus
      attr_reader :deployments, :instances, :completion, :status, :canary_ingress

      def complete?
        completion == 100
      end

      def loading?
        @status == :loading
      end

      def not_found?
        @status == :not_found
      end

      def found?
        @status == :found
      end

      def canary_ingress_exists?
        canary_ingress.present?
      end

      def self.from_deployments(*deployments_attrs, pods_attrs: [], ingresses: [])
        return new([], status: :not_found) if deployments_attrs.empty?

        deployments = deployments_attrs.map do |attrs|
          ::Gitlab::Kubernetes::Deployment.new(attrs, pods: pods_attrs)
        end
        deployments.sort_by!(&:order)

        pods = pods_attrs.map do |attrs|
          ::Gitlab::Kubernetes::Pod.new(attrs)
        end

        ingresses = ingresses.map { |ingress| ::Gitlab::Kubernetes::Ingress.new(ingress) }

        new(deployments, pods: pods, ingresses: ingresses)
      end

      def self.loading
        new([], status: :loading)
      end

      def initialize(deployments, pods: [], ingresses: [], status: :found)
        @status       = status
        @deployments  = deployments
        @instances = RolloutInstances.new(deployments, pods).pod_instances
        @canary_ingress = ingresses.find(&:canary?)

        @completion =
          if @instances.empty?
            100
          else
            # We downcase the pod status in Gitlab::Kubernetes::Deployment#deployment_instance
            finished = @instances.count { |instance| instance[:status] == ::Gitlab::Kubernetes::Pod::RUNNING.downcase }

            (finished / @instances.count.to_f * 100).to_i
          end
      end
    end
  end
end