Commit fa22bd92 authored by Thong Kuah's avatar Thong Kuah Committed by Enrique Alcantara

Teach RolloutStatus about has_legacy_app_label

This will be used by the wrong end to display a warning about deploy
boards when the app label matching the environment slug is used.

has_legacy_app_label will be true whenever we detect a deployment using
the legacy label.

NB: If a deployment uses the app label but does not match the
environment slug, don't match it. This could happen eg when user deploys
a un-related helm chart to the same namespace. Adds a test case for this
scenario.
parent 3587a23b
......@@ -13,7 +13,9 @@ module EE
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) if data[:pods]&.any?
::Gitlab::Kubernetes::RolloutStatus.from_deployments(*deployments, pods: pods)
legacy_deployments = filter_by_label(data[:deployments], { app: environment.slug })
::Gitlab::Kubernetes::RolloutStatus.from_deployments(*deployments, pods: pods, legacy_deployments: legacy_deployments)
end
result || ::Gitlab::Kubernetes::RolloutStatus.loading
end
......
......@@ -4,6 +4,7 @@ class RolloutStatusEntity < Grape::Entity
include RequestAwareEntity
expose :status, as: :status
expose :has_legacy_app_label?, as: :has_legacy_app_label
expose :instances, if: -> (rollout_status, _) { rollout_status.found? }
expose :completion, if: -> (rollout_status, _) { rollout_status.found? }
......
---
title: Show warning for deploy boards if legacy app label is used
merge_request: 14103
author:
type: other
......@@ -30,26 +30,31 @@ module Gitlab
@status == :not_found
end
def has_legacy_app_label?
legacy_deployments.present?
end
def found?
@status == :found
end
def self.from_deployments(*deployments, pods: {})
return new([], status: :not_found) if deployments.empty?
def self.from_deployments(*deployments, pods: {}, legacy_deployments: [])
return new([], status: :not_found, legacy_deployments: legacy_deployments) if deployments.empty?
deployments = deployments.map { |deploy| ::Gitlab::Kubernetes::Deployment.new(deploy, pods: pods) }
deployments.sort_by!(&:order)
new(deployments)
new(deployments, legacy_deployments: legacy_deployments)
end
def self.loading
new([], status: :loading)
end
def initialize(deployments, status: :found)
def initialize(deployments, status: :found, legacy_deployments: [])
@status = status
@deployments = deployments
@instances = deployments.flat_map(&:instances)
@legacy_deployments = legacy_deployments
@completion =
if @instances.empty?
......@@ -60,6 +65,10 @@ module Gitlab
(finished / @instances.count.to_f * 100).to_i
end
end
private
attr_reader :legacy_deployments
end
end
end
......@@ -14,6 +14,9 @@
"is_completed": {
"type": "boolean"
},
"has_legacy_app_label": {
"type": "boolean"
},
"instances": {
"type": "array",
"items": {
......
......@@ -5,7 +5,7 @@ describe Gitlab::Kubernetes::RolloutStatus do
let(:track) { nil }
let(:specs) { specs_all_finished }
let(:specs_none) { [] }
let(:legacy_deployments) { [] }
let(:pods) do
create_pods(name: "one", count: 3, track: 'stable') + create_pods(name: "two", count: 3, track: "canary")
......@@ -25,7 +25,26 @@ describe Gitlab::Kubernetes::RolloutStatus do
]
end
subject(:rollout_status) { described_class.from_deployments(*specs, pods: pods) }
subject(:rollout_status) { described_class.from_deployments(*specs, pods: pods, legacy_deployments: legacy_deployments) }
describe '#has_legacy_app_label?' do
let(:specs) { [] }
let(:pods) { [] }
context 'no legacy deployments' do
it { is_expected.not_to be_has_legacy_app_label }
end
context 'with legacy deployment' do
let(:legacy_deployments) do
[
kube_deployment(name: 'legacy')
]
end
it { is_expected.to be_has_legacy_app_label }
end
end
describe '#deployments' do
it 'stores the deployments' do
......@@ -125,7 +144,7 @@ describe Gitlab::Kubernetes::RolloutStatus do
end
context 'when list of specs is empty' do
let(:specs) { specs_none }
let(:specs) { [] }
it { is_expected.to be_not_found }
end
......@@ -137,7 +156,7 @@ describe Gitlab::Kubernetes::RolloutStatus do
end
context 'when list of specs is empty' do
let(:specs) { specs_none }
let(:specs) { [] }
it { is_expected.not_to be_found }
end
......
......@@ -43,6 +43,10 @@ describe KubernetesService, models: true, use_clean_rails_memory_store_caching:
expect(rollout_status.deployments).to eq([])
end
it 'has the has_legacy_app_label flag' do
expect(rollout_status).to be_has_legacy_app_label
end
end
context 'new deployment based on annotations' do
......@@ -62,6 +66,40 @@ describe KubernetesService, models: true, use_clean_rails_memory_store_caching:
expect(rollout_status.deployments.map(&:name)).to contain_exactly('matched-deployment')
end
it 'does have the has_legacy_app_label flag' do
expect(rollout_status).to be_has_legacy_app_label
end
end
context 'deployment with app label not matching the environment' do
let(:other_deployment) do
kube_deployment(name: 'other-deployment').tap do |deployment|
deployment['metadata']['annotations'].delete('app.gitlab.com/env')
deployment['metadata']['annotations'].delete('app.gitlab.com/app')
deployment['metadata']['labels']['app'] = 'helm-app-label'
end
end
let(:other_pod) do
kube_pod(name: 'other-pod').tap do |pod|
pod['metadata']['annotations'].delete('app.gitlab.com/env')
pod['metadata']['annotations'].delete('app.gitlab.com/app')
pod['metadata']['labels']['app'] = environment.slug
end
end
before do
stub_reactive_cache(
service,
deployments: [other_deployment],
pods: [other_pod]
)
end
it 'does not have the has_legacy_app_label flag' do
expect(rollout_status).not_to be_has_legacy_app_label
end
end
end
......
......@@ -3,19 +3,23 @@ require 'spec_helper'
describe RolloutStatusEntity do
include KubernetesHelpers
let(:rollout_status) { kube_deployment_rollout_status }
let(:entity) do
described_class.new(rollout_status, request: double)
end
subject { entity.as_json }
context 'when kube deployment is valid' do
let(:rollout_status) { kube_deployment_rollout_status }
it "exposes status" do
is_expected.to include(:status)
end
it "exposes status" do
is_expected.to include(:status)
end
it 'exposes has_legacy_app_label' do
is_expected.to include(:has_legacy_app_label)
end
context 'when kube deployment is valid' do
it "exposes deployment data" do
is_expected.to include(:instances, :completion, :is_completed)
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