Opitmize migration process by using both unmanaged_kubernetes_service and kubernetes_service_without_template

This commit is contained in:
Shinya Maeda 2018-01-04 23:49:42 +09:00
parent 665972e2f5
commit c80598816c
2 changed files with 36 additions and 40 deletions

View file

@ -54,7 +54,21 @@ class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migrati
belongs_to :project, class_name: 'MigrateKubernetesServiceToNewClustersArchitectures::Project' belongs_to :project, class_name: 'MigrateKubernetesServiceToNewClustersArchitectures::Project'
scope :kubernetes_service, -> do scope :unmanaged_kubernetes_service, -> do
where(category: 'deployment')
.where(type: 'KubernetesService')
.where(template: false)
.where("NOT EXISTS (?)",
MigrateKubernetesServiceToNewClustersArchitectures::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(project_id: :asc)
end
scope :kubernetes_service_without_template, -> do
where(category: 'deployment') where(category: 'deployment')
.where(type: 'KubernetesService') .where(type: 'KubernetesService')
.where(template: false) .where(template: false)
@ -78,17 +92,9 @@ class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migrati
end end
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 def up
MigrateKubernetesServiceToNewClustersArchitectures::Service.kubernetes_service.find_each(batch_size: 1) do |kubernetes_service| MigrateKubernetesServiceToNewClustersArchitectures::Service
unless managed_by_clusters?(kubernetes_service) .unmanaged_kubernetes_service.find_each(batch_size: 1) do |kubernetes_service|
MigrateKubernetesServiceToNewClustersArchitectures::Cluster.create( MigrateKubernetesServiceToNewClustersArchitectures::Cluster.create(
enabled: kubernetes_service.active, enabled: kubernetes_service.active,
user_id: nil, # KubernetesService doesn't have user_id: nil, # KubernetesService doesn't have
@ -108,11 +114,7 @@ class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migrati
} ) } )
end end
# Disable the KubernetesService. Platforms::Kubernetes will be used from next time. MigrateKubernetesServiceToNewClustersArchitectures::Service.kubernetes_service_without_template.update_all(active: false)
kubernetes_service.active = false
kubernetes_service.properties.merge!( { migrated: true } )
kubernetes_service.save!
end
end end
def down def down

View file

@ -28,7 +28,6 @@ describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
expect(cluster.platform_kubernetes.ca_pem).to eq(project.kubernetes_service.ca_pem) expect(cluster.platform_kubernetes.ca_pem).to eq(project.kubernetes_service.ca_pem)
expect(cluster.platform_kubernetes.token).to eq(project.kubernetes_service.token) expect(cluster.platform_kubernetes.token).to eq(project.kubernetes_service.token)
expect(project.kubernetes_service).not_to be_active expect(project.kubernetes_service).not_to be_active
expect(project.kubernetes_service.properties['migrated']).to be_truthy
end end
end end
end end
@ -85,7 +84,6 @@ describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
expect(cluster.platform_kubernetes.ca_pem).to eq(project.kubernetes_service.ca_pem) expect(cluster.platform_kubernetes.ca_pem).to eq(project.kubernetes_service.ca_pem)
expect(cluster.platform_kubernetes.token).to eq(project.kubernetes_service.token) expect(cluster.platform_kubernetes.token).to eq(project.kubernetes_service.token)
expect(project.kubernetes_service).not_to be_active expect(project.kubernetes_service).not_to be_active
expect(project.kubernetes_service.properties['migrated']).to be_truthy
end end
end end
end end
@ -110,7 +108,6 @@ describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
kubernetes_service.reload kubernetes_service.reload
expect(kubernetes_service).not_to be_active expect(kubernetes_service).not_to be_active
expect(kubernetes_service.properties['migrated']).to be_truthy
end end
end end
@ -129,7 +126,6 @@ describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
expect(cluster.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem) expect(cluster.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem)
expect(cluster.platform_kubernetes.token).to eq(kubernetes_service.token) expect(cluster.platform_kubernetes.token).to eq(kubernetes_service.token)
expect(kubernetes_service).not_to be_active expect(kubernetes_service).not_to be_active
expect(kubernetes_service.properties['migrated']).to be_truthy
end end
end end
end end
@ -149,7 +145,6 @@ describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
expect(cluster.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem) expect(cluster.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem)
expect(cluster.platform_kubernetes.token).to eq(kubernetes_service.token) expect(cluster.platform_kubernetes.token).to eq(kubernetes_service.token)
expect(kubernetes_service).not_to be_active expect(kubernetes_service).not_to be_active
expect(kubernetes_service.properties['migrated']).to be_truthy
end end
end end
end end
@ -170,7 +165,6 @@ describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
expect(cluster.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem) expect(cluster.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem)
expect(cluster.platform_kubernetes.token).to eq(kubernetes_service.token) expect(cluster.platform_kubernetes.token).to eq(kubernetes_service.token)
expect(kubernetes_service).not_to be_active expect(kubernetes_service).not_to be_active
expect(kubernetes_service.properties['migrated']).to be_truthy
end end
end end
end end