Commit de69a808 authored by Peter Leitzen's avatar Peter Leitzen

Use correct k8s namespace in Prometheus queries

Before this commit the wrong namespace could have been used in
Prometheus queries for group-level installations.
parent ab9910f7
......@@ -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)
......
......@@ -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
......
......@@ -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
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