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
Léo-Paul Géneau
gitlab-ce
Commits
f083739e
Commit
f083739e
authored
Jan 03, 2018
by
Shinya Maeda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add logic to swtich environment_scope by the situation
parent
7eeada80
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
33 deletions
+69
-33
db/post_migrate/20171124104327_migrate_kubernetes_service_to_new_clusters_architectures.rb
...grate_kubernetes_service_to_new_clusters_architectures.rb
+29
-23
spec/migrations/migrate_kubernetes_service_to_new_clusters_architectures_spec.rb
..._kubernetes_service_to_new_clusters_architectures_spec.rb
+40
-10
No files found.
db/post_migrate/20171124104327_migrate_kubernetes_service_to_new_clusters_architectures.rb
View file @
f083739e
...
...
@@ -51,36 +51,41 @@ class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migrati
belongs_to
:project
,
class_name:
'Project'
# 10.1 ~ 10.2
# When users created a cluster, KubernetesService was automatically configured
# by Platforms::Kubernetes parameters.
# Because Platforms::Kubernetes delegated some logic to KubernetesService.
#
# 10.3
# When users create a cluster, KubernetesService is no longer synchronized.
# Because we copied delegated logic in Platforms::Kubernetes.
#
# NOTE:
# - "unmanaged" means "unmanaged by Platforms::Kubernetes(New archetecture)"
# - We only want to migrate records which are not synchronized with Platforms::Kubernetes.
scope
:unmanaged_kubernetes_service
,
->
do
scope
:kubernetes_service
,
->
do
where
(
"services.category = 'deployment'"
)
.
where
(
"services.type = 'KubernetesService'"
)
.
where
(
"services.template = FALSE"
)
.
where
(
"NOT EXISTS (?)"
,
PlatformsKubernetes
.
joins
(
'INNER JOIN projects ON projects.id = services.project_id'
)
.
joins
(
'INNER JOIN cluster_projects ON cluster_projects.project_id = projects.id'
)
.
where
(
'cluster_projects.cluster_id = cluster_platforms_kubernetes.cluster_id'
)
.
where
(
"services.properties LIKE CONCAT('%', cluster_platforms_kubernetes.api_url, '%')"
)
.
select
(
'1'
)
)
.
order
(
'services.project_id'
)
end
end
def
find_dedicated_environement_scope
(
project
)
environment_scopes
=
project
.
clusters
.
map
(
&
:environment_scope
)
return
'*'
if
environment_scopes
.
exclude?
(
'*'
)
# KubernetesService should be added as a default cluster (environment_scope: '*') at first place
return
'migrated/*'
if
environment_scopes
.
exclude?
(
'migrated/*'
)
# If it's conflicted, the KubernetesService added as a migrated cluster
unique_iid
=
0
# If it's still conflicted, finding an unique environment scope incrementaly
while
true
candidate
=
"migrated
#{
unique_iid
}
/*"
return
candidate
if
environment_scopes
.
exclude?
(
candidate
)
unique_iid
+=
1
end
end
# KubernetesService might be already managed by clusters
def
managed_by_clusters?
(
kubernetes_service
)
kubernetes_service
.
project
.
clusters
.
joins
(
'INNER JOIN cluster_platforms_kubernetes ON clusters.id = cluster_platforms_kubernetes.cluster_id'
)
.
where
(
'cluster_platforms_kubernetes.api_url = ?'
,
kubernetes_service
.
api_url
)
.
exists?
end
def
up
Service
.
unmanaged_kubernetes_service
.
find_each
(
batch_size:
1
)
do
|
kubernetes_service
|
Service
.
kubernetes_service
.
find_each
(
batch_size:
1
)
do
|
kubernetes_service
|
unless
managed_by_clusters?
(
kubernetes_service
)
Cluster
.
create
(
enabled:
kubernetes_service
.
active
,
user_id:
nil
,
# KubernetesService doesn't have
...
...
@@ -88,7 +93,7 @@ class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migrati
provider_type:
Cluster
.
provider_types
[
:user
],
platform_type:
Cluster
.
platform_types
[
:kubernetes
],
projects:
[
kubernetes_service
.
project
],
environment_scope:
'*'
,
# KubernetesService is considered as a default cluster
environment_scope:
find_dedicated_environement_scope
(
kubernetes_service
.
project
),
platform_kubernetes_attributes:
{
api_url:
kubernetes_service
.
api_url
,
ca_cert:
kubernetes_service
.
ca_pem
,
...
...
@@ -98,6 +103,7 @@ class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migrati
encrypted_password_iv:
nil
,
# KubernetesService doesn't have
token:
kubernetes_service
.
token
# encrypted_token and encrypted_token_iv
}
)
end
# Disable the KubernetesService. Platforms::Kubernetes will be used from next time.
kubernetes_service
.
active
=
false
...
...
spec/migrations/migrate_kubernetes_service_to_new_clusters_architectures_spec.rb
View file @
f083739e
...
...
@@ -91,7 +91,7 @@ describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
end
end
context
'when
sync
ed KubernetesService exists'
do
context
'when
manag
ed KubernetesService exists'
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:cluster
)
{
create
(
:cluster
,
:provided_by_gcp
,
projects:
[
project
])
}
let!
(
:platform_kubernetes
)
{
cluster
.
platform_kubernetes
}
...
...
@@ -105,15 +105,16 @@ describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
ca_pem:
platform_kubernetes
.
ca_cert
)
end
it
'does not migrate the KubernetesService'
do
# Because the corresponding Platform::Kubernetes already exists
it
'does not migrate the KubernetesService
and disables the kubernetes_service
'
do
# Because the corresponding Platform::Kubernetes already exists
expect
{
migrate!
}.
not_to
change
{
Clusters
::
Cluster
.
count
}
expect
(
kubernetes_service
).
to
be_active
expect
(
kubernetes_service
.
properties
[
'migrated'
]).
to
be_falsy
kubernetes_service
.
reload
expect
(
kubernetes_service
).
not_to
be_active
expect
(
kubernetes_service
.
properties
[
'migrated'
]).
to
be_truthy
end
end
context
'when production cluster has already been exist
sed'
do
context
'when production cluster has already been exist
ed'
do
# i.e. There are no environment_scope conflicts
let
(
:project
)
{
create
(
:project
)
}
let!
(
:cluster
)
{
create
(
:cluster
,
:provided_by_gcp
,
environment_scope:
'production/*'
,
projects:
[
project
])
}
let!
(
:kubernetes_service
)
{
create
(
:kubernetes_service
,
api_url:
'https://debug.kube.com'
,
active:
true
,
project:
project
)
}
...
...
@@ -123,6 +124,7 @@ describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
kubernetes_service
.
reload
project
.
clusters
.
last
.
tap
do
|
cluster
|
expect
(
cluster
.
environment_scope
).
to
eq
(
'*'
)
expect
(
cluster
.
platform_kubernetes
.
api_url
).
to
eq
(
kubernetes_service
.
api_url
)
expect
(
cluster
.
platform_kubernetes
.
ca_pem
).
to
eq
(
kubernetes_service
.
ca_pem
)
expect
(
cluster
.
platform_kubernetes
.
token
).
to
eq
(
kubernetes_service
.
token
)
...
...
@@ -132,16 +134,44 @@ describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
end
end
context
'when default cluster has already been exist
s
ed'
do
context
'when default cluster has already been existed'
do
let
(
:project
)
{
create
(
:project
)
}
let!
(
:cluster
)
{
create
(
:cluster
,
:provided_by_gcp
,
environment_scope:
'*'
,
projects:
[
project
])
}
let!
(
:kubernetes_service
)
{
create
(
:kubernetes_service
,
api_url:
'https://debug.kube.com'
,
active:
true
,
project:
project
)
}
it
'does not migrate the KubernetesService'
do
# Because environment_scope is duplicated
expect
{
migrate!
}.
not_to
change
{
Clusters
::
Cluster
.
count
}
it
'migrates the KubernetesService to Platform::Kubernetes with dedicated environment_scope'
do
# Because environment_scope is duplicated
expect
{
migrate!
}.
to
change
{
Clusters
::
Cluster
.
count
}.
by
(
1
)
kubernetes_service
.
reload
project
.
clusters
.
last
.
tap
do
|
cluster
|
expect
(
cluster
.
environment_scope
).
to
eq
(
'migrated/*'
)
expect
(
cluster
.
platform_kubernetes
.
api_url
).
to
eq
(
kubernetes_service
.
api_url
)
expect
(
cluster
.
platform_kubernetes
.
ca_pem
).
to
eq
(
kubernetes_service
.
ca_pem
)
expect
(
cluster
.
platform_kubernetes
.
token
).
to
eq
(
kubernetes_service
.
token
)
expect
(
kubernetes_service
).
not_to
be_active
expect
(
kubernetes_service
.
properties
[
'migrated'
]).
to
be_truthy
end
end
end
context
'when default cluster and migrated cluster has already been existed'
do
let
(
:project
)
{
create
(
:project
)
}
let!
(
:cluster
)
{
create
(
:cluster
,
:provided_by_gcp
,
environment_scope:
'*'
,
projects:
[
project
])
}
let!
(
:migrated_cluster
)
{
create
(
:cluster
,
:provided_by_gcp
,
environment_scope:
'migrated/*'
,
projects:
[
project
])
}
let!
(
:kubernetes_service
)
{
create
(
:kubernetes_service
,
api_url:
'https://debug.kube.com'
,
active:
true
,
project:
project
)
}
it
'migrates the KubernetesService to Platform::Kubernetes with dedicated environment_scope'
do
# Because environment_scope is duplicated
expect
{
migrate!
}.
to
change
{
Clusters
::
Cluster
.
count
}.
by
(
1
)
expect
(
kubernetes_service
).
to
be_active
expect
(
kubernetes_service
.
properties
[
'migrated'
]).
to
be_falsy
kubernetes_service
.
reload
project
.
clusters
.
last
.
tap
do
|
cluster
|
expect
(
cluster
.
environment_scope
).
to
eq
(
'migrated0/*'
)
expect
(
cluster
.
platform_kubernetes
.
api_url
).
to
eq
(
kubernetes_service
.
api_url
)
expect
(
cluster
.
platform_kubernetes
.
ca_pem
).
to
eq
(
kubernetes_service
.
ca_pem
)
expect
(
cluster
.
platform_kubernetes
.
token
).
to
eq
(
kubernetes_service
.
token
)
expect
(
kubernetes_service
).
not_to
be_active
expect
(
kubernetes_service
.
properties
[
'migrated'
]).
to
be_truthy
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