Commit d10117f9 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '340364-add-agent-connection-event-types' into 'master'

Log an activity event when an Agent connects

See merge request gitlab-org/gitlab!75487
parents 49b40e97 0d50fdfb
......@@ -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
......@@ -33,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
......@@ -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
......@@ -18,7 +18,10 @@ module Clusters
nullify_if_blank :detail
enum kind: {
token_created: 0
token_created: 0,
token_revoked: 1,
agent_connected: 2,
agent_disconnected: 3
}, _prefix: true
enum level: {
......
......@@ -75,4 +75,37 @@ RSpec.describe Clusters::Agent do
expect(agent.has_access_to?(create(:project))).to be_falsey
end
end
describe '#active?' do
let_it_be(:agent) { create(:cluster_agent) }
let!(:token) { create(:cluster_agent_token, agent: agent, last_used_at: last_used_at) }
subject { agent.active? }
context 'agent has never connected' do
let(:last_used_at) { nil }
it { is_expected.to be_falsey }
end
context 'agent has connected, but not recently' do
let(:last_used_at) { 2.hours.ago }
it { is_expected.to be_falsey }
end
context 'agent has connected recently' do
let(:last_used_at) { 2.minutes.ago }
it { is_expected.to be_truthy }
end
context 'agent has multiple tokens' do
let!(:inactive_token) { create(:cluster_agent_token, agent: agent, last_used_at: 2.hours.ago) }
let(:last_used_at) { 2.minutes.ago }
it { is_expected.to be_truthy }
end
end
end
......@@ -39,7 +39,9 @@ RSpec.describe Clusters::AgentToken do
end
describe '#track_usage', :clean_gitlab_redis_cache do
let(:agent_token) { create(:cluster_agent_token) }
let_it_be(:agent) { create(:cluster_agent) }
let(:agent_token) { create(:cluster_agent_token, agent: agent) }
subject { agent_token.track_usage }
......@@ -73,6 +75,34 @@ RSpec.describe Clusters::AgentToken do
expect_redis_update
end
end
context 'agent is inactive' do
before do
allow(agent).to receive(:active?).and_return(false)
end
it 'creates an activity event' do
expect { subject }.to change { agent.activity_events.count }
event = agent.activity_events.last
expect(event).to have_attributes(
kind: 'agent_connected',
level: 'info',
recorded_at: agent_token.reload.read_attribute(:last_used_at),
agent_token: agent_token
)
end
end
context 'agent is active' do
before do
allow(agent).to receive(:active?).and_return(true)
end
it 'does not create an activity event' do
expect { subject }.not_to change { agent.activity_events.count }
end
end
end
def expect_redis_update
......
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