summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/populate_cluster_kubernetes_namespace_table.rb
blob: 35bfc381180078b55990a0bf23f743dd1696b298 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true
#
# rubocop:disable Style/Documentation

module Gitlab
  module BackgroundMigration
    class PopulateClusterKubernetesNamespaceTable
      include Gitlab::Database::MigrationHelpers

      BATCH_SIZE = 1_000

      module Migratable
        class KubernetesNamespace < ActiveRecord::Base
          self.table_name = 'clusters_kubernetes_namespaces'
        end

        class ClusterProject < ActiveRecord::Base
          include EachBatch

          self.table_name = 'cluster_projects'

          belongs_to :project

          def self.with_no_kubernetes_namespace
            where.not(id: Migratable::KubernetesNamespace.select(:cluster_project_id))
          end

          def namespace
            slug = "#{project.path}-#{project.id}".downcase
            slug.gsub(/[^-a-z0-9]/, '-').gsub(/^-+/, '')
          end

          def service_account
            "#{namespace}-service-account"
          end
        end

        class Project < ActiveRecord::Base
          self.table_name = 'projects'
        end
      end

      def perform
        cluster_projects_with_no_kubernetes_namespace.each_batch(of: BATCH_SIZE) do |cluster_projects_batch, index|
          sql_values = sql_values_for(cluster_projects_batch)

          insert_into_cluster_kubernetes_namespace(sql_values)
        end
      end

      private

      def cluster_projects_with_no_kubernetes_namespace
        Migratable::ClusterProject.with_no_kubernetes_namespace
      end

      def sql_values_for(cluster_projects)
        cluster_projects.map do |cluster_project|
          values_for_cluster_project(cluster_project)
        end
      end

      def values_for_cluster_project(cluster_project)
        {
          cluster_project_id: cluster_project.id,
          cluster_id: cluster_project.cluster_id,
          project_id: cluster_project.project_id,
          namespace: cluster_project.namespace,
          service_account_name: cluster_project.service_account,
          created_at: 'NOW()',
          updated_at: 'NOW()'
        }
      end

      def insert_into_cluster_kubernetes_namespace(rows)
        Gitlab::Database.bulk_insert(Migratable::KubernetesNamespace.table_name,
                                     rows,
                                     disable_quote: [:created_at, :updated_at])
      end
    end
  end
end