Extract query variables into own module

This commit is contained in:
Peter Leitzen 2018-11-16 15:02:43 +01:00
parent fbbe5ccd1b
commit b47a3a4085
No known key found for this signature in database
GPG Key ID: 97DE9D2E708A225E
4 changed files with 55 additions and 5 deletions

View File

@ -0,0 +1,5 @@
---
title: Extract into Gitlab::Prometheus::QueryVariables
merge_request: 23335
author:
type: other

View File

@ -83,11 +83,8 @@ module Gitlab
end
def common_query_context(environment, timeframe_start:, timeframe_end:)
base_query_context(timeframe_start, timeframe_end).merge({
ci_environment_slug: environment.slug,
kube_namespace: environment.deployment_platform&.actual_namespace || '',
environment_filter: %{container_name!="POD",environment="#{environment.slug}"}
})
base_query_context(timeframe_start, timeframe_end)
.merge(QueryVariables.call(environment))
end
def base_query_context(timeframe_start, timeframe_end)

View File

@ -0,0 +1,15 @@
# frozen_string_literal: true
module Gitlab
module Prometheus
module QueryVariables
def self.call(environment)
{
ci_environment_slug: environment.slug,
kube_namespace: environment.deployment_platform&.actual_namespace || '',
environment_filter: %{container_name!="POD",environment="#{environment.slug}"}
}
end
end
end
end

View File

@ -0,0 +1,33 @@
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::Prometheus::QueryVariables do
describe '.call' do
set(:environment) { create(:environment) }
let(:slug) { environment.slug }
subject { described_class.call(environment) }
it { is_expected.to include(ci_environment_slug: slug) }
it do
is_expected.to include(environment_filter:
%{container_name!="POD",environment="#{slug}"})
end
context 'without deployment platform' do
it { is_expected.to include(kube_namespace: '') }
end
context 'with deplyoment platform' do
let(:kube_namespace) { environment.deployment_platform.actual_namespace }
before do
create(:cluster, :provided_by_user, projects: [environment.project])
end
it { is_expected.to include(kube_namespace: kube_namespace) }
end
end
end