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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
587b8803
Commit
587b8803
authored
Jun 01, 2017
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'ee-fix-kubernetes-namespace' into 'master'
Ee fix kubernetes namespace See merge request !2014
parents
eae6778a
b90bde5c
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
49 additions
and
17 deletions
+49
-17
app/models/project_services/kubernetes_service.rb
app/models/project_services/kubernetes_service.rb
+13
-13
changelogs/unreleased/fix-terminals-support-for-kubernetes-service.yml
...released/fix-terminals-support-for-kubernetes-service.yml
+4
-0
spec/factories/services.rb
spec/factories/services.rb
+0
-1
spec/models/project_services/kubernetes_service_spec.rb
spec/models/project_services/kubernetes_service_spec.rb
+29
-0
spec/support/kubernetes_helpers.rb
spec/support/kubernetes_helpers.rb
+3
-3
No files found.
app/models/project_services/kubernetes_service.rb
View file @
587b8803
...
@@ -77,6 +77,14 @@ class KubernetesService < DeploymentService
...
@@ -77,6 +77,14 @@ class KubernetesService < DeploymentService
]
]
end
end
def
actual_namespace
if
namespace
.
present?
namespace
else
default_namespace
end
end
# Check we can connect to the Kubernetes API
# Check we can connect to the Kubernetes API
def
test
(
*
args
)
def
test
(
*
args
)
kubeclient
=
build_kubeclient!
kubeclient
=
build_kubeclient!
...
@@ -91,7 +99,7 @@ class KubernetesService < DeploymentService
...
@@ -91,7 +99,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_variabl
e
,
public:
true
}
{
key:
'KUBE_NAMESPACE'
,
value:
actual_namespac
e
,
public:
true
}
]
]
if
ca_pem
.
present?
if
ca_pem
.
present?
...
@@ -109,7 +117,7 @@ class KubernetesService < DeploymentService
...
@@ -109,7 +117,7 @@ class KubernetesService < DeploymentService
def
terminals
(
environment
)
def
terminals
(
environment
)
with_reactive_cache
do
|
data
|
with_reactive_cache
do
|
data
|
pods
=
filter_by_label
(
data
[
:pods
],
app:
environment
.
slug
)
pods
=
filter_by_label
(
data
[
:pods
],
app:
environment
.
slug
)
terminals
=
pods
.
flat_map
{
|
pod
|
terminals_for_pod
(
api_url
,
namespace
,
pod
)
}
terminals
=
pods
.
flat_map
{
|
pod
|
terminals_for_pod
(
api_url
,
actual_
namespace
,
pod
)
}
terminals
.
each
{
|
terminal
|
add_terminal_auth
(
terminal
,
terminal_auth
)
}
terminals
.
each
{
|
terminal
|
add_terminal_auth
(
terminal
,
terminal_auth
)
}
end
end
end
end
...
@@ -139,20 +147,12 @@ class KubernetesService < DeploymentService
...
@@ -139,20 +147,12 @@ class KubernetesService < DeploymentService
default_namespace
||
TEMPLATE_PLACEHOLDER
default_namespace
||
TEMPLATE_PLACEHOLDER
end
end
def
namespace_variable
if
namespace
.
present?
namespace
else
default_namespace
end
end
def
default_namespace
def
default_namespace
"
#{
project
.
path
}
-
#{
project
.
id
}
"
if
project
.
present?
"
#{
project
.
path
}
-
#{
project
.
id
}
"
if
project
.
present?
end
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
&&
actual_
namespace
&&
token
::
Kubeclient
::
Client
.
new
(
::
Kubeclient
::
Client
.
new
(
join_api_url
(
api_path
),
join_api_url
(
api_path
),
...
@@ -167,7 +167,7 @@ class KubernetesService < DeploymentService
...
@@ -167,7 +167,7 @@ class KubernetesService < DeploymentService
def
read_pods
def
read_pods
kubeclient
=
build_kubeclient!
kubeclient
=
build_kubeclient!
kubeclient
.
get_pods
(
namespace:
namespace
).
as_json
kubeclient
.
get_pods
(
namespace:
actual_
namespace
).
as_json
rescue
KubeException
=>
err
rescue
KubeException
=>
err
raise
err
unless
err
.
error_code
==
404
raise
err
unless
err
.
error_code
==
404
[]
[]
...
@@ -176,7 +176,7 @@ class KubernetesService < DeploymentService
...
@@ -176,7 +176,7 @@ class KubernetesService < DeploymentService
def
read_deployments
def
read_deployments
kubeclient
=
build_kubeclient!
(
api_path:
'apis/extensions'
,
api_version:
'v1beta1'
)
kubeclient
=
build_kubeclient!
(
api_path:
'apis/extensions'
,
api_version:
'v1beta1'
)
kubeclient
.
get_deployments
(
namespace:
namespace
).
as_json
kubeclient
.
get_deployments
(
namespace:
actual_
namespace
).
as_json
rescue
KubeException
=>
err
rescue
KubeException
=>
err
raise
err
unless
err
.
error_code
==
404
raise
err
unless
err
.
error_code
==
404
[]
[]
...
...
changelogs/unreleased/fix-terminals-support-for-kubernetes-service.yml
0 → 100644
View file @
587b8803
---
title
:
Fix terminals support for Kubernetes Service
merge_request
:
author
:
spec/factories/services.rb
View file @
587b8803
...
@@ -20,7 +20,6 @@ FactoryGirl.define do
...
@@ -20,7 +20,6 @@ FactoryGirl.define do
project
factory: :empty_project
project
factory: :empty_project
active
true
active
true
properties
({
properties
({
namespace:
'somepath'
,
api_url:
'https://kubernetes.example.com'
,
api_url:
'https://kubernetes.example.com'
,
token:
'a'
*
40
token:
'a'
*
40
})
})
...
...
spec/models/project_services/kubernetes_service_spec.rb
View file @
587b8803
...
@@ -87,6 +87,34 @@ describe KubernetesService, models: true, caching: true do
...
@@ -87,6 +87,34 @@ describe KubernetesService, models: true, caching: true do
end
end
end
end
describe
'#actual_namespace'
do
subject
{
service
.
actual_namespace
}
it
"returns the default namespace"
do
is_expected
.
to
eq
(
service
.
send
(
:default_namespace
))
end
context
'when namespace is specified'
do
before
do
service
.
namespace
=
'my-namespace'
end
it
"returns the user-namespace"
do
is_expected
.
to
eq
(
'my-namespace'
)
end
end
context
'when service is not assigned to project'
do
before
do
service
.
project
=
nil
end
it
"does not return namespace"
do
is_expected
.
to
be_nil
end
end
end
describe
'#test'
do
describe
'#test'
do
let
(
:discovery_url
)
{
'https://kubernetes.example.com/api/v1'
}
let
(
:discovery_url
)
{
'https://kubernetes.example.com/api/v1'
}
...
@@ -179,6 +207,7 @@ describe KubernetesService, models: true, caching: true do
...
@@ -179,6 +207,7 @@ describe KubernetesService, models: true, caching: true do
describe
'#terminals'
do
describe
'#terminals'
do
let
(
:environment
)
{
build
(
:environment
,
project:
project
,
name:
"env"
,
slug:
"env-000000"
)
}
let
(
:environment
)
{
build
(
:environment
,
project:
project
,
name:
"env"
,
slug:
"env-000000"
)
}
subject
{
service
.
terminals
(
environment
)
}
subject
{
service
.
terminals
(
environment
)
}
context
'with invalid pods'
do
context
'with invalid pods'
do
...
...
spec/support/kubernetes_helpers.rb
View file @
587b8803
...
@@ -20,14 +20,14 @@ module KubernetesHelpers
...
@@ -20,14 +20,14 @@ module KubernetesHelpers
def
stub_kubeclient_pods
(
response
=
nil
)
def
stub_kubeclient_pods
(
response
=
nil
)
stub_kubeclient_discover
stub_kubeclient_discover
pods_url
=
service
.
api_url
+
"/api/v1/namespaces/
#{
service
.
namespace
}
/pods"
pods_url
=
service
.
api_url
+
"/api/v1/namespaces/
#{
service
.
actual_
namespace
}
/pods"
WebMock
.
stub_request
(
:get
,
pods_url
).
to_return
(
response
||
kube_pods_response
)
WebMock
.
stub_request
(
:get
,
pods_url
).
to_return
(
response
||
kube_pods_response
)
end
end
def
stub_kubeclient_deployments
(
response
=
nil
)
def
stub_kubeclient_deployments
(
response
=
nil
)
stub_kubeclient_discover
stub_kubeclient_discover
deployments_url
=
service
.
api_url
+
"/apis/extensions/v1beta1/namespaces/
#{
service
.
namespace
}
/deployments"
deployments_url
=
service
.
api_url
+
"/apis/extensions/v1beta1/namespaces/
#{
service
.
actual_
namespace
}
/deployments"
WebMock
.
stub_request
(
:get
,
deployments_url
).
to_return
(
response
||
kube_deployments_response
)
WebMock
.
stub_request
(
:get
,
deployments_url
).
to_return
(
response
||
kube_deployments_response
)
end
end
...
@@ -112,7 +112,7 @@ module KubernetesHelpers
...
@@ -112,7 +112,7 @@ module KubernetesHelpers
containers
.
map
do
|
container
|
containers
.
map
do
|
container
|
terminal
=
{
terminal
=
{
selectors:
{
pod:
pod_name
,
container:
container
[
'name'
]
},
selectors:
{
pod:
pod_name
,
container:
container
[
'name'
]
},
url:
container_exec_url
(
service
.
api_url
,
service
.
namespace
,
pod_name
,
container
[
'name'
]),
url:
container_exec_url
(
service
.
api_url
,
service
.
actual_
namespace
,
pod_name
,
container
[
'name'
]),
subprotocols:
[
'channel.k8s.io'
],
subprotocols:
[
'channel.k8s.io'
],
headers:
{
'Authorization'
=>
[
"Bearer
#{
service
.
token
}
"
]
},
headers:
{
'Authorization'
=>
[
"Bearer
#{
service
.
token
}
"
]
},
created_at:
DateTime
.
parse
(
pod
[
'metadata'
][
'creationTimestamp'
]),
created_at:
DateTime
.
parse
(
pod
[
'metadata'
][
'creationTimestamp'
]),
...
...
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