Commit 38ef9219 authored by Tiger's avatar Tiger

EE - Remove legacy Kubernetes #actual_namespace

https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/12913
parent 98793c2e
......@@ -8,6 +8,8 @@ module EE
def rollout_status(environment)
result = with_reactive_cache do |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) if data[:pods]&.any?
......@@ -26,14 +28,18 @@ module EE
def reactive_cache_updated
super
::Gitlab::EtagCaching::Store.new.tap do |store|
store.touch(
::Gitlab::Routing.url_helpers.project_environments_path(project, format: :json))
if first_project
::Gitlab::EtagCaching::Store.new.tap do |store|
store.touch(
::Gitlab::Routing.url_helpers.project_environments_path(first_project, format: :json))
end
end
end
def read_deployments
kubeclient.get_deployments(namespace: actual_namespace).as_json
return [] unless first_project
kubeclient.get_deployments(namespace: kubernetes_namespace_for(first_project)).as_json
rescue KubeException => err
raise err unless err.error_code == 404
......@@ -41,11 +47,28 @@ module EE
end
def read_pod_logs(pod_name, container: nil)
kubeclient.get_pod_log(pod_name, actual_namespace, container: container, tail_lines: LOGS_LIMIT).as_json
return [] unless first_project
kubeclient.get_pod_log(pod_name, kubernetes_namespace_for(first_project), container: container, tail_lines: LOGS_LIMIT).as_json
rescue ::Kubeclient::HttpError => err
raise err unless err.error_code == 404
[]
end
private
##
# TODO: KubernetesService is soon to be removed (https://gitlab.com/gitlab-org/gitlab-ce/issues/39217),
# after which we can retrieve the project from the cluster in all cases.
#
# This currently only works for project-level clusters, this is likely to be fixed as part of
# https://gitlab.com/gitlab-org/gitlab-ce/issues/61156, which will require logic to select
# a project from a cluster based on an environment.
def first_project
return project unless respond_to?(:cluster)
cluster.first_project if cluster.project_type?
end
end
end
......@@ -7,22 +7,29 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching
describe '#calculate_reactive_cache' do
subject { service.calculate_reactive_cache }
let!(:cluster) { create(:cluster, :project, enabled: true, platform_kubernetes: service) }
let(:cluster) { create(:cluster, :project, enabled: true, platform_kubernetes: service) }
let(:service) { create(:cluster_platform_kubernetes, :configured) }
let(:namespace) { cluster.kubernetes_namespace_for(cluster.first_project) }
context 'when kubernetes responds with valid pods and deployments' do
before do
stub_kubeclient_pods
stub_kubeclient_deployments
stub_kubeclient_pods(namespace)
stub_kubeclient_deployments(namespace)
end
it { is_expected.to eq(pods: [kube_pod], deployments: [kube_deployment]) }
context 'on a cluster that is not project level' do
let(:cluster) { create(:cluster, :group, platform_kubernetes: service) }
it { is_expected.to eq(pods: [], deployments: []) }
end
end
context 'when kubernetes responds with 404s' do
before do
stub_kubeclient_pods(status: 404)
stub_kubeclient_deployments(status: 404)
stub_kubeclient_pods(namespace, status: 404)
stub_kubeclient_deployments(namespace, status: 404)
end
it { is_expected.to eq(pods: [], deployments: []) }
......@@ -33,22 +40,29 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching
subject { service.read_pod_logs(pod_name) }
let(:pod_name) { 'foo' }
let!(:cluster) { create(:cluster, :project, enabled: true, platform_kubernetes: service) }
let(:cluster) { create(:cluster, :project, enabled: true, platform_kubernetes: service) }
let(:service) { create(:cluster_platform_kubernetes, :configured) }
let(:namespace) { cluster.kubernetes_namespace_for(cluster.first_project) }
context 'when kubernetes responds with valid logs' do
before do
stub_kubeclient_logs(pod_name)
stub_kubeclient_logs(pod_name, namespace)
end
it 'returns logs' do
expect(subject.body).to eq("\"Log 1\\nLog 2\\nLog 3\"")
end
context 'on a cluster that is not project level' do
let(:cluster) { create(:cluster, :group, platform_kubernetes: service) }
it { is_expected.to be_empty }
end
end
context 'when kubernetes response with 500s' do
before do
stub_kubeclient_logs(pod_name, status: 500)
stub_kubeclient_logs(pod_name, namespace, status: 500)
end
it { expect { subject }.to raise_error(::Kubeclient::HttpError) }
......@@ -56,7 +70,7 @@ describe Clusters::Platforms::Kubernetes, :use_clean_rails_memory_store_caching
context 'when kubernetes responds with 404s' do
before do
stub_kubeclient_logs(pod_name, status: 404)
stub_kubeclient_logs(pod_name, namespace, status: 404)
end
it { is_expected.to be_empty }
......
......@@ -77,6 +77,7 @@ describe KubernetesService, models: true, use_clean_rails_memory_store_caching:
describe '#calculate_reactive_cache' do
let(:project) { create(:kubernetes_project) }
let(:service) { project.deployment_platform }
let(:namespace) { service.kubernetes_namespace_for(project) }
subject { service.calculate_reactive_cache }
......@@ -90,8 +91,8 @@ describe KubernetesService, models: true, use_clean_rails_memory_store_caching:
context 'when kubernetes responds with valid pods and deployments' do
before do
stub_kubeclient_pods
stub_kubeclient_deployments
stub_kubeclient_pods(namespace)
stub_kubeclient_deployments(namespace)
end
it { is_expected.to eq(pods: [kube_pod], deployments: [kube_deployment]) }
......@@ -99,8 +100,8 @@ describe KubernetesService, models: true, use_clean_rails_memory_store_caching:
context 'when kubernetes responds with 500s' do
before do
stub_kubeclient_pods(status: 500)
stub_kubeclient_deployments(status: 500)
stub_kubeclient_pods(namespace, status: 500)
stub_kubeclient_deployments(namespace, status: 500)
end
it { expect { subject }.to raise_error(Kubeclient::HttpError) }
......@@ -108,11 +109,41 @@ describe KubernetesService, models: true, use_clean_rails_memory_store_caching:
context 'when kubernetes responds with 404s' do
before do
stub_kubeclient_pods(status: 404)
stub_kubeclient_deployments(status: 404)
stub_kubeclient_pods(namespace, status: 404)
stub_kubeclient_deployments(namespace, status: 404)
end
it { is_expected.to eq(pods: [], deployments: []) }
end
end
describe '#reactive_cache_updated' do
subject { service.reactive_cache_updated }
shared_examples 'cache expiry' do
let(:mock_store) { double }
it 'expires the environments path for the project' do
expect(::Gitlab::EtagCaching::Store).to receive(:new).and_return(mock_store)
expect(mock_store).to receive(:touch).with(::Gitlab::Routing.url_helpers.project_environments_path(project, format: :json))
subject
end
end
context 'Platforms::Kubernetes' do
let(:cluster) { create(:cluster, :project, :provided_by_gcp) }
let(:service) { cluster.platform_kubernetes }
let(:project) { cluster.first_project }
include_examples 'cache expiry'
end
context 'KubernetesService' do
let(:project) { create(:kubernetes_project) }
let(:service) { project.deployment_platform }
include_examples 'cache expiry'
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