From c80598816c4cb79c25255ecd53906c84fc215a87 Mon Sep 17 00:00:00 2001 From: Shinya Maeda Date: Thu, 4 Jan 2018 23:49:42 +0900 Subject: Opitmize migration process by using both unmanaged_kubernetes_service and kubernetes_service_without_template --- ...rnetes_service_to_new_clusters_architectures.rb | 70 +++++++++++----------- ...s_service_to_new_clusters_architectures_spec.rb | 6 -- 2 files changed, 36 insertions(+), 40 deletions(-) diff --git a/db/post_migrate/20171124104327_migrate_kubernetes_service_to_new_clusters_architectures.rb b/db/post_migrate/20171124104327_migrate_kubernetes_service_to_new_clusters_architectures.rb index e6c7ea58bd2..32f8ef3ff02 100644 --- a/db/post_migrate/20171124104327_migrate_kubernetes_service_to_new_clusters_architectures.rb +++ b/db/post_migrate/20171124104327_migrate_kubernetes_service_to_new_clusters_architectures.rb @@ -54,7 +54,21 @@ class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migrati 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(type: 'KubernetesService') .where(template: false) @@ -78,41 +92,29 @@ class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migrati 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 - MigrateKubernetesServiceToNewClustersArchitectures::Service.kubernetes_service.find_each(batch_size: 1) do |kubernetes_service| - unless managed_by_clusters?(kubernetes_service) - MigrateKubernetesServiceToNewClustersArchitectures::Cluster.create( - enabled: kubernetes_service.active, - user_id: nil, # KubernetesService doesn't have - name: DEFAULT_KUBERNETES_SERVICE_CLUSTER_NAME, - provider_type: MigrateKubernetesServiceToNewClustersArchitectures::Cluster.provider_types[:user], - platform_type: MigrateKubernetesServiceToNewClustersArchitectures::Cluster.platform_types[:kubernetes], - projects: [kubernetes_service.project.becomes(MigrateKubernetesServiceToNewClustersArchitectures::Project)], - environment_scope: find_dedicated_environement_scope(kubernetes_service.project), - platform_kubernetes_attributes: { - api_url: kubernetes_service.api_url, - ca_cert: kubernetes_service.ca_pem, - namespace: kubernetes_service.namespace, - username: nil, # KubernetesService doesn't have - encrypted_password: nil, # KubernetesService doesn't have - 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 - kubernetes_service.properties.merge!( { migrated: true } ) - kubernetes_service.save! + MigrateKubernetesServiceToNewClustersArchitectures::Service + .unmanaged_kubernetes_service.find_each(batch_size: 1) do |kubernetes_service| + MigrateKubernetesServiceToNewClustersArchitectures::Cluster.create( + enabled: kubernetes_service.active, + user_id: nil, # KubernetesService doesn't have + name: DEFAULT_KUBERNETES_SERVICE_CLUSTER_NAME, + provider_type: MigrateKubernetesServiceToNewClustersArchitectures::Cluster.provider_types[:user], + platform_type: MigrateKubernetesServiceToNewClustersArchitectures::Cluster.platform_types[:kubernetes], + projects: [kubernetes_service.project.becomes(MigrateKubernetesServiceToNewClustersArchitectures::Project)], + environment_scope: find_dedicated_environement_scope(kubernetes_service.project), + platform_kubernetes_attributes: { + api_url: kubernetes_service.api_url, + ca_cert: kubernetes_service.ca_pem, + namespace: kubernetes_service.namespace, + username: nil, # KubernetesService doesn't have + encrypted_password: nil, # KubernetesService doesn't have + encrypted_password_iv: nil, # KubernetesService doesn't have + token: kubernetes_service.token # encrypted_token and encrypted_token_iv + } ) end + + MigrateKubernetesServiceToNewClustersArchitectures::Service.kubernetes_service_without_template.update_all(active: false) end def down diff --git a/spec/migrations/migrate_kubernetes_service_to_new_clusters_architectures_spec.rb b/spec/migrations/migrate_kubernetes_service_to_new_clusters_architectures_spec.rb index 5ce1c43d92a..d6643a63f0d 100644 --- a/spec/migrations/migrate_kubernetes_service_to_new_clusters_architectures_spec.rb +++ b/spec/migrations/migrate_kubernetes_service_to_new_clusters_architectures_spec.rb @@ -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.token).to eq(project.kubernetes_service.token) expect(project.kubernetes_service).not_to be_active - expect(project.kubernetes_service.properties['migrated']).to be_truthy 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.token).to eq(project.kubernetes_service.token) expect(project.kubernetes_service).not_to be_active - expect(project.kubernetes_service.properties['migrated']).to be_truthy end end end @@ -110,7 +108,6 @@ describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do kubernetes_service.reload expect(kubernetes_service).not_to be_active - expect(kubernetes_service.properties['migrated']).to be_truthy 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.token).to eq(kubernetes_service.token) expect(kubernetes_service).not_to be_active - expect(kubernetes_service.properties['migrated']).to be_truthy 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.token).to eq(kubernetes_service.token) expect(kubernetes_service).not_to be_active - expect(kubernetes_service.properties['migrated']).to be_truthy 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.token).to eq(kubernetes_service.token) expect(kubernetes_service).not_to be_active - expect(kubernetes_service.properties['migrated']).to be_truthy end end end -- cgit v1.2.1