diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-20 16:37:47 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-20 16:37:47 +0300 |
commit | aee0a117a889461ce8ced6fcf73207fe017f1d99 (patch) | |
tree | 891d9ef189227a8445d83f35c1b0fc99573f4380 /app/models/clusters | |
parent | 8d46af3258650d305f53b819eabf7ab18d22f59e (diff) |
Add latest changes from gitlab-org/gitlab@14-6-stable-eev14.6.0-rc42
Diffstat (limited to 'app/models/clusters')
-rw-r--r-- | app/models/clusters/agent.rb | 8 | ||||
-rw-r--r-- | app/models/clusters/agent_token.rb | 17 | ||||
-rw-r--r-- | app/models/clusters/agents/activity_event.rb | 37 | ||||
-rw-r--r-- | app/models/clusters/applications/runner.rb | 31 | ||||
-rw-r--r-- | app/models/clusters/platforms/kubernetes.rb | 19 |
5 files changed, 74 insertions, 38 deletions
diff --git a/app/models/clusters/agent.rb b/app/models/clusters/agent.rb index cf6d95fc6df..98490a13351 100644 --- a/app/models/clusters/agent.rb +++ b/app/models/clusters/agent.rb @@ -4,6 +4,8 @@ module Clusters class Agent < ApplicationRecord self.table_name = 'cluster_agents' + INACTIVE_AFTER = 1.hour.freeze + belongs_to :created_by_user, class_name: 'User', optional: true belongs_to :project, class_name: '::Project' # Otherwise, it will load ::Clusters::Project @@ -16,6 +18,8 @@ module Clusters has_many :project_authorizations, class_name: 'Clusters::Agents::ProjectAuthorization' has_many :authorized_projects, class_name: '::Project', through: :project_authorizations, source: :project + has_many :activity_events, -> { in_timeline_order }, class_name: 'Clusters::Agents::ActivityEvent', inverse_of: :agent + scope :ordered_by_name, -> { order(:name) } scope :with_name, -> (name) { where(name: name) } @@ -31,5 +35,9 @@ module Clusters def has_access_to?(requested_project) requested_project == project end + + def active? + agent_tokens.where("last_used_at > ?", INACTIVE_AFTER.ago).exists? + end end end diff --git a/app/models/clusters/agent_token.rb b/app/models/clusters/agent_token.rb index 27a3cd8d13d..87dba50cd69 100644 --- a/app/models/clusters/agent_token.rb +++ b/app/models/clusters/agent_token.rb @@ -28,8 +28,12 @@ module Clusters cache_attributes(track_values) - # Use update_column so updated_at is skipped - update_columns(track_values) if can_update_track_values? + if can_update_track_values? + log_activity_event!(track_values[:last_used_at]) unless agent.active? + + # Use update_column so updated_at is skipped + update_columns(track_values) + end end private @@ -44,5 +48,14 @@ module Clusters real_last_used_at.nil? || (Time.current - real_last_used_at) >= last_used_at_max_age end + + def log_activity_event!(recorded_at) + agent.activity_events.create!( + kind: :agent_connected, + level: :info, + recorded_at: recorded_at, + agent_token: self + ) + end end end diff --git a/app/models/clusters/agents/activity_event.rb b/app/models/clusters/agents/activity_event.rb new file mode 100644 index 00000000000..5d9c885c923 --- /dev/null +++ b/app/models/clusters/agents/activity_event.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module Clusters + module Agents + class ActivityEvent < ApplicationRecord + include NullifyIfBlank + + self.table_name = 'agent_activity_events' + + belongs_to :agent, class_name: 'Clusters::Agent', optional: false + belongs_to :user + belongs_to :agent_token, class_name: 'Clusters::AgentToken' + + scope :in_timeline_order, -> { order(recorded_at: :desc, id: :desc) } + + validates :recorded_at, :kind, :level, presence: true + + nullify_if_blank :detail + + enum kind: { + token_created: 0, + token_revoked: 1, + agent_connected: 2, + agent_disconnected: 3 + }, _prefix: true + + enum level: { + debug: 0, + info: 1, + warn: 2, + error: 3, + fatal: 4, + unknown: 5 + }, _prefix: true + end + end +end diff --git a/app/models/clusters/applications/runner.rb b/app/models/clusters/applications/runner.rb index 59a9251d6b7..b57a24dead0 100644 --- a/app/models/clusters/applications/runner.rb +++ b/app/models/clusters/applications/runner.rb @@ -3,7 +3,7 @@ module Clusters module Applications class Runner < ApplicationRecord - VERSION = '0.34.0' + VERSION = '0.35.0' self.table_name = 'clusters_applications_runners' @@ -50,34 +50,6 @@ module Clusters private - def ensure_runner - runner || create_and_assign_runner - end - - def create_and_assign_runner - transaction do - Ci::Runner.create!(runner_create_params).tap do |runner| - update!(runner_id: runner.id) - end - end - end - - def runner_create_params - attributes = { - name: 'kubernetes-cluster', - runner_type: cluster.cluster_type, - tag_list: %w[kubernetes cluster] - } - - if cluster.group_type? - attributes[:runner_namespaces] = [::Ci::RunnerNamespace.new(namespace: group)] - elsif cluster.project_type? - attributes[:runner_projects] = [::Ci::RunnerProject.new(project: project)] - end - - attributes - end - def gitlab_url Gitlab::Routing.url_helpers.root_url(only_path: false) end @@ -85,7 +57,6 @@ module Clusters def specification { "gitlabUrl" => gitlab_url, - "runnerToken" => ensure_runner.token, "runners" => { "privileged" => privileged } } end diff --git a/app/models/clusters/platforms/kubernetes.rb b/app/models/clusters/platforms/kubernetes.rb index 7ec614b048c..1bd8e8b44cb 100644 --- a/app/models/clusters/platforms/kubernetes.rb +++ b/app/models/clusters/platforms/kubernetes.rb @@ -50,12 +50,6 @@ module Clusters alias_attribute :ca_pem, :ca_cert - delegate :enabled?, to: :cluster, allow_nil: true - delegate :provided_by_user?, to: :cluster, allow_nil: true - delegate :allow_user_defined_namespace?, to: :cluster, allow_nil: true - - alias_method :active?, :enabled? - enum_with_nil authorization_type: { unknown_authorization: nil, rbac: 1, @@ -66,6 +60,19 @@ module Clusters nullify_if_blank :namespace + def enabled? + !!cluster&.enabled? + end + alias_method :active?, :enabled? + + def provided_by_user? + !!cluster&.provided_by_user? + end + + def allow_user_defined_namespace? + !!cluster&.allow_user_defined_namespace? + end + def predefined_variables(project:, environment_name:, kubernetes_namespace: nil) Gitlab::Ci::Variables::Collection.new.tap do |variables| variables.append(key: 'KUBE_URL', value: api_url) |