diff options
author | Shinya Maeda <shinya@gitlab.com> | 2017-11-24 18:31:13 +0900 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2018-01-08 16:22:17 +0900 |
commit | 5b2ca1c66c3a69a1177c0d2f62c208bdce7a81c0 (patch) | |
tree | 413546e2e55a37b8af6aab77b9e597b501e608dd | |
parent | 2c66b942bde756554b22d8b54c586fe45f544b0e (diff) | |
download | gitlab-ce-5b2ca1c66c3a69a1177c0d2f62c208bdce7a81c0.tar.gz |
Migrate existing data from KubernetesService to Clusters::Platforms::Kubernetes
-rw-r--r-- | db/post_migrate/20171124104327_migrate_kubernetes_service_to_new_clusters_architectures.rb | 104 |
1 files changed, 104 insertions, 0 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 new file mode 100644 index 00000000000..dde69058523 --- /dev/null +++ b/db/post_migrate/20171124104327_migrate_kubernetes_service_to_new_clusters_architectures.rb @@ -0,0 +1,104 @@ +class MigrateKubernetesServiceToNewClustersArchitectures < ActiveRecord::Migration + DOWNTIME = false + DEFAULT_KUBERNETES_SERVICE_CLUSTER_NAME = 'KubernetesService' + + class Cluster < ActiveRecord::Base + self.table_name = 'clusters' + + has_many :cluster_projects, class_name: 'ClustersProject' + has_many :projects, through: :cluster_projects, class_name: 'Project' + has_one :provider_gcp, class_name: 'ProvidersGcp' + has_one :platform_kubernetes, class_name: 'PlatformsKubernetes' + + attr_encrypted :token, + mode: :per_attribute_iv, + key: Gitlab::Application.secrets.db_key_base, + algorithm: 'aes-256-cbc' + + accepts_nested_attributes_for :provider_gcp + accepts_nested_attributes_for :platform_kubernetes + + enum platform_type: { + kubernetes: 1 + } + + enum provider_type: { + user: 0, + gcp: 1 + } + end + + class Project < ActiveRecord::Base + self.table_name = 'projects' + + has_one :cluster_project, class_name: 'ClustersProject' + has_one :cluster, through: :cluster_project, class_name: 'Cluster' + end + + class Service < ActiveRecord::Base + include EachBatch + + self.table_name = 'services' + + belongs_to :project, class_name: 'Project' + + # When users create a cluster, KubernetesService is automatically synchronized + # with Platforms::Kubernetes due to delegate Kubernetes specific logic. + # We only target unmanaged KubernetesService records. + scope :unmanaged_kubernetes_service, -> do + joins( + 'INNER JOIN projects ON projects.id = services.project_id' \ + 'INNER JOIN cluster_projects ON projects.id = cluster_projects.project_id' \ + 'INNER JOIN clusters ON cluster_projects.cluster_id = clusters.id' \ + 'INNER JOIN cluster_platforms_kubernetes ON cluster_platforms_kubernetes.cluster_id = clusters.id') + .where( + "services.category = 'deployment' AND services.type = 'KubernetesService'" \ + "AND (" \ + " cluster_projects.project_id IS NULL" \ + " OR" \ + " services.properties NOT LIKE CONCAT('%', cluster_platforms_kubernetes.api_url, '%')" \ + ")") + end + end + + class ClustersProject < ActiveRecord::Base + self.table_name = 'cluster_projects' + + belongs_to :cluster, class_name: 'Cluster' + belongs_to :project, class_name: 'Project' + end + + class ProvidersGcp < ActiveRecord::Base + self.table_name = 'cluster_providers_gcp' + end + + class PlatformsKubernetes < ActiveRecord::Base + self.table_name = 'cluster_platforms_kubernetes' + end + + def up + Service.unmanaged_kubernetes_service + .find_each(batch_size: 1) do |kubernetes_service| + Cluster.create( + enabled: kubernetes_service.active, + user_id: nil, # KubernetesService doesn't have + name: DEFAULT_KUBERNETES_SERVICE_CLUSTER_NAME, + provider_type: Cluster.provider_types[:user], + platform_type: Cluster.platform_types[:kubernetes], + projects: [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 + end + + def down + # noop + end +end |