Use correct k8s namespace in Prometheus queries

Before this commit the wrong namespace could have been used in
Prometheus queries for group-level installations.
This commit is contained in:
Peter Leitzen 2019-04-27 16:35:32 +02:00
parent ab9910f7a5
commit de69a808a0
No known key found for this signature in database
GPG key ID: 97DE9D2E708A225E
3 changed files with 33 additions and 5 deletions

View file

@ -76,6 +76,10 @@ module Clusters
end
end
def namespace_for(project)
cluster.find_or_initialize_kubernetes_namespace_for_project(project)&.namespace
end
def predefined_variables(project:)
Gitlab::Ci::Variables::Collection.new.tap do |variables|
variables.append(key: 'KUBE_URL', value: api_url)

View file

@ -4,9 +4,13 @@ module Gitlab
module Prometheus
module QueryVariables
def self.call(environment)
deployment_platform = environment.deployment_platform
namespace = deployment_platform&.namespace_for(environment.project) ||
deployment_platform&.actual_namespace || ''
{
ci_environment_slug: environment.slug,
kube_namespace: environment.deployment_platform&.actual_namespace || '',
kube_namespace: namespace,
environment_filter: %{container_name!="POD",environment="#{environment.slug}"}
}
end

View file

@ -4,6 +4,7 @@ require 'spec_helper'
describe Gitlab::Prometheus::QueryVariables do
describe '.call' do
let(:project) { environment.project }
let(:environment) { create(:environment) }
let(:slug) { environment.slug }
@ -21,13 +22,32 @@ describe Gitlab::Prometheus::QueryVariables do
end
context 'with deployment platform' do
let(:kube_namespace) { environment.deployment_platform.actual_namespace }
context 'with project cluster' do
let(:kube_namespace) { environment.deployment_platform.actual_namespace }
before do
create(:cluster, :provided_by_user, projects: [environment.project])
before do
create(:cluster, :project, :provided_by_user, projects: [project])
end
it { is_expected.to include(kube_namespace: kube_namespace) }
end
it { is_expected.to include(kube_namespace: kube_namespace) }
context 'with group cluster' do
let(:cluster) { create(:cluster, :group, :provided_by_user, groups: [group]) }
let(:group) { create(:group) }
let(:project2) { create(:project) }
let(:kube_namespace) { k8s_ns.namespace }
let!(:k8s_ns) { create(:cluster_kubernetes_namespace, cluster: cluster, project: project) }
let!(:k8s_ns2) { create(:cluster_kubernetes_namespace, cluster: cluster, project: project2) }
before do
group.projects << project
group.projects << project2
end
it { is_expected.to include(kube_namespace: kube_namespace) }
end
end
end
end