Commit fc4d3a57 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Make rollout_status to be part of single API endpoint

parent 051daa03
class Projects::EnvironmentsController < Projects::ApplicationController class Projects::EnvironmentsController < Projects::ApplicationController
layout 'project' layout 'project'
before_action :authorize_read_environment! before_action :authorize_read_environment!
before_action :authorize_read_deploy_board!, only: :status
before_action :authorize_create_environment!, only: [:new, :create] before_action :authorize_create_environment!, only: [:new, :create]
before_action :authorize_create_deployment!, only: [:stop] before_action :authorize_create_deployment!, only: [:stop]
before_action :authorize_update_environment!, only: [:edit, :update] before_action :authorize_update_environment!, only: [:edit, :update]
before_action :authorize_admin_environment!, only: [:terminal, :terminal_websocket_authorize] before_action :authorize_admin_environment!, only: [:terminal, :terminal_websocket_authorize]
before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize, :metrics, :status] before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize, :metrics]
before_action :verify_api_request!, only: :terminal_websocket_authorize before_action :verify_api_request!, only: :terminal_websocket_authorize
def index def index
...@@ -132,25 +131,6 @@ class Projects::EnvironmentsController < Projects::ApplicationController ...@@ -132,25 +131,6 @@ class Projects::EnvironmentsController < Projects::ApplicationController
end end
end end
# The rollout status of an enviroment
def status
unless @environment.deployment_service_ready?
render text: 'Not found', status: 404
return
end
rollout_status = @environment.rollout_status
Gitlab::PollingInterval.set_header(response, interval: 3000) unless rollout_status.try(:complete?)
if rollout_status.nil?
render body: nil, status: 204 # no result yet
else
serializer = RolloutStatusSerializer.new(project: @project, current_user: @current_user)
render json: serializer.represent(rollout_status)
end
end
def additional_metrics def additional_metrics
respond_to do |format| respond_to do |format|
format.json do format.json do
......
...@@ -17,12 +17,14 @@ class MockDeploymentService < DeploymentService ...@@ -17,12 +17,14 @@ class MockDeploymentService < DeploymentService
end end
def rollout_status(environment) def rollout_status(environment)
OpenStruct.new( case environment.name
instances: rollout_status_instances, when 'staging'
completion: 80, Gitlab::Kubernetes::RolloutStatus.new([], status: :not_found)
valid?: true, when 'test'
complete?: true Gitlab::Kubernetes::RolloutStatus.new([], status: :loading)
) else
Gitlab::Kubernetes::RolloutStatus.new(rollout_status_deployments)
end
end end
private private
...@@ -31,4 +33,8 @@ class MockDeploymentService < DeploymentService ...@@ -31,4 +33,8 @@ class MockDeploymentService < DeploymentService
data = File.read(Rails.root.join('spec', 'fixtures', 'rollout_status_instances.json')) data = File.read(Rails.root.join('spec', 'fixtures', 'rollout_status_instances.json'))
JSON.parse(data) JSON.parse(data)
end end
def rollout_status_deployments
[ OpenStruct.new(instances: rollout_status_instances) ]
end
end end
...@@ -9,6 +9,10 @@ class EnvironmentEntity < Grape::Entity ...@@ -9,6 +9,10 @@ class EnvironmentEntity < Grape::Entity
expose :last_deployment, using: DeploymentEntity expose :last_deployment, using: DeploymentEntity
expose :stop_action? expose :stop_action?
expose :rollout_status,
if: -> (environment, _) { can?(request.current_user, :read_deploy_board, environment.project) },
using: RolloutStatusEntity
expose :metrics_path, if: -> (environment, _) { environment.has_metrics? } do |environment| expose :metrics_path, if: -> (environment, _) { environment.has_metrics? } do |environment|
metrics_project_environment_path(environment.project, environment) metrics_project_environment_path(environment.project, environment)
end end
...@@ -26,11 +30,6 @@ class EnvironmentEntity < Grape::Entity ...@@ -26,11 +30,6 @@ class EnvironmentEntity < Grape::Entity
terminal_project_environment_path(environment.project, environment) terminal_project_environment_path(environment.project, environment)
end end
expose :rollout_status_path, if: ->(environment, _) { environment.deployment_service_ready? } do |environment|
can?(request.current_user, :read_deploy_board, environment.project) &&
status_project_environment_path(environment.project, environment, format: :json)
end
expose :folder_path do |environment| expose :folder_path do |environment|
folder_project_environments_path(environment.project, environment.folder_name) folder_project_environments_path(environment.project, environment.folder_name)
end end
......
class RolloutStatusEntity < Grape::Entity class RolloutStatusEntity < Grape::Entity
include RequestAwareEntity include RequestAwareEntity
expose :instances expose :status, as: :status
expose :completion
expose :valid?, as: :valid
expose :is_completed do |rollout_status| expose :instances, if: -> (rollout_status, _) { rollout_status.found? }
expose :completion, if: -> (rollout_status, _) { rollout_status.found? }
expose :is_completed, if: -> (rollout_status, _) { rollout_status.found? } do |rollout_status|
rollout_status.complete? rollout_status.complete?
end end
end end
...@@ -224,7 +224,6 @@ constraints(ProjectUrlConstrainer.new) do ...@@ -224,7 +224,6 @@ constraints(ProjectUrlConstrainer.new) do
get :terminal get :terminal
get :metrics get :metrics
get :additional_metrics get :additional_metrics
get :status, constraints: { format: :json }
get '/terminal.ws/authorize', to: 'environments#terminal_websocket_authorize', constraints: { format: nil } get '/terminal.ws/authorize', to: 'environments#terminal_websocket_authorize', constraints: { format: nil }
end end
......
module EE module EE
module KubernetesService module KubernetesService
def rollout_status(environment) def rollout_status(environment)
with_reactive_cache do |data| result = with_reactive_cache do |data|
specs = filter_by_label(data[:deployments], app: environment.slug) specs = filter_by_label(data[:deployments], app: environment.slug)
::Gitlab::Kubernetes::RolloutStatus.from_specs(*specs) ::Gitlab::Kubernetes::RolloutStatus.from_specs(*specs)
end end
result || ::Gitlab::Kubernetes::RolloutStatus.loading_status
end end
def calculate_reactive_cache def calculate_reactive_cache
......
...@@ -6,26 +6,42 @@ module Gitlab ...@@ -6,26 +6,42 @@ module Gitlab
# other resources, unified by an `app=` label. The rollout status sums the # other resources, unified by an `app=` label. The rollout status sums the
# Kubernetes deployments together. # Kubernetes deployments together.
class RolloutStatus class RolloutStatus
attr_reader :deployments, :instances, :completion attr_reader :deployments, :instances, :completion, :status
def complete? def complete?
completion == 100 completion == 100
end end
def valid? def status
@valid @status
end
def loading?
@status == :loading
end
def not_found?
@status == :not_found
end
def found?
@status == :found
end end
def self.from_specs(*specs) def self.from_specs(*specs)
return new([], valid: false) if specs.empty? return new([], status: :not_found) if specs.empty?
deployments = specs.map { |spec| ::Gitlab::Kubernetes::Deployment.new(spec) } deployments = specs.map { |spec| ::Gitlab::Kubernetes::Deployment.new(spec) }
deployments.sort_by!(&:order) deployments.sort_by!(&:order)
new(deployments) new(deployments)
end end
def initialize(deployments, valid: true) def self.loading_rollout
@valid = valid new([], status: :loading)
end
def initialize(deployments, status: :found)
@status = status
@deployments = deployments @deployments = deployments
@instances = deployments.flat_map(&:instances) @instances = deployments.flat_map(&:instances)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment