diff options
author | Tiger <twatson@gitlab.com> | 2019-06-14 03:45:31 +0300 |
---|---|---|
committer | Tiger <twatson@gitlab.com> | 2019-06-21 01:30:26 +0300 |
commit | bae848418b1784be938bb670da4ffceed06f42dd (patch) | |
tree | ba3a2234dabe881760cf851375e9cae9bd9046a5 /db/post_migrate | |
parent | 2c48cb24983314ab78963d2dfb2a74b0f9104fc8 (diff) |
Migrate clusters with no token to unmanaged
There are clusters that have Kubernetes namespaces
stored which are missing a service account token.
These namespaces are unable to be used for deployments,
so marking the clusters as unmanaged will allow the
platform credentials to be used instead.
Diffstat (limited to 'db/post_migrate')
-rw-r--r-- | db/post_migrate/20190613231640_migrate_managed_clusters_with_no_token_to_unmanaged.rb | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/db/post_migrate/20190613231640_migrate_managed_clusters_with_no_token_to_unmanaged.rb b/db/post_migrate/20190613231640_migrate_managed_clusters_with_no_token_to_unmanaged.rb new file mode 100644 index 00000000000..b2914afe2cd --- /dev/null +++ b/db/post_migrate/20190613231640_migrate_managed_clusters_with_no_token_to_unmanaged.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +# See http://doc.gitlab.com/ce/development/migration_style_guide.html +# for more information on how to write migrations for GitLab. + +class MigrateManagedClustersWithNoTokenToUnmanaged < ActiveRecord::Migration[5.1] + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + class Cluster < ActiveRecord::Base + include EachBatch + + self.table_name = 'clusters' + + has_many :kubernetes_namespaces, class_name: 'MigrateManagedClustersWithNoTokenToUnmanaged::KubernetesNamespace' + + scope :managed, -> { where(managed: true) } + + enum cluster_type: { + instance_type: 1, + group_type: 2, + project_type: 3 + } + end + + class KubernetesNamespace < ActiveRecord::Base + self.table_name = 'clusters_kubernetes_namespaces' + + belongs_to :cluster, class_name: 'MigrateManagedClustersWithNoTokenToUnmanaged::Cluster' + end + + def up + Cluster.managed + .project_type + .joins(:kubernetes_namespaces) + .where(clusters_kubernetes_namespaces: { encrypted_service_account_token: nil }) + .where('clusters.created_at < ?', Date.new(2018, 12, 1).midnight) + .each_batch do |batch| + batch.update_all(managed: false) + end + end + + def down + end +end |