Commit 326dc7da authored by Grzegorz Bizon's avatar Grzegorz Bizon

Check if kubernetes required before creating a job

parent ae99f74b
...@@ -32,6 +32,7 @@ module Ci ...@@ -32,6 +32,7 @@ module Ci
delegate :id, to: :project, prefix: true delegate :id, to: :project, prefix: true
delegate :deployment_variables, to: :project, prefix: true delegate :deployment_variables, to: :project, prefix: true
delegate :secret_variables_for, to: :project, prefix: true
validates :source, exclusion: { in: %w(unknown), unless: :importing? }, on: :create validates :source, exclusion: { in: %w(unknown), unless: :importing? }, on: :create
validates :sha, presence: { unless: :importing? } validates :sha, presence: { unless: :importing? }
...@@ -305,6 +306,15 @@ module Ci ...@@ -305,6 +306,15 @@ module Ci
@stage_seeds ||= config_processor.stage_seeds(self) @stage_seeds ||= config_processor.stage_seeds(self)
end end
def variables
project_secret_variables_for(ref: ref).map(&:to_runner_variable) +
project_deployment_variables
end
def has_kubernetes_available?
(variables.map { |v| v.fetch(:key) } & %w[KUBECONFIG KUBE_DOMAIN]).many?
end
def has_stage_seeds? def has_stage_seeds?
stage_seeds.any? stage_seeds.any?
end end
......
...@@ -44,6 +44,22 @@ module Ci ...@@ -44,6 +44,22 @@ module Ci
end end
end end
def pipeline_stage_builds(stage, pipeline)
builds = builds_for_stage_and_ref(
stage, pipeline.ref, pipeline.tag?, pipeline.source)
builds.select do |build|
job = @jobs[build.fetch(:name).to_sym]
has_kubernetes = pipeline.has_kubernetes_available?
only_kubernetes = job.dig(:only, :kubernetes)
except_kubernetes = job.dig(:except, :kubernetes)
[!only_kubernetes & !except_kubernetes,
only_kubernetes & has_kubernetes,
except_kubernetes & !has_kubernetes].any?
end
end
def builds def builds
@jobs.map do |name, _| @jobs.map do |name, _|
build_attributes(name) build_attributes(name)
...@@ -52,8 +68,7 @@ module Ci ...@@ -52,8 +68,7 @@ module Ci
def stage_seeds(pipeline) def stage_seeds(pipeline)
seeds = @stages.uniq.map do |stage| seeds = @stages.uniq.map do |stage|
builds = builds_for_stage_and_ref( builds = pipeline_stage_builds(stage, pipeline)
stage, pipeline.ref, pipeline.tag?, pipeline.source)
Gitlab::Ci::Stage::Seed.new(pipeline, stage, builds) if builds.any? Gitlab::Ci::Stage::Seed.new(pipeline, stage, builds) if builds.any?
end end
......
...@@ -7,7 +7,7 @@ module Gitlab ...@@ -7,7 +7,7 @@ module Gitlab
# #
class Policy < Simplifiable class Policy < Simplifiable
strategy :RefsPolicy, if: -> (config) { config.is_a?(Array) } strategy :RefsPolicy, if: -> (config) { config.is_a?(Array) }
strategy :ExpressionsPolicy, if: -> (config) { config.is_a?(Hash) } strategy :ComplexPolicy, if: -> (config) { config.is_a?(Hash) }
class RefsPolicy < Entry::Node class RefsPolicy < Entry::Node
include Entry::Validatable include Entry::Validatable
...@@ -21,20 +21,19 @@ module Gitlab ...@@ -21,20 +21,19 @@ module Gitlab
end end
end end
class ExpressionsPolicy < Entry::Node class ComplexPolicy < Entry::Node
include Entry::Validatable include Entry::Validatable
include Entry::Attributable include Entry::Attributable
attributes :refs, :expressions attributes :refs, :kubernetes
validations do validations do
validates :config, presence: true validates :config, presence: true
validates :config, allowed_keys: %i[refs expressions] validates :config, allowed_keys: %i[refs kubernetes]
with_options allow_nil: true do with_options allow_nil: true do
validates :refs, array_of_strings_or_regexps: true validates :refs, array_of_strings_or_regexps: true
validates :expressions, type: Array validates :kubernetes, inclusion: { in: [true] }
validates :expressions, presence: true
end end
end end
end end
......
...@@ -164,6 +164,46 @@ module Ci ...@@ -164,6 +164,46 @@ module Ci
expect(seeds.first.builds.dig(0, :name)).to eq 'spinach' expect(seeds.first.builds.dig(0, :name)).to eq 'spinach'
end end
end end
context 'when kubernetes policy is specified' do
let(:pipeline) { create(:ci_empty_pipeline) }
let(:config) do
YAML.dump(
spinach: { stage: 'test', script: 'spinach' },
production: { stage: 'deploy', script: 'cap', only: {
kubernetes: true } }
)
end
context 'when kubernetes is configured' do
let(:project) { create(:kubernetes_project) }
let(:pipeline) { create(:ci_empty_pipeline, project: project) }
before do
create(:ci_variable, key: 'KUBE_DOMAIN',
protected: false,
project: project)
end
it 'returns seeds for kubernetes dependent job' do
seeds = subject.stage_seeds(pipeline)
expect(seeds.size).to eq 2
expect(seeds.first.builds.dig(0, :name)).to eq 'spinach'
expect(seeds.second.builds.dig(0, :name)).to eq 'production'
end
end
context 'when kubernetes is not configured' do
it 'does not return seeds for kubernetes dependent job' do
seeds = subject.stage_seeds(pipeline)
expect(seeds.size).to eq 1
expect(seeds.first.builds.dig(0, :name)).to eq 'spinach'
end
end
end
end end
describe "#builds_for_ref" do describe "#builds_for_ref" do
......
...@@ -542,6 +542,46 @@ describe Ci::Pipeline, :mailer do ...@@ -542,6 +542,46 @@ describe Ci::Pipeline, :mailer do
end end
end end
context 'when kubernetes is configured' do
let(:project) { create(:kubernetes_project) }
before do
create(:ci_variable, key: 'KUBE_DOMAIN',
protected: false,
project: project)
end
describe '#variables' do
it 'returns kubernetes-related variables' do
variables = pipeline.variables.map { |v| v.fetch(:key) }
expect(variables).to include 'KUBECONFIG', 'KUBE_DOMAIN'
end
end
describe '#has_kubernetes_available?' do
it 'returns true' do
expect(pipeline).to have_kubernetes_available
end
end
end
context 'when kubernetes is not configured' do
describe '#variables' do
it 'does not return kubernetes related variables' do
variables = pipeline.variables.map { |v| v.fetch(:key) }
expect(variables).not_to include 'KUBECONFIG', 'KUBE_DOMAIN'
end
end
describe '#has_kubernetes_available?' do
it 'returns false' do
expect(pipeline).not_to have_kubernetes_available
end
end
end
describe '#has_stage_seeds?' do describe '#has_stage_seeds?' do
context 'when pipeline has stage seeds' do context 'when pipeline has stage seeds' do
subject { build(:ci_pipeline_with_one_job) } subject { build(:ci_pipeline_with_one_job) }
......
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