Commit e2c3af44 authored by Shinya Maeda's avatar Shinya Maeda

Create has_environment_scope for centerlizing env_scope specific process. Pass...

Create has_environment_scope for centerlizing env_scope specific process. Pass environment_scope to  deployment_platform method.
parent 587d8e13
......@@ -41,7 +41,7 @@ class Projects::BranchesController < Projects::ApplicationController
branch_name = sanitize(strip_tags(params[:branch_name]))
branch_name = Addressable::URI.unescape(branch_name)
redirect_to_autodeploy = project.empty_repo? && project.deployment_platform.present?
redirect_to_autodeploy = project.empty_repo? && project.deployment_platform(environment: ???).present?
result = CreateBranchService.new(project, current_user)
.execute(branch_name, ref)
......
......@@ -26,7 +26,7 @@ module AutoDevopsHelper
def auto_devops_warning_message(project)
missing_domain = !project.auto_devops&.has_domain?
missing_service = !project.deployment_platform&.active?
missing_service = !project.deployment_platform(environment: ???)&.active?
if missing_service
params = {
......
......@@ -377,7 +377,7 @@ module Ci
end
def has_kubernetes_active?
project.deployment_platform&.active?
project.deployment_platform(environment: ???)&.active?
end
def has_stage_seeds?
......
......@@ -3,7 +3,7 @@ module Ci
extend Gitlab::Ci::Model
include HasVariable
include Presentable
prepend EE::Ci::Variable
prepend Ci::HasEnvironmentScope
belongs_to :project
......
module Clusters
class Cluster < ActiveRecord::Base
include Presentable
prepend Ci::HasEnvironmentScope
self.table_name = 'clusters'
......@@ -25,6 +26,7 @@ module Clusters
accepts_nested_attributes_for :provider_gcp, update_only: true
accepts_nested_attributes_for :platform_kubernetes, update_only: true
validates :environment_scope, uniqueness: { scope: :project_id }
validates :name, cluster_name: true
validate :restrict_modification, on: :update
......
......@@ -138,11 +138,11 @@ class Environment < ActiveRecord::Base
end
def has_terminals?
project.deployment_platform.present? && available? && last_deployment.present?
project.deployment_platform(environment: name).present? && available? && last_deployment.present?
end
def terminals
project.deployment_platform.terminals(self) if has_terminals?
project.deployment_platform(environment: name).terminals(self) if has_terminals?
end
def rollout_status
......
......@@ -906,10 +906,7 @@ class Project < ActiveRecord::Base
end
def deployment_platform
@deployment_platform ||= clusters.select do |cluster|
cluster.matches?(environment) # TODO: This is the same logic with Environment Variable
end.first&.platform_kubernetes
@deployment_platform ||= clusters.find_by(enabled: true)&.platform_kubernetes
@deployment_platform ||= services.where(category: :deployment).reorder(nil).find_by(active: true)
end
......@@ -1559,10 +1556,10 @@ class Project < ActiveRecord::Base
ProtectedTag.protected?(self, ref)
end
def deployment_variables
return [] unless deployment_platform
def deployment_variables(environment: nil)
return [] unless deployment_platform(environment: environment)
deployment_platform.predefined_variables
deployment_platform(environment: environment).predefined_variables
end
def auto_devops_variables
......
......@@ -159,7 +159,7 @@
%ul
%li Be careful. Renaming a project's repository can have unintended side effects.
%li You will need to update your local repositories to point to the new location.
- if @project.deployment_platform.present?
- if @project.deployment_platform(environment: ???).present?
%li Your deployment services will be broken, you will need to manually fix the services after renaming.
= f.submit 'Rename project', class: "btn btn-warning"
- if can?(current_user, :change_namespace, @project)
......
......@@ -67,7 +67,7 @@
- if koding_enabled? && @repository.koding_yml.blank?
%li.missing
= link_to _('Set up Koding'), add_koding_stack_path(@project)
- if @repository.gitlab_ci_yml.blank? && @project.deployment_platform.present?
- if @repository.gitlab_ci_yml.blank? && @project.deployment_platform(environment: ???).present?
%li.missing
= link_to add_special_file_path(@project, file_name: '.gitlab-ci.yml', commit_message: 'Set up auto deploy', branch_name: 'auto-deploy', context: 'autodeploy') do
#{ _('Set up auto deploy') }
......
module Ci
module HasEnvironmentScope
extend ActiveSupport::Concern
prepended do
validates(
:environment_scope,
presence: true,
format: { with: ::Gitlab::Regex.environment_scope_regex,
message: ::Gitlab::Regex.environment_scope_regex_message }
)
end
def environment_scope=(new_environment_scope)
super(new_environment_scope.to_s.strip)
end
end
end
......@@ -254,6 +254,16 @@ module EE
end
end
def deployment_platform(environment: nil)
return super unless environment && feature_available?(:multiple_clusters)
@deployment_platform ||= clusters.select do |cluster|
cluster.matches?(environment) # TODO: This is the same logic with Environment Variable
end.first&.platform_kubernetes
@deployment_platform ||= services.where(category: :deployment).reorder(nil).find_by(active: true)
end
def secret_variables_for(ref:, environment: nil)
return super.where(environment_scope: '*') unless
environment && feature_available?(:variable_environment_scope)
......
......@@ -76,7 +76,7 @@ module Gitlab
timeframe_start: timeframe_start,
timeframe_end: timeframe_end,
ci_environment_slug: environment.slug,
kube_namespace: environment.project.deployment_platform&.actual_namespace || '',
kube_namespace: environment.project.deployment_platform(environment: environment.name)&.actual_namespace || '',
environment_filter: %{container_name!="POD",environment="#{environment.slug}"}
}
end
......
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