summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-08-26 12:45:36 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-08-26 12:45:36 +0200
commit326dc7da3bb7e6537095277dc8ee8ae880774b62 (patch)
treea7975f8850f5cef9fc706de3a05a7bbd49f8da82
parentae99f74b77fc0f49a9efd5f71119e7de4e313629 (diff)
downloadgitlab-ce-326dc7da3bb7e6537095277dc8ee8ae880774b62.tar.gz
Check if kubernetes required before creating a job
-rw-r--r--app/models/ci/pipeline.rb10
-rw-r--r--lib/ci/gitlab_ci_yaml_processor.rb19
-rw-r--r--lib/gitlab/ci/config/entry/policy.rb11
-rw-r--r--spec/lib/ci/gitlab_ci_yaml_processor_spec.rb40
-rw-r--r--spec/models/ci/pipeline_spec.rb40
5 files changed, 112 insertions, 8 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb
index 36381956195..ed18a8631c8 100644
--- a/app/models/ci/pipeline.rb
+++ b/app/models/ci/pipeline.rb
@@ -32,6 +32,7 @@ module Ci
delegate :id, 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 :sha, presence: { unless: :importing? }
@@ -305,6 +306,15 @@ module Ci
@stage_seeds ||= config_processor.stage_seeds(self)
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?
stage_seeds.any?
end
diff --git a/lib/ci/gitlab_ci_yaml_processor.rb b/lib/ci/gitlab_ci_yaml_processor.rb
index 3efd9b3bdac..72a38e97648 100644
--- a/lib/ci/gitlab_ci_yaml_processor.rb
+++ b/lib/ci/gitlab_ci_yaml_processor.rb
@@ -44,6 +44,22 @@ module Ci
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
@jobs.map do |name, _|
build_attributes(name)
@@ -52,8 +68,7 @@ module Ci
def stage_seeds(pipeline)
seeds = @stages.uniq.map do |stage|
- builds = builds_for_stage_and_ref(
- stage, pipeline.ref, pipeline.tag?, pipeline.source)
+ builds = pipeline_stage_builds(stage, pipeline)
Gitlab::Ci::Stage::Seed.new(pipeline, stage, builds) if builds.any?
end
diff --git a/lib/gitlab/ci/config/entry/policy.rb b/lib/gitlab/ci/config/entry/policy.rb
index bcb76de65b2..a8bba3d3ea4 100644
--- a/lib/gitlab/ci/config/entry/policy.rb
+++ b/lib/gitlab/ci/config/entry/policy.rb
@@ -7,7 +7,7 @@ module Gitlab
#
class Policy < Simplifiable
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
include Entry::Validatable
@@ -21,20 +21,19 @@ module Gitlab
end
end
- class ExpressionsPolicy < Entry::Node
+ class ComplexPolicy < Entry::Node
include Entry::Validatable
include Entry::Attributable
- attributes :refs, :expressions
+ attributes :refs, :kubernetes
validations do
validates :config, presence: true
- validates :config, allowed_keys: %i[refs expressions]
+ validates :config, allowed_keys: %i[refs kubernetes]
with_options allow_nil: true do
validates :refs, array_of_strings_or_regexps: true
- validates :expressions, type: Array
- validates :expressions, presence: true
+ validates :kubernetes, inclusion: { in: [true] }
end
end
end
diff --git a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
index c70a4cb55fe..6d31ed875c5 100644
--- a/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
+++ b/spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
@@ -164,6 +164,46 @@ module Ci
expect(seeds.first.builds.dig(0, :name)).to eq 'spinach'
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
describe "#builds_for_ref" do
diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb
index eea494a22b6..b298b0b1354 100644
--- a/spec/models/ci/pipeline_spec.rb
+++ b/spec/models/ci/pipeline_spec.rb
@@ -542,6 +542,46 @@ describe Ci::Pipeline, :mailer do
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
context 'when pipeline has stage seeds' do
subject { build(:ci_pipeline_with_one_job) }