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
layout 'project'
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_deployment!, only: [:stop]
before_action :authorize_update_environment!, only: [:edit, :update]
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
def index
......@@ -132,25 +131,6 @@ class Projects::EnvironmentsController < Projects::ApplicationController
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
respond_to do |format|
format.json do
......
......@@ -17,12 +17,14 @@ class MockDeploymentService < DeploymentService
end
def rollout_status(environment)
OpenStruct.new(
instances: rollout_status_instances,
completion: 80,
valid?: true,
complete?: true
)
case environment.name
when 'staging'
Gitlab::Kubernetes::RolloutStatus.new([], status: :not_found)
when 'test'
Gitlab::Kubernetes::RolloutStatus.new([], status: :loading)
else
Gitlab::Kubernetes::RolloutStatus.new(rollout_status_deployments)
end
end
private
......@@ -31,4 +33,8 @@ class MockDeploymentService < DeploymentService
data = File.read(Rails.root.join('spec', 'fixtures', 'rollout_status_instances.json'))
JSON.parse(data)
end
def rollout_status_deployments
[ OpenStruct.new(instances: rollout_status_instances) ]
end
end
......@@ -9,6 +9,10 @@ class EnvironmentEntity < Grape::Entity
expose :last_deployment, using: DeploymentEntity
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|
metrics_project_environment_path(environment.project, environment)
end
......@@ -26,11 +30,6 @@ class EnvironmentEntity < Grape::Entity
terminal_project_environment_path(environment.project, environment)
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|
folder_project_environments_path(environment.project, environment.folder_name)
end
......
class RolloutStatusEntity < Grape::Entity
include RequestAwareEntity
expose :instances
expose :completion
expose :valid?, as: :valid
expose :status, as: :status
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?
end
end
......@@ -224,7 +224,6 @@ constraints(ProjectUrlConstrainer.new) do
get :terminal
get :metrics
get :additional_metrics
get :status, constraints: { format: :json }
get '/terminal.ws/authorize', to: 'environments#terminal_websocket_authorize', constraints: { format: nil }
end
......
module EE
module KubernetesService
def rollout_status(environment)
with_reactive_cache do |data|
result = with_reactive_cache do |data|
specs = filter_by_label(data[:deployments], app: environment.slug)
::Gitlab::Kubernetes::RolloutStatus.from_specs(*specs)
end
result || ::Gitlab::Kubernetes::RolloutStatus.loading_status
end
def calculate_reactive_cache
......
......@@ -6,26 +6,42 @@ module Gitlab
# other resources, unified by an `app=` label. The rollout status sums the
# Kubernetes deployments together.
class RolloutStatus
attr_reader :deployments, :instances, :completion
attr_reader :deployments, :instances, :completion, :status
def complete?
completion == 100
end
def valid?
@valid
def status
@status
end
def loading?
@status == :loading
end
def not_found?
@status == :not_found
end
def found?
@status == :found
end
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.sort_by!(&:order)
new(deployments)
end
def initialize(deployments, valid: true)
@valid = valid
def self.loading_rollout
new([], status: :loading)
end
def initialize(deployments, status: :found)
@status = status
@deployments = deployments
@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