Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Jérome Perrin
gitlab-ce
Commits
326dc7da
Commit
326dc7da
authored
Aug 26, 2017
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Check if kubernetes required before creating a job
parent
ae99f74b
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
112 additions
and
8 deletions
+112
-8
app/models/ci/pipeline.rb
app/models/ci/pipeline.rb
+10
-0
lib/ci/gitlab_ci_yaml_processor.rb
lib/ci/gitlab_ci_yaml_processor.rb
+17
-2
lib/gitlab/ci/config/entry/policy.rb
lib/gitlab/ci/config/entry/policy.rb
+5
-6
spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
+40
-0
spec/models/ci/pipeline_spec.rb
spec/models/ci/pipeline_spec.rb
+40
-0
No files found.
app/models/ci/pipeline.rb
View file @
326dc7da
...
...
@@ -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
...
...
lib/ci/gitlab_ci_yaml_processor.rb
View file @
326dc7da
...
...
@@ -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
...
...
lib/gitlab/ci/config/entry/policy.rb
View file @
326dc7da
...
...
@@ -7,7 +7,7 @@ module Gitlab
#
class
Policy
<
Simplifiable
strategy
:RefsPolicy
,
if:
->
(
config
)
{
config
.
is_a?
(
Array
)
}
strategy
:
Expressions
Policy
,
if:
->
(
config
)
{
config
.
is_a?
(
Hash
)
}
strategy
:
Complex
Policy
,
if:
->
(
config
)
{
config
.
is_a?
(
Hash
)
}
class
RefsPolicy
<
Entry
::
Node
include
Entry
::
Validatable
...
...
@@ -21,20 +21,19 @@ module Gitlab
end
end
class
Expressions
Policy
<
Entry
::
Node
class
Complex
Policy
<
Entry
::
Node
include
Entry
::
Validatable
include
Entry
::
Attributable
attributes
:refs
,
:
expression
s
attributes
:refs
,
:
kubernete
s
validations
do
validates
:config
,
presence:
true
validates
:config
,
allowed_keys:
%i[refs
expression
s]
validates
:config
,
allowed_keys:
%i[refs
kubernete
s]
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
...
...
spec/lib/ci/gitlab_ci_yaml_processor_spec.rb
View file @
326dc7da
...
...
@@ -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
...
...
spec/models/ci/pipeline_spec.rb
View file @
326dc7da
...
...
@@ -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
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment