Commit 3836bd28 authored by Hordur Freyr Yngvason's avatar Hordur Freyr Yngvason

Create and write to deployment_clusters table

Creates a table "deployment_clusters" to expand the cluster-related
deployment details from just cluster_id to include kubernetes namespaces
as well, and adds double writes to this table. In the next
release, we can then copy the remaining data over, start reading
exclusively from deployment_clusters, and add an ignore for
deployments.cluster_id

Storing the namespace used is important for accurate deploy boards,
because Environment#deployment_namespace is the namespace for the *next*
deployment, and this can change as cluster configuration changes.
parent d9fdfb24
......@@ -31,6 +31,7 @@ module Clusters
has_many :cluster_projects, class_name: 'Clusters::Project'
has_many :projects, through: :cluster_projects, class_name: '::Project'
has_one :cluster_project, -> { order(id: :desc) }, class_name: 'Clusters::Project'
has_many :deployment_clusters
has_many :cluster_groups, class_name: 'Clusters::Group'
has_many :groups, through: :cluster_groups, class_name: '::Group'
......
......@@ -18,6 +18,8 @@ class Deployment < ApplicationRecord
has_many :merge_requests,
through: :deployment_merge_requests
has_one :deployment_cluster
has_internal_id :iid, scope: :project, track_if: -> { !importing? }, init: ->(s) do
Deployment.where(project: s.project).maximum(:iid) if s&.project
end
......
# frozen_string_literal: true
class DeploymentCluster < ApplicationRecord
belongs_to :deployment, optional: false
belongs_to :cluster, optional: false, class_name: 'Clusters::Cluster'
end
# frozen_string_literal: true
class CreateDeploymentClusters < ActiveRecord::Migration[6.0]
DOWNTIME = false
def change
create_table :deployment_clusters, id: false, force: :cascade do |t|
t.references :deployment, foreign_key: { on_delete: :cascade }, primary_key: true, type: :integer, index: false, default: nil
t.references :cluster, foreign_key: { on_delete: :cascade }, type: :integer, index: false, null: false
t.string :kubernetes_namespace, limit: 255
t.index [:cluster_id, :kubernetes_namespace], name: 'idx_deployment_clusters_on_cluster_id_and_kubernetes_namespace'
t.index [:cluster_id, :deployment_id], unique: true
end
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_02_06_111847) do
ActiveRecord::Schema.define(version: 2020_02_07_151640) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
......@@ -1340,6 +1340,13 @@ ActiveRecord::Schema.define(version: 2020_02_06_111847) do
t.index ["token_encrypted"], name: "index_deploy_tokens_on_token_encrypted", unique: true
end
create_table "deployment_clusters", primary_key: "deployment_id", id: :integer, default: nil, force: :cascade do |t|
t.integer "cluster_id", null: false
t.string "kubernetes_namespace", limit: 255
t.index ["cluster_id", "deployment_id"], name: "index_deployment_clusters_on_cluster_id_and_deployment_id", unique: true
t.index ["cluster_id", "kubernetes_namespace"], name: "idx_deployment_clusters_on_cluster_id_and_kubernetes_namespace"
end
create_table "deployment_merge_requests", id: false, force: :cascade do |t|
t.integer "deployment_id", null: false
t.integer "merge_request_id", null: false
......@@ -3744,7 +3751,7 @@ ActiveRecord::Schema.define(version: 2020_02_06_111847) do
t.string "sso_url", null: false
t.boolean "enforced_sso", default: false, null: false
t.boolean "enforced_group_managed_accounts", default: false, null: false
t.boolean "prohibited_outer_forks", default: false, null: false
t.boolean "prohibited_outer_forks", default: false
t.index ["group_id"], name: "index_saml_providers_on_group_id"
end
......@@ -4659,6 +4666,8 @@ ActiveRecord::Schema.define(version: 2020_02_06_111847) do
add_foreign_key "dependency_proxy_blobs", "namespaces", column: "group_id", on_delete: :cascade
add_foreign_key "dependency_proxy_group_settings", "namespaces", column: "group_id", on_delete: :cascade
add_foreign_key "deploy_keys_projects", "projects", name: "fk_58a901ca7e", on_delete: :cascade
add_foreign_key "deployment_clusters", "clusters", on_delete: :cascade
add_foreign_key "deployment_clusters", "deployments", on_delete: :cascade
add_foreign_key "deployment_merge_requests", "deployments", on_delete: :cascade
add_foreign_key "deployment_merge_requests", "merge_requests", on_delete: :cascade
add_foreign_key "deployments", "clusters", name: "fk_289bba3222", on_delete: :nullify
......
......@@ -24,8 +24,14 @@ module Gitlab
# non-environment job.
return unless deployment.valid? && deployment.environment.persisted?
deployment.cluster_id =
deployment.environment.deployment_platform&.cluster_id
if cluster_id = deployment.environment.deployment_platform&.cluster_id
# double write cluster_id until 12.9: https://gitlab.com/gitlab-org/gitlab/issues/202628
deployment.cluster_id = cluster_id
deployment.deployment_cluster = ::DeploymentCluster.new(
cluster_id: cluster_id,
kubernetes_namespace: deployment.environment.deployment_namespace
)
end
# Allocate IID for deployments.
# This operation must be outside of transactions of pipeline creations.
......
......@@ -33,13 +33,18 @@ describe Gitlab::Ci::Pipeline::Seed::Deployment do
expect(subject.iid).to be_present
expect(subject.environment.name).to eq('production')
expect(subject.cluster).to be_nil
expect(subject.deployment_cluster).to be_nil
end
context 'when environment has deployment platform' do
let!(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
it 'returns a deployment with cluster id' do
expect(subject.cluster).to eq(cluster)
it 'sets the cluster and deployment_cluster' do
expect(subject.cluster).to eq(cluster) # until we stop double writing in 12.9: https://gitlab.com/gitlab-org/gitlab/issues/202628
expect(subject.deployment_cluster).to have_attributes(
cluster_id: cluster.id,
kubernetes_namespace: subject.environment.deployment_namespace
)
end
end
......
......@@ -26,6 +26,7 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do
it { is_expected.to have_one(:application_runner) }
it { is_expected.to have_many(:kubernetes_namespaces) }
it { is_expected.to have_one(:cluster_project) }
it { is_expected.to have_many(:deployment_clusters) }
it { is_expected.to delegate_method(:status).to(:provider) }
it { is_expected.to delegate_method(:status_reason).to(:provider) }
......
# frozen_string_literal: true
require 'spec_helper'
describe DeploymentCluster do
let(:cluster) { create(:cluster) }
let(:deployment) { create(:deployment) }
let(:kubernetes_namespace) { 'an-example-namespace' }
subject { described_class.new(deployment: deployment, cluster: cluster, kubernetes_namespace: kubernetes_namespace) }
it { is_expected.to belong_to(:deployment).required }
it { is_expected.to belong_to(:cluster).required }
it do
is_expected.to have_attributes(
cluster_id: cluster.id,
deployment_id: deployment.id,
kubernetes_namespace: kubernetes_namespace
)
end
end
......@@ -10,6 +10,7 @@ describe Deployment do
it { is_expected.to belong_to(:cluster).class_name('Clusters::Cluster') }
it { is_expected.to belong_to(:user) }
it { is_expected.to belong_to(:deployable) }
it { is_expected.to have_one(:deployment_cluster) }
it { is_expected.to have_many(:deployment_merge_requests) }
it { is_expected.to have_many(:merge_requests).through(:deployment_merge_requests) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment