Commit 5bd48e0f authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '212320-move-deploy-board-model-logic' into 'master'

RUN AS-IF-FOSS Move deploy boards backend to core

See merge request gitlab-org/gitlab!47979
parents 59fc740e 8a4333ab
......@@ -94,9 +94,20 @@ module Clusters
return unless enabled?
pods = read_pods(environment.deployment_namespace)
deployments = read_deployments(environment.deployment_namespace)
# extract_relevant_pod_data avoids uploading all the pod info into ReactiveCaching
{ pods: extract_relevant_pod_data(pods) }
ingresses = if ::Feature.enabled?(:canary_ingress_weight_control, environment.project, default_enabled: true)
read_ingresses(environment.deployment_namespace)
else
[]
end
# extract only the data required for display to avoid unnecessary caching
{
pods: extract_relevant_pod_data(pods),
deployments: extract_relevant_deployment_data(deployments),
ingresses: extract_relevant_ingress_data(ingresses)
}
end
def terminals(environment, data)
......@@ -109,6 +120,25 @@ module Clusters
@kubeclient ||= build_kube_client!
end
def rollout_status(environment, data)
project = environment.project
deployments = filter_by_project_environment(data[:deployments], project.full_path_slug, environment.slug)
pods = filter_by_project_environment(data[:pods], project.full_path_slug, environment.slug)
ingresses = data[:ingresses].presence || []
::Gitlab::Kubernetes::RolloutStatus.from_deployments(*deployments, pods_attrs: pods, ingresses: ingresses)
end
def ingresses(namespace)
ingresses = read_ingresses(namespace)
ingresses.map { |ingress| ::Gitlab::Kubernetes::Ingress.new(ingress) }
end
def patch_ingress(namespace, ingress, data)
kubeclient.patch_ingress(ingress.name, data, namespace)
end
private
def default_namespace(project, environment_name:)
......@@ -140,6 +170,18 @@ module Clusters
[]
end
def read_deployments(namespace)
kubeclient.get_deployments(namespace: namespace).as_json
rescue Kubeclient::ResourceNotFoundError
[]
end
def read_ingresses(namespace)
kubeclient.get_ingresses(namespace: namespace).as_json
rescue Kubeclient::ResourceNotFoundError
[]
end
def build_kube_client!
raise "Incomplete settings" unless api_url
......@@ -231,8 +273,24 @@ module Clusters
}
end
end
def extract_relevant_deployment_data(deployments)
deployments.map do |deployment|
{
'metadata' => deployment.fetch('metadata', {}).slice('name', 'generation', 'labels', 'annotations'),
'spec' => deployment.fetch('spec', {}).slice('replicas'),
'status' => deployment.fetch('status', {}).slice('observedGeneration')
}
end
end
def extract_relevant_ingress_data(ingresses)
ingresses.map do |ingress|
{
'metadata' => ingress.fetch('metadata', {}).slice('name', 'labels', 'annotations')
}
end
end
end
end
end
Clusters::Platforms::Kubernetes.prepend_if_ee('EE::Clusters::Platforms::Kubernetes')
......@@ -384,8 +384,38 @@ class Environment < ApplicationRecord
!!deployment_platform&.cluster&.application_elastic_stack_available?
end
def rollout_status
return unless rollout_status_available?
result = rollout_status_with_reactive_cache
result || ::Gitlab::Kubernetes::RolloutStatus.loading
end
def ingresses
return unless rollout_status_available?
deployment_platform.ingresses(deployment_namespace)
end
def patch_ingress(ingress, data)
return unless rollout_status_available?
deployment_platform.patch_ingress(deployment_namespace, ingress, data)
end
private
def rollout_status_available?
has_terminals?
end
def rollout_status_with_reactive_cache
with_reactive_cache do |data|
deployment_platform.rollout_status(self, data)
end
end
def has_metrics_and_can_query?
has_metrics? && prometheus_adapter.can_query?
end
......
......@@ -31,6 +31,26 @@ class MockDeploymentService < Service
def can_test?
false
end
end
MockDeploymentService.prepend_if_ee('EE::MockDeploymentService')
def rollout_status(environment)
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
def rollout_status_instances
data = File.read(Rails.root.join('spec', 'fixtures', 'rollout_status_instances.json'))
Gitlab::Json.parse(data)
end
def rollout_status_deployments
[OpenStruct.new(instances: rollout_status_instances)]
end
end
# frozen_string_literal: true
module EE
module Clusters
module Platforms
module Kubernetes
extend ActiveSupport::Concern
extend ::Gitlab::Utils::Override
override :calculate_reactive_cache_for
def calculate_reactive_cache_for(environment)
result = super
if result
deployments = read_deployments(environment.deployment_namespace)
ingresses = if ::Feature.enabled?(:canary_ingress_weight_control, environment.project, default_enabled: true)
read_ingresses(environment.deployment_namespace)
else
[]
end
# extract_relevant_deployment_data avoids uploading all the deployment info into ReactiveCaching
result[:deployments] = extract_relevant_deployment_data(deployments)
result[:ingresses] = extract_relevant_ingress_data(ingresses)
end
result
end
def rollout_status(environment, data)
project = environment.project
deployments = filter_by_project_environment(data[:deployments], project.full_path_slug, environment.slug)
pods = filter_by_project_environment(data[:pods], project.full_path_slug, environment.slug)
ingresses = data[:ingresses].presence || []
::Gitlab::Kubernetes::RolloutStatus.from_deployments(*deployments, pods_attrs: pods, ingresses: ingresses)
end
def ingresses(namespace)
ingresses = read_ingresses(namespace)
ingresses.map { |ingress| ::Gitlab::Kubernetes::Ingress.new(ingress) }
end
def patch_ingress(namespace, ingress, data)
kubeclient.patch_ingress(ingress.name, data, namespace)
end
private
def read_deployments(namespace)
kubeclient.get_deployments(namespace: namespace).as_json
rescue Kubeclient::ResourceNotFoundError
[]
end
def read_ingresses(namespace)
kubeclient.get_ingresses(namespace: namespace).as_json
rescue Kubeclient::ResourceNotFoundError
[]
end
def extract_relevant_deployment_data(deployments)
deployments.map do |deployment|
{
'metadata' => deployment.fetch('metadata', {}).slice('name', 'generation', 'labels', 'annotations'),
'spec' => deployment.fetch('spec', {}).slice('replicas'),
'status' => deployment.fetch('status', {}).slice('observedGeneration')
}
end
end
def extract_relevant_ingress_data(ingresses)
ingresses.map do |ingress|
{
'metadata' => ingress.fetch('metadata', {}).slice('name', 'labels', 'annotations')
}
end
end
end
end
end
end
# frozen_string_literal: true
module EE
module DeploymentService
# Environments have a rollout status. This represents the current state of
# deployments to that environment.
def rollout_status(environment)
raise NotImplementedError
end
end
end
......@@ -66,37 +66,5 @@ module EE
def protected_deployable_by_user?(user)
project.protected_environment_accessible_to?(name, user)
end
def rollout_status
return unless rollout_status_available?
result = rollout_status_with_reactive_cache
result || ::Gitlab::Kubernetes::RolloutStatus.loading
end
def ingresses
return unless rollout_status_available?
deployment_platform.ingresses(deployment_namespace)
end
def patch_ingress(ingress, data)
return unless rollout_status_available?
deployment_platform.patch_ingress(deployment_namespace, ingress, data)
end
private
def rollout_status_available?
has_terminals?
end
def rollout_status_with_reactive_cache
with_reactive_cache do |data|
deployment_platform.rollout_status(self, data)
end
end
end
end
# frozen_string_literal: true
module EE
module MockDeploymentService
def rollout_status(environment)
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
def rollout_status_instances
data = File.read(Rails.root.join('spec', 'fixtures', 'rollout_status_instances.json'))
Gitlab::Json.parse(data)
end
def rollout_status_deployments
[OpenStruct.new(instances: rollout_status_instances)]
end
end
end
......@@ -184,123 +184,4 @@ RSpec.describe Environment, :use_clean_rails_memory_store_caching do
end
end
end
describe '#rollout_status' do
let!(:cluster) { create(:cluster, :project, :provided_by_user, projects: [project]) }
let!(:environment) { create(:environment, project: project) }
let!(:deployment) { create(:deployment, :success, environment: environment, project: project) }
subject { environment.rollout_status }
context 'environment does not have a deployment board available' do
before do
allow(environment).to receive(:has_terminals?).and_return(false)
end
it { is_expected.to be_nil }
end
context 'cached rollout status is present' do
let(:pods) { %w(pod1 pod2) }
let(:deployments) { %w(deployment1 deployment2) }
before do
stub_reactive_cache(environment, pods: pods, deployments: deployments)
end
it 'fetches the rollout status from the deployment platform' do
expect(environment.deployment_platform).to receive(:rollout_status)
.with(environment, pods: pods, deployments: deployments)
.and_return(:mock_rollout_status)
is_expected.to eq(:mock_rollout_status)
end
end
context 'cached rollout status is not present yet' do
before do
stub_reactive_cache(environment, nil)
end
it 'falls back to a loading status' do
expect(::Gitlab::Kubernetes::RolloutStatus).to receive(:loading).and_return(:mock_loading_status)
is_expected.to eq(:mock_loading_status)
end
end
end
describe '#ingresses' do
subject { environment.ingresses }
let(:deployment_platform) { double(:deployment_platform) }
let(:deployment_namespace) { 'production' }
before do
allow(environment).to receive(:deployment_platform) { deployment_platform }
allow(environment).to receive(:deployment_namespace) { deployment_namespace }
end
context 'when rollout status is available' do
before do
allow(environment).to receive(:rollout_status_available?) { true }
end
it 'fetches ingresses from the deployment platform' do
expect(deployment_platform).to receive(:ingresses).with(deployment_namespace)
subject
end
end
context 'when rollout status is not available' do
before do
allow(environment).to receive(:rollout_status_available?) { false }
end
it 'does nothing' do
expect(deployment_platform).not_to receive(:ingresses)
subject
end
end
end
describe '#patch_ingress' do
subject { environment.patch_ingress(ingress, data) }
let(:ingress) { double(:ingress) }
let(:data) { double(:data) }
let(:deployment_platform) { double(:deployment_platform) }
let(:deployment_namespace) { 'production' }
before do
allow(environment).to receive(:deployment_platform) { deployment_platform }
allow(environment).to receive(:deployment_namespace) { deployment_namespace }
end
context 'when rollout status is available' do
before do
allow(environment).to receive(:rollout_status_available?) { true }
end
it 'fetches ingresses from the deployment platform' do
expect(deployment_platform).to receive(:patch_ingress).with(deployment_namespace, ingress, data)
subject
end
end
context 'when rollout status is not available' do
before do
allow(environment).to receive(:rollout_status_available?) { false }
end
it 'does nothing' do
expect(deployment_platform).not_to receive(:patch_ingress)
subject
end
end
end
end
......@@ -1421,4 +1421,123 @@ RSpec.describe Environment, :use_clean_rails_memory_store_caching do
end
end
end
describe '#rollout_status' do
let!(:cluster) { create(:cluster, :project, :provided_by_user, projects: [project]) }
let!(:environment) { create(:environment, project: project) }
let!(:deployment) { create(:deployment, :success, environment: environment, project: project) }
subject { environment.rollout_status }
context 'environment does not have a deployment board available' do
before do
allow(environment).to receive(:has_terminals?).and_return(false)
end
it { is_expected.to be_nil }
end
context 'cached rollout status is present' do
let(:pods) { %w(pod1 pod2) }
let(:deployments) { %w(deployment1 deployment2) }
before do
stub_reactive_cache(environment, pods: pods, deployments: deployments)
end
it 'fetches the rollout status from the deployment platform' do
expect(environment.deployment_platform).to receive(:rollout_status)
.with(environment, pods: pods, deployments: deployments)
.and_return(:mock_rollout_status)
is_expected.to eq(:mock_rollout_status)
end
end
context 'cached rollout status is not present yet' do
before do
stub_reactive_cache(environment, nil)
end
it 'falls back to a loading status' do
expect(::Gitlab::Kubernetes::RolloutStatus).to receive(:loading).and_return(:mock_loading_status)
is_expected.to eq(:mock_loading_status)
end
end
end
describe '#ingresses' do
subject { environment.ingresses }
let(:deployment_platform) { double(:deployment_platform) }
let(:deployment_namespace) { 'production' }
before do
allow(environment).to receive(:deployment_platform) { deployment_platform }
allow(environment).to receive(:deployment_namespace) { deployment_namespace }
end
context 'when rollout status is available' do
before do
allow(environment).to receive(:rollout_status_available?) { true }
end
it 'fetches ingresses from the deployment platform' do
expect(deployment_platform).to receive(:ingresses).with(deployment_namespace)
subject
end
end
context 'when rollout status is not available' do
before do
allow(environment).to receive(:rollout_status_available?) { false }
end
it 'does nothing' do
expect(deployment_platform).not_to receive(:ingresses)
subject
end
end
end
describe '#patch_ingress' do
subject { environment.patch_ingress(ingress, data) }
let(:ingress) { double(:ingress) }
let(:data) { double(:data) }
let(:deployment_platform) { double(:deployment_platform) }
let(:deployment_namespace) { 'production' }
before do
allow(environment).to receive(:deployment_platform) { deployment_platform }
allow(environment).to receive(:deployment_namespace) { deployment_namespace }
end
context 'when rollout status is available' do
before do
allow(environment).to receive(:rollout_status_available?) { true }
end
it 'fetches ingresses from the deployment platform' do
expect(deployment_platform).to receive(:patch_ingress).with(deployment_namespace, ingress, data)
subject
end
end
context 'when rollout status is not available' do
before do
allow(environment).to receive(:rollout_status_available?) { false }
end
it 'does nothing' do
expect(deployment_platform).not_to receive(:patch_ingress)
subject
end
end
end
end
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