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

update_service.rb « canary_ingress « environments « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 474c3de23d9515e30111a5fed920037b59eba66d (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
# frozen_string_literal: true

module Environments
  module CanaryIngress
    class UpdateService < ::BaseService
      def execute_async(environment)
        result = validate(environment)

        return result unless result[:status] == :success

        Environments::CanaryIngress::UpdateWorker.perform_async(environment.id, params)

        success
      end

      # This method actually executes the PATCH request to Kubernetes,
      # that is used by internal processes i.e. sidekiq worker.
      # You should always use `execute_async` to properly validate user's requests.
      def execute(environment)
        canary_ingress = environment.ingresses&.find(&:canary?)

        unless canary_ingress.present?
          return error(_('Canary Ingress does not exist in the environment.'))
        end

        if environment.patch_ingress(canary_ingress, patch_data)
          success
        else
          error(_('Failed to update the Canary Ingress.'), :bad_request)
        end
      end

      private

      def validate(environment)
        unless Feature.enabled?(:canary_ingress_weight_control, environment.project, default_enabled: true)
          return error(_("Feature flag is not enabled on the environment's project."))
        end

        unless can?(current_user, :update_environment, environment)
          return error(_('You do not have permission to update the environment.'))
        end

        unless params[:weight].is_a?(Integer) && (0..100).cover?(params[:weight])
          return error(_('Canary weight must be specified and valid range (0..100).'))
        end

        if environment.has_running_deployments?
          return error(_('There are running deployments on the environment. Please retry later.'))
        end

        if ::Gitlab::ApplicationRateLimiter.throttled?(:update_environment_canary_ingress, scope: [environment])
          return error(_("This environment's canary ingress has been updated recently. Please retry later."))
        end

        success
      end

      def patch_data
        {
          metadata: {
            annotations: {
              Gitlab::Kubernetes::Ingress::ANNOTATION_KEY_CANARY_WEIGHT => params[:weight].to_s
            }
          }
        }
      end
    end
  end
end