Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
0d50fdfb
Commit
0d50fdfb
authored
Nov 30, 2021
by
Tiger
Committed by
Bob Van Landuyt
Dec 08, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Log an activity event when an Agent connects
Changelog: added
parent
6ba31f27
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
89 additions
and
4 deletions
+89
-4
app/models/clusters/agent.rb
app/models/clusters/agent.rb
+6
-0
app/models/clusters/agent_token.rb
app/models/clusters/agent_token.rb
+15
-2
app/models/clusters/agents/activity_event.rb
app/models/clusters/agents/activity_event.rb
+4
-1
spec/models/clusters/agent_spec.rb
spec/models/clusters/agent_spec.rb
+33
-0
spec/models/clusters/agent_token_spec.rb
spec/models/clusters/agent_token_spec.rb
+31
-1
No files found.
app/models/clusters/agent.rb
View file @
0d50fdfb
...
@@ -4,6 +4,8 @@ module Clusters
...
@@ -4,6 +4,8 @@ module Clusters
class
Agent
<
ApplicationRecord
class
Agent
<
ApplicationRecord
self
.
table_name
=
'cluster_agents'
self
.
table_name
=
'cluster_agents'
INACTIVE_AFTER
=
1
.
hour
.
freeze
belongs_to
:created_by_user
,
class_name:
'User'
,
optional:
true
belongs_to
:created_by_user
,
class_name:
'User'
,
optional:
true
belongs_to
:project
,
class_name:
'::Project'
# Otherwise, it will load ::Clusters::Project
belongs_to
:project
,
class_name:
'::Project'
# Otherwise, it will load ::Clusters::Project
...
@@ -33,5 +35,9 @@ module Clusters
...
@@ -33,5 +35,9 @@ module Clusters
def
has_access_to?
(
requested_project
)
def
has_access_to?
(
requested_project
)
requested_project
==
project
requested_project
==
project
end
end
def
active?
agent_tokens
.
where
(
"last_used_at > ?"
,
INACTIVE_AFTER
.
ago
).
exists?
end
end
end
end
end
app/models/clusters/agent_token.rb
View file @
0d50fdfb
...
@@ -28,8 +28,12 @@ module Clusters
...
@@ -28,8 +28,12 @@ module Clusters
cache_attributes
(
track_values
)
cache_attributes
(
track_values
)
# Use update_column so updated_at is skipped
if
can_update_track_values?
update_columns
(
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
end
private
private
...
@@ -44,5 +48,14 @@ module Clusters
...
@@ -44,5 +48,14 @@ module Clusters
real_last_used_at
.
nil?
||
real_last_used_at
.
nil?
||
(
Time
.
current
-
real_last_used_at
)
>=
last_used_at_max_age
(
Time
.
current
-
real_last_used_at
)
>=
last_used_at_max_age
end
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
end
end
app/models/clusters/agents/activity_event.rb
View file @
0d50fdfb
...
@@ -18,7 +18,10 @@ module Clusters
...
@@ -18,7 +18,10 @@ module Clusters
nullify_if_blank
:detail
nullify_if_blank
:detail
enum
kind:
{
enum
kind:
{
token_created:
0
token_created:
0
,
token_revoked:
1
,
agent_connected:
2
,
agent_disconnected:
3
},
_prefix:
true
},
_prefix:
true
enum
level:
{
enum
level:
{
...
...
spec/models/clusters/agent_spec.rb
View file @
0d50fdfb
...
@@ -75,4 +75,37 @@ RSpec.describe Clusters::Agent do
...
@@ -75,4 +75,37 @@ RSpec.describe Clusters::Agent do
expect
(
agent
.
has_access_to?
(
create
(
:project
))).
to
be_falsey
expect
(
agent
.
has_access_to?
(
create
(
:project
))).
to
be_falsey
end
end
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
end
spec/models/clusters/agent_token_spec.rb
View file @
0d50fdfb
...
@@ -39,7 +39,9 @@ RSpec.describe Clusters::AgentToken do
...
@@ -39,7 +39,9 @@ RSpec.describe Clusters::AgentToken do
end
end
describe
'#track_usage'
,
:clean_gitlab_redis_cache
do
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
}
subject
{
agent_token
.
track_usage
}
...
@@ -73,6 +75,34 @@ RSpec.describe Clusters::AgentToken do
...
@@ -73,6 +75,34 @@ RSpec.describe Clusters::AgentToken do
expect_redis_update
expect_redis_update
end
end
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
end
def
expect_redis_update
def
expect_redis_update
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment