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
07a65da1
Commit
07a65da1
authored
Jun 16, 2017
by
Lin Jen-Shin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Generate KUBECONFIG in KubernetesService#predefined_variables
parent
bcb7d885
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
138 additions
and
16 deletions
+138
-16
app/models/project_services/kubernetes_service.rb
app/models/project_services/kubernetes_service.rb
+13
-1
lib/gitlab/kubernetes.rb
lib/gitlab/kubernetes.rb
+39
-0
spec/fixtures/config/kubeconfig-without-ca.yml
spec/fixtures/config/kubeconfig-without-ca.yml
+18
-0
spec/fixtures/config/kubeconfig.yml
spec/fixtures/config/kubeconfig.yml
+19
-0
spec/lib/gitlab/kubernetes_spec.rb
spec/lib/gitlab/kubernetes_spec.rb
+24
-0
spec/models/project_services/kubernetes_service_spec.rb
spec/models/project_services/kubernetes_service_spec.rb
+25
-15
No files found.
app/models/project_services/kubernetes_service.rb
View file @
07a65da1
...
@@ -96,10 +96,14 @@ class KubernetesService < DeploymentService
...
@@ -96,10 +96,14 @@ class KubernetesService < DeploymentService
end
end
def
predefined_variables
def
predefined_variables
config
=
YAML
.
dump
(
kubeconfig
)
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:
actual_namespace
,
public:
true
}
{
key:
'KUBE_NAMESPACE'
,
value:
actual_namespace
,
public:
true
},
{
key:
'KUBECONFIG'
,
value:
config
,
public:
false
},
{
key:
'KUBECONFIG_FILE'
,
value:
config
,
public:
false
,
file:
true
},
]
]
if
ca_pem
.
present?
if
ca_pem
.
present?
...
@@ -135,6 +139,14 @@ class KubernetesService < DeploymentService
...
@@ -135,6 +139,14 @@ class KubernetesService < DeploymentService
private
private
def
kubeconfig
to_kubeconfig
(
url:
api_url
,
namespace:
actual_namespace
,
token:
token
,
ca_pem:
ca_pem
)
end
def
namespace_placeholder
def
namespace_placeholder
default_namespace
||
TEMPLATE_PLACEHOLDER
default_namespace
||
TEMPLATE_PLACEHOLDER
end
end
...
...
lib/gitlab/kubernetes.rb
View file @
07a65da1
...
@@ -76,5 +76,44 @@ module Gitlab
...
@@ -76,5 +76,44 @@ module Gitlab
url
.
to_s
url
.
to_s
end
end
def
to_kubeconfig
(
url
:,
namespace
:,
token
:,
ca_pem:
nil
)
config
=
{
apiVersion:
'v1'
,
clusters:
[
name:
'gitlab-deploy'
,
cluster:
{
server:
url
},
],
contexts:
[
name:
'gitlab-deploy'
,
context:
{
cluster:
'gitlab-deploy'
,
namespace:
namespace
,
user:
'gitlab-deploy'
},
],
:'current-context'
=>
'gitlab-deploy'
,
kind:
'Config'
,
users:
[
{
name:
'gitlab-deploy'
,
user:
{
token:
token
}
}
]
}
kubeconfig_embed_ca_pem
(
config
,
ca_pem
)
if
ca_pem
config
.
deep_stringify_keys
end
private
def
kubeconfig_embed_ca_pem
(
config
,
ca_pem
)
cluster
=
config
.
dig
(
:clusters
,
0
,
:cluster
)
cluster
[
:'certificate-authority-data'
]
=
ca_pem
end
end
end
end
end
spec/fixtures/config/kubeconfig-without-ca.yml
0 → 100644
View file @
07a65da1
---
apiVersion
:
v1
clusters
:
-
name
:
gitlab-deploy
cluster
:
server
:
https://kube.domain.com
contexts
:
-
name
:
gitlab-deploy
context
:
cluster
:
gitlab-deploy
namespace
:
NAMESPACE
user
:
gitlab-deploy
current-context
:
gitlab-deploy
kind
:
Config
users
:
-
name
:
gitlab-deploy
user
:
token
:
TOKEN
spec/fixtures/config/kubeconfig.yml
0 → 100644
View file @
07a65da1
---
apiVersion
:
v1
clusters
:
-
name
:
gitlab-deploy
cluster
:
server
:
https://kube.domain.com
certificate-authority-data
:
PEM
contexts
:
-
name
:
gitlab-deploy
context
:
cluster
:
gitlab-deploy
namespace
:
NAMESPACE
user
:
gitlab-deploy
current-context
:
gitlab-deploy
kind
:
Config
users
:
-
name
:
gitlab-deploy
user
:
token
:
TOKEN
spec/lib/gitlab/kubernetes_spec.rb
View file @
07a65da1
...
@@ -46,4 +46,28 @@ describe Gitlab::Kubernetes do
...
@@ -46,4 +46,28 @@ describe Gitlab::Kubernetes do
expect
(
filter_by_label
(
items
,
app:
'foo'
)).
to
eq
(
matching_items
)
expect
(
filter_by_label
(
items
,
app:
'foo'
)).
to
eq
(
matching_items
)
end
end
end
end
describe
'#to_kubeconfig'
do
subject
do
to_kubeconfig
(
url:
'https://kube.domain.com'
,
namespace:
'NAMESPACE'
,
token:
'TOKEN'
,
ca_pem:
ca_pem
)
end
context
'when CA PEM is provided'
do
let
(
:ca_pem
)
{
'PEM'
}
let
(
:path
)
{
expand_fixture_path
(
'config/kubeconfig.yml'
)
}
it
{
is_expected
.
to
eq
(
YAML
.
load_file
(
path
))
}
end
context
'when CA PEM is not provided'
do
let
(
:ca_pem
)
{
nil
}
let
(
:path
)
{
expand_fixture_path
(
'config/kubeconfig-without-ca.yml'
)
}
it
{
is_expected
.
to
eq
(
YAML
.
load_file
(
path
))
}
end
end
end
end
spec/models/project_services/kubernetes_service_spec.rb
View file @
07a65da1
...
@@ -129,7 +129,7 @@ describe KubernetesService, models: true, caching: true do
...
@@ -129,7 +129,7 @@ describe KubernetesService, models: true, caching: true do
it
"returns the default namespace"
do
it
"returns the default namespace"
do
is_expected
.
to
eq
(
service
.
send
(
:default_namespace
))
is_expected
.
to
eq
(
service
.
send
(
:default_namespace
))
end
end
context
'when namespace is specified'
do
context
'when namespace is specified'
do
before
do
before
do
service
.
namespace
=
'my-namespace'
service
.
namespace
=
'my-namespace'
...
@@ -201,6 +201,13 @@ describe KubernetesService, models: true, caching: true do
...
@@ -201,6 +201,13 @@ describe KubernetesService, models: true, caching: true do
end
end
describe
'#predefined_variables'
do
describe
'#predefined_variables'
do
let
(
:kubeconfig
)
do
File
.
read
(
expand_fixture_path
(
'config/kubeconfig.yml'
))
.
gsub
(
'TOKEN'
,
'token'
)
.
gsub
(
'PEM'
,
'CA PEM DATA'
)
.
gsub
(
'NAMESPACE'
,
namespace
)
end
before
do
before
do
subject
.
api_url
=
'https://kube.domain.com'
subject
.
api_url
=
'https://kube.domain.com'
subject
.
token
=
'token'
subject
.
token
=
'token'
...
@@ -208,32 +215,35 @@ describe KubernetesService, models: true, caching: true do
...
@@ -208,32 +215,35 @@ describe KubernetesService, models: true, caching: true do
subject
.
project
=
project
subject
.
project
=
project
end
end
context
'namespace is provided'
do
shared_examples
'setting variables'
do
before
do
subject
.
namespace
=
'my-project'
end
it
'sets the variables'
do
it
'sets the variables'
do
expect
(
subject
.
predefined_variables
).
to
include
(
expect
(
subject
.
predefined_variables
).
to
include
(
{
key:
'KUBE_URL'
,
value:
'https://kube.domain.com'
,
public:
true
},
{
key:
'KUBE_URL'
,
value:
'https://kube.domain.com'
,
public:
true
},
{
key:
'KUBE_TOKEN'
,
value:
'token'
,
public:
false
},
{
key:
'KUBE_TOKEN'
,
value:
'token'
,
public:
false
},
{
key:
'KUBE_NAMESPACE'
,
value:
'my-project'
,
public:
true
},
{
key:
'KUBE_NAMESPACE'
,
value:
namespace
,
public:
true
},
{
key:
'KUBECONFIG'
,
value:
kubeconfig
,
public:
false
},
{
key:
'KUBECONFIG_FILE'
,
value:
kubeconfig
,
public:
false
,
file:
true
},
{
key:
'KUBE_CA_PEM'
,
value:
'CA PEM DATA'
,
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
}
{
key:
'KUBE_CA_PEM_FILE'
,
value:
'CA PEM DATA'
,
public:
true
,
file:
true
}
)
)
end
end
end
end
context
'no namespace provided'
do
context
'namespace is provided'
do
it
'sets the variables'
do
let
(
:namespace
)
{
'my-project'
}
expect
(
subject
.
predefined_variables
).
to
include
(
{
key:
'KUBE_URL'
,
value:
'https://kube.domain.com'
,
public:
true
},
before
do
{
key:
'KUBE_TOKEN'
,
value:
'token'
,
public:
false
},
subject
.
namespace
=
namespace
{
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
it_behaves_like
'setting variables'
end
context
'no namespace provided'
do
let
(
:namespace
)
{
subject
.
actual_namespace
}
it_behaves_like
'setting variables'
it
'sets the KUBE_NAMESPACE'
do
it
'sets the KUBE_NAMESPACE'
do
kube_namespace
=
subject
.
predefined_variables
.
find
{
|
h
|
h
[
:key
]
==
'KUBE_NAMESPACE'
}
kube_namespace
=
subject
.
predefined_variables
.
find
{
|
h
|
h
[
:key
]
==
'KUBE_NAMESPACE'
}
...
...
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