Migrate project level clusters with no Kubernetes namespace to unmanaged
These clusters were created before we introduced the option to manage your own cluster, and not having a Kubernetes namespace present means that we have tried and failed to create one - and therefore we cannot manage your cluster for you. The 5 minute window should prevent a race condition where a cluster has only just been created and we haven't had a chance to create any resources for it yet.
This commit is contained in:
parent
c6152f3d28
commit
8a8ddec661
3 changed files with 109 additions and 0 deletions
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
title: Migrate GitLab managed project-level clusters to unmanaged if a Kubernetes
|
||||||
|
namespace was unable to be created
|
||||||
|
merge_request: 29251
|
||||||
|
author:
|
||||||
|
type: other
|
|
@ -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 MigrateLegacyManagedClustersToUnmanaged < 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: 'MigrateLegacyManagedClustersToUnmanaged::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: 'MigrateLegacyManagedClustersToUnmanaged::Cluster'
|
||||||
|
end
|
||||||
|
|
||||||
|
def up
|
||||||
|
Cluster.managed
|
||||||
|
.project_type
|
||||||
|
.left_joins(:kubernetes_namespaces)
|
||||||
|
.where(clusters_kubernetes_namespaces: { cluster_id: nil })
|
||||||
|
.where('clusters.created_at < ?', 5.minutes.ago)
|
||||||
|
.each_batch do |batch|
|
||||||
|
batch.update_all(managed: false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,55 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'spec_helper'
|
||||||
|
require Rails.root.join('db', 'post_migrate', '20190606163724_migrate_legacy_managed_clusters_to_unmanaged.rb')
|
||||||
|
|
||||||
|
describe MigrateLegacyManagedClustersToUnmanaged, :migration do
|
||||||
|
let(:cluster_type) { 'project_type' }
|
||||||
|
let(:created_at) { 1.hour.ago }
|
||||||
|
|
||||||
|
let!(:cluster) do
|
||||||
|
table(:clusters).create!(
|
||||||
|
name: 'cluster',
|
||||||
|
cluster_type: described_class::Cluster.cluster_types[cluster_type],
|
||||||
|
managed: true,
|
||||||
|
created_at: created_at
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'marks the cluster as unmanaged' do
|
||||||
|
migrate!
|
||||||
|
expect(cluster.reload).not_to be_managed
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'cluster is not project type' do
|
||||||
|
let(:cluster_type) { 'group_type' }
|
||||||
|
|
||||||
|
it 'does not update the cluster' do
|
||||||
|
migrate!
|
||||||
|
expect(cluster.reload).to be_managed
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'cluster has a kubernetes namespace associated' do
|
||||||
|
before do
|
||||||
|
table(:clusters_kubernetes_namespaces).create!(
|
||||||
|
cluster_id: cluster.id,
|
||||||
|
namespace: 'namespace'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not update the cluster' do
|
||||||
|
migrate!
|
||||||
|
expect(cluster.reload).to be_managed
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'cluster was recently created' do
|
||||||
|
let(:created_at) { 2.minutes.ago }
|
||||||
|
|
||||||
|
it 'does not update the cluster' do
|
||||||
|
migrate!
|
||||||
|
expect(cluster.reload).to be_managed
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue