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
Boxiang Sun
gitlab-ce
Commits
e6ffb158
Commit
e6ffb158
authored
Apr 04, 2017
by
Z.J. van de Weg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Don't autofill kubernetes namespace
parent
fa65b65b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
90 additions
and
48 deletions
+90
-48
app/models/project_services/kubernetes_service.rb
app/models/project_services/kubernetes_service.rb
+30
-13
app/views/projects/edit.html.haml
app/views/projects/edit.html.haml
+2
-0
changelogs/unreleased/zj-kube-service-auto-fill.yml
changelogs/unreleased/zj-kube-service-auto-fill.yml
+4
-0
spec/models/project_services/kubernetes_service_spec.rb
spec/models/project_services/kubernetes_service_spec.rb
+54
-35
No files found.
app/models/project_services/kubernetes_service.rb
View file @
e6ffb158
...
@@ -22,22 +22,21 @@ class KubernetesService < DeploymentService
...
@@ -22,22 +22,21 @@ class KubernetesService < DeploymentService
with_options
presence:
true
,
if: :activated?
do
with_options
presence:
true
,
if: :activated?
do
validates
:api_url
,
url:
true
validates
:api_url
,
url:
true
validates
:token
validates
:token
validates
:namespace
,
format:
{
with:
Gitlab
::
Regex
.
kubernetes_namespace_regex
,
message:
Gitlab
::
Regex
.
kubernetes_namespace_regex_message
,
},
length:
1
..
63
end
end
validates
:namespace
,
allow_blank:
true
,
length:
1
..
63
,
if: :activated?
,
format:
{
with:
Gitlab
::
Regex
.
kubernetes_namespace_regex
,
message:
Gitlab
::
Regex
.
kubernetes_namespace_regex_message
}
after_save
:clear_reactive_cache!
after_save
:clear_reactive_cache!
def
initialize_properties
def
initialize_properties
if
properties
.
nil?
self
.
properties
=
{}
if
properties
.
nil?
self
.
properties
=
{}
self
.
namespace
=
"
#{
project
.
path
}
-
#{
project
.
id
}
"
if
project
.
present?
end
end
end
def
title
def
title
...
@@ -62,7 +61,7 @@ class KubernetesService < DeploymentService
...
@@ -62,7 +61,7 @@ class KubernetesService < DeploymentService
{
type:
'text'
,
{
type:
'text'
,
name:
'namespace'
,
name:
'namespace'
,
title:
'Kubernetes namespace'
,
title:
'Kubernetes namespace'
,
placeholder:
'Kubernetes namespace'
},
placeholder:
namespace_placeholder
},
{
type:
'text'
,
{
type:
'text'
,
name:
'api_url'
,
name:
'api_url'
,
title:
'API URL'
,
title:
'API URL'
,
...
@@ -92,7 +91,7 @@ class KubernetesService < DeploymentService
...
@@ -92,7 +91,7 @@ class KubernetesService < DeploymentService
variables
=
[
variables
=
[
{
key:
'KUBE_URL'
,
value:
api_url
,
public:
true
},
{
key:
'KUBE_URL'
,
value:
api_url
,
public:
true
},
{
key:
'KUBE_TOKEN'
,
value:
token
,
public:
false
},
{
key:
'KUBE_TOKEN'
,
value:
token
,
public:
false
},
{
key:
'KUBE_NAMESPACE'
,
value:
namespace
,
public:
true
}
{
key:
'KUBE_NAMESPACE'
,
value:
namespace
_variable
,
public:
true
}
]
]
if
ca_pem
.
present?
if
ca_pem
.
present?
...
@@ -135,8 +134,26 @@ class KubernetesService < DeploymentService
...
@@ -135,8 +134,26 @@ class KubernetesService < DeploymentService
{
pods:
pods
}
{
pods:
pods
}
end
end
TEMPLATE_PLACEHOLDER
=
'Kubernetes namespace'
.
freeze
private
private
def
namespace_placeholder
default_namespace
||
TEMPLATE_PLACEHOLDER
end
def
namespace_variable
if
namespace
.
present?
namespace
else
default_namespace
end
end
def
default_namespace
"
#{
project
.
path
}
-
#{
project
.
id
}
"
if
project
.
present?
end
def
build_kubeclient!
(
api_path:
'api'
,
api_version:
'v1'
)
def
build_kubeclient!
(
api_path:
'api'
,
api_version:
'v1'
)
raise
"Incomplete settings"
unless
api_url
&&
namespace
&&
token
raise
"Incomplete settings"
unless
api_url
&&
namespace
&&
token
...
...
app/views/projects/edit.html.haml
View file @
e6ffb158
...
@@ -238,6 +238,8 @@
...
@@ -238,6 +238,8 @@
%ul
%ul
%li
Be careful. Renaming a project's repository can have unintended side effects.
%li
Be careful. Renaming a project's repository can have unintended side effects.
%li
You will need to update your local repositories to point to the new location.
%li
You will need to update your local repositories to point to the new location.
-
if
@project
.
deployment_services
.
any?
%li
Your deployment services will be broken, you will need to manually fix the services after renaming.
=
f
.
submit
'Rename project'
,
class:
"btn btn-warning"
=
f
.
submit
'Rename project'
,
class:
"btn btn-warning"
-
if
can?
(
current_user
,
:change_namespace
,
@project
)
-
if
can?
(
current_user
,
:change_namespace
,
@project
)
%hr
%hr
...
...
changelogs/unreleased/zj-kube-service-auto-fill.yml
0 → 100644
View file @
e6ffb158
---
title
:
Don't fill in the default kubernetes namespace
merge_request
:
author
:
spec/models/project_services/kubernetes_service_spec.rb
View file @
e6ffb158
...
@@ -4,7 +4,7 @@ describe KubernetesService, models: true, caching: true do
...
@@ -4,7 +4,7 @@ describe KubernetesService, models: true, caching: true do
include
KubernetesHelpers
include
KubernetesHelpers
include
ReactiveCachingHelpers
include
ReactiveCachingHelpers
let
(
:project
)
{
create
(
:kubernetes_project
)
}
let
(
:project
)
{
build_stubbed
(
:kubernetes_project
)
}
let
(
:service
)
{
project
.
kubernetes_service
}
let
(
:service
)
{
project
.
kubernetes_service
}
# We use Kubeclient to interactive with the Kubernetes API. It will
# We use Kubeclient to interactive with the Kubernetes API. It will
...
@@ -32,7 +32,8 @@ describe KubernetesService, models: true, caching: true do
...
@@ -32,7 +32,8 @@ describe KubernetesService, models: true, caching: true do
describe
'Validations'
do
describe
'Validations'
do
context
'when service is active'
do
context
'when service is active'
do
before
{
subject
.
active
=
true
}
before
{
subject
.
active
=
true
}
it
{
is_expected
.
to
validate_presence_of
(
:namespace
)
}
it
{
is_expected
.
not_to
validate_presence_of
(
:namespace
)
}
it
{
is_expected
.
to
validate_presence_of
(
:api_url
)
}
it
{
is_expected
.
to
validate_presence_of
(
:api_url
)
}
it
{
is_expected
.
to
validate_presence_of
(
:token
)
}
it
{
is_expected
.
to
validate_presence_of
(
:token
)
}
...
@@ -55,7 +56,7 @@ describe KubernetesService, models: true, caching: true do
...
@@ -55,7 +56,7 @@ describe KubernetesService, models: true, caching: true do
'a.b'
=>
false
,
'a.b'
=>
false
,
'a*b'
=>
false
,
'a*b'
=>
false
,
}.
each
do
|
namespace
,
validity
|
}.
each
do
|
namespace
,
validity
|
it
"
should validate
#{
namespace
}
as
#{
validity
?
'valid'
:
'invalid'
}
"
do
it
"
validates
#{
namespace
}
as
#{
validity
?
'valid'
:
'invalid'
}
"
do
subject
.
namespace
=
namespace
subject
.
namespace
=
namespace
expect
(
subject
.
valid?
).
to
eq
(
validity
)
expect
(
subject
.
valid?
).
to
eq
(
validity
)
...
@@ -66,24 +67,40 @@ describe KubernetesService, models: true, caching: true do
...
@@ -66,24 +67,40 @@ describe KubernetesService, models: true, caching: true do
context
'when service is inactive'
do
context
'when service is inactive'
do
before
{
subject
.
active
=
false
}
before
{
subject
.
active
=
false
}
it
{
is_expected
.
not_to
validate_presence_of
(
:namespace
)
}
it
{
is_expected
.
not_to
validate_presence_of
(
:api_url
)
}
it
{
is_expected
.
not_to
validate_presence_of
(
:api_url
)
}
it
{
is_expected
.
not_to
validate_presence_of
(
:token
)
}
it
{
is_expected
.
not_to
validate_presence_of
(
:token
)
}
end
end
end
end
describe
'#initialize_properties'
do
describe
'#initialize_properties'
do
context
'with a project'
do
context
'without a project'
do
let
(
:namespace_name
)
{
"
#{
project
.
path
}
-
#{
project
.
id
}
"
}
it
'leaves the namespace unset'
do
expect
(
described_class
.
new
.
namespace
).
to
be_nil
end
end
end
describe
'#fields'
do
let
(
:kube_namespace
)
do
subject
.
fields
.
find
{
|
h
|
h
[
:name
]
==
'namespace'
}
end
context
'as template'
do
before
{
subject
.
template
=
true
}
it
'defaults to the project name with ID'
do
it
'sets the namespace to the default'
do
expect
(
described_class
.
new
(
project:
project
).
namespace
).
to
eq
(
namespace_name
)
expect
(
kube_namespace
).
not_to
be_nil
expect
(
kube_namespace
[
:placeholder
]).
to
eq
(
subject
.
class
::
TEMPLATE_PLACEHOLDER
)
end
end
end
end
context
'without a project'
do
context
'with associated project'
do
it
'leaves the namespace unset'
do
before
{
subject
.
project
=
project
}
expect
(
described_class
.
new
.
namespace
).
to
be_nil
it
'sets the namespace to the default'
do
expect
(
kube_namespace
).
not_to
be_nil
expect
(
kube_namespace
[
:placeholder
]).
to
match
(
/\A
#{
Gitlab
::
Regex
::
PATH_REGEX_STR
}
-\d+\z/
)
end
end
end
end
end
end
...
@@ -138,38 +155,40 @@ describe KubernetesService, models: true, caching: true do
...
@@ -138,38 +155,40 @@ describe KubernetesService, models: true, caching: true do
before
do
before
do
subject
.
api_url
=
'https://kube.domain.com'
subject
.
api_url
=
'https://kube.domain.com'
subject
.
token
=
'token'
subject
.
token
=
'token'
subject
.
namespace
=
'my-project'
subject
.
ca_pem
=
'CA PEM DATA'
subject
.
ca_pem
=
'CA PEM DATA'
subject
.
project
=
project
end
end
it
'sets KUBE_URL'
do
context
'namespace is provided'
do
expect
(
subject
.
predefined_variables
).
to
include
(
before
{
subject
.
namespace
=
'my-project'
}
{
key:
'KUBE_URL'
,
value:
'https://kube.domain.com'
,
public:
true
}
)
end
it
'sets KUBE_TOKEN'
do
it
'sets the variables'
do
expect
(
subject
.
predefined_variables
).
to
include
(
expect
(
subject
.
predefined_variables
).
to
include
(
{
key:
'KUBE_TOKEN'
,
value:
'token'
,
public:
false
}
{
key:
'KUBE_URL'
,
value:
'https://kube.domain.com'
,
public:
true
},
)
{
key:
'KUBE_TOKEN'
,
value:
'token'
,
public:
false
},
{
key:
'KUBE_NAMESPACE'
,
value:
'my-project'
,
public:
true
},
{
key:
'KUBE_CA_PEM'
,
value:
'CA PEM DATA'
,
public:
true
},
{
key:
'KUBE_CA_PEM_FILE'
,
value:
'CA PEM DATA'
,
public:
true
,
file:
true
},
)
end
end
end
it
'sets KUBE_NAMESPACE'
do
context
'no namespace provided'
do
expect
(
subject
.
predefined_variables
).
to
include
(
it
'sets the variables'
do
{
key:
'KUBE_NAMESPACE'
,
value:
'my-project'
,
public:
true
}
expect
(
subject
.
predefined_variables
).
to
include
(
)
{
key:
'KUBE_URL'
,
value:
'https://kube.domain.com'
,
public:
true
},
end
{
key:
'KUBE_TOKEN'
,
value:
'token'
,
public:
false
},
{
key:
'KUBE_CA_PEM'
,
value:
'CA PEM DATA'
,
public:
true
},
{
key:
'KUBE_CA_PEM_FILE'
,
value:
'CA PEM DATA'
,
public:
true
,
file:
true
},
)
end
it
'sets KUBE_CA_PEM'
do
it
'sets the KUBE_NAMESPACE'
do
expect
(
subject
.
predefined_variables
).
to
include
(
kube_namespace
=
subject
.
predefined_variables
.
find
{
|
h
|
h
[
:key
]
==
'KUBE_NAMESPACE'
}
{
key:
'KUBE_CA_PEM'
,
value:
'CA PEM DATA'
,
public:
true
}
)
end
it
'sets KUBE_CA_PEM_FILE'
do
expect
(
kube_namespace
).
not_to
be_nil
expect
(
subject
.
predefined_variables
).
to
include
(
expect
(
kube_namespace
[
:value
]).
to
match
(
/\A
#{
Gitlab
::
Regex
::
PATH_REGEX_STR
}
-\d+\z/
)
{
key:
'KUBE_CA_PEM_FILE'
,
value:
'CA PEM DATA'
,
public:
true
,
file:
true
}
end
)
end
end
end
end
...
...
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