1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Projects::ContainerRepository::ThirdParty::CleanupTagsService, :clean_gitlab_redis_cache, feature_category: :container_registry do
using RSpec::Parameterized::TableSyntax
include_context 'for a cleanup tags service'
let_it_be(:user) { create(:user) }
let_it_be(:project, reload: true) { create(:project, :private) }
let(:repository) { create(:container_repository, :root, project: project) }
let(:service) { described_class.new(container_repository: repository, current_user: user, params: params) }
let(:tags) { %w[latest A Ba Bb C D E] }
before do
project.add_maintainer(user) if user
stub_container_registry_config(enabled: true)
stub_container_registry_tags(
repository: repository.path,
tags: tags
)
stub_tag_digest('latest', 'sha256:configA')
stub_tag_digest('A', 'sha256:configA')
stub_tag_digest('Ba', 'sha256:configB')
stub_tag_digest('Bb', 'sha256:configB')
stub_tag_digest('C', 'sha256:configC')
stub_tag_digest('D', 'sha256:configD')
stub_tag_digest('E', nil)
stub_digest_config('sha256:configA', 1.hour.ago)
stub_digest_config('sha256:configB', 5.days.ago)
stub_digest_config('sha256:configC', 1.month.ago)
stub_digest_config('sha256:configD', nil)
end
describe '#execute' do
subject { service.execute }
it_behaves_like 'when regex matching everything is specified',
delete_expectations: [%w[A Ba Bb C D E]],
service_response_extra: {
before_truncate_size: 6,
after_truncate_size: 6,
before_delete_size: 6,
cached_tags_count: 0
},
supports_caching: true
it_behaves_like 'when regex matching everything is specified and latest is not kept',
delete_expectations: [%w[A Ba Bb C D E latest]],
service_response_extra: {
before_truncate_size: 7,
after_truncate_size: 7,
before_delete_size: 7,
cached_tags_count: 0
},
supports_caching: true
it_behaves_like 'when delete regex matching specific tags is used',
service_response_extra: {
before_truncate_size: 2,
after_truncate_size: 2,
before_delete_size: 2,
cached_tags_count: 0
},
supports_caching: true
it_behaves_like 'when delete regex matching specific tags is used with overriding allow regex',
service_response_extra: {
before_truncate_size: 1,
after_truncate_size: 1,
before_delete_size: 1,
cached_tags_count: 0
},
supports_caching: true
it_behaves_like 'with allow regex value',
delete_expectations: [%w[A C D E]],
service_response_extra: {
before_truncate_size: 4,
after_truncate_size: 4,
before_delete_size: 4,
cached_tags_count: 0
},
supports_caching: true
it_behaves_like 'when keeping only N tags',
delete_expectations: [%w[Bb Ba C]],
service_response_extra: {
before_truncate_size: 4,
after_truncate_size: 4,
before_delete_size: 3,
cached_tags_count: 0
},
supports_caching: true
it_behaves_like 'when not keeping N tags',
delete_expectations: [%w[A Ba Bb C]],
service_response_extra: {
before_truncate_size: 4,
after_truncate_size: 4,
before_delete_size: 4,
cached_tags_count: 0
},
supports_caching: true
it_behaves_like 'when removing keeping only 3',
delete_expectations: [%w[Bb Ba C]],
service_response_extra: {
before_truncate_size: 6,
after_truncate_size: 6,
before_delete_size: 3,
cached_tags_count: 0
},
supports_caching: true
it_behaves_like 'when removing older than 1 day',
delete_expectations: [%w[Ba Bb C]],
service_response_extra: {
before_truncate_size: 6,
after_truncate_size: 6,
before_delete_size: 3,
cached_tags_count: 0
},
supports_caching: true
it_behaves_like 'when combining all parameters',
delete_expectations: [%w[Bb Ba C]],
service_response_extra: {
before_truncate_size: 6,
after_truncate_size: 6,
before_delete_size: 3,
cached_tags_count: 0
},
supports_caching: true
it_behaves_like 'when running a container_expiration_policy',
delete_expectations: [%w[Bb Ba C]],
service_response_extra: {
before_truncate_size: 6,
after_truncate_size: 6,
before_delete_size: 3,
cached_tags_count: 0
},
supports_caching: true
context 'when running a container_expiration_policy with caching' do
let(:user) { nil }
let(:params) do
{
'name_regex_delete' => '.*',
'keep_n' => 1,
'older_than' => '1 day',
'container_expiration_policy' => true
}
end
it 'expects caching to be used' do
expect_delete(%w[Bb Ba C], container_expiration_policy: true)
expect_caching
subject
end
context 'when setting set to false' do
before do
stub_application_setting(container_registry_expiration_policies_caching: false)
end
it 'does not use caching' do
expect_delete(%w[Bb Ba C], container_expiration_policy: true)
expect_no_caching
subject
end
end
end
context 'when truncating the tags list' do
let(:params) do
{
'name_regex_delete' => '.*',
'keep_n' => 1
}
end
shared_examples 'returning the response' do
|status:, original_size:, before_truncate_size:, after_truncate_size:, before_delete_size:|
it 'returns the response' do
expect_no_caching
result = subject
service_response = expected_service_response(
status: status,
original_size: original_size,
deleted: nil
).merge(
before_truncate_size: before_truncate_size,
after_truncate_size: after_truncate_size,
before_delete_size: before_delete_size,
cached_tags_count: 0
)
expect(result).to eq(service_response)
end
end
where(:max_list_size, :delete_tags_service_status, :expected_status, :expected_truncated) do
10 | :success | :success | false
10 | :error | :error | false
3 | :success | :error | true
3 | :error | :error | true
0 | :success | :success | false
0 | :error | :error | false
end
with_them do
before do
stub_application_setting(container_registry_cleanup_tags_service_max_list_size: max_list_size)
allow_next_instance_of(Projects::ContainerRepository::DeleteTagsService) do |service|
allow(service).to receive(:execute).and_return(status: delete_tags_service_status)
end
end
original_size = 7
keep_n = 1
it_behaves_like(
'returning the response',
status: params[:expected_status],
original_size: original_size,
before_truncate_size: original_size - keep_n,
after_truncate_size: params[:expected_truncated] ? params[:max_list_size] + keep_n : original_size - keep_n,
# one tag is filtered out with older_than filter
before_delete_size: params[:expected_truncated] ? params[:max_list_size] : original_size - keep_n - 1
)
end
end
context 'with caching', :freeze_time do
let(:params) do
{
'name_regex_delete' => '.*',
'keep_n' => 1,
'older_than' => '1 day',
'container_expiration_policy' => true
}
end
let(:tags_and_created_ats) do
{
'A' => 1.hour.ago,
'Ba' => 5.days.ago,
'Bb' => 5.days.ago,
'C' => 1.month.ago,
'D' => nil,
'E' => nil
}
end
let(:cacheable_tags) { tags_and_created_ats.reject { |_, value| value.nil? } }
before do
expect_delete(%w[Bb Ba C], container_expiration_policy: true)
# We froze time so we need to set the created_at stubs again
stub_digest_config('sha256:configA', 1.hour.ago)
stub_digest_config('sha256:configB', 5.days.ago)
stub_digest_config('sha256:configC', 1.month.ago)
end
it 'caches the created_at values' do
expect_mget(tags_and_created_ats.keys)
expect_set(cacheable_tags)
expect(subject).to include(cached_tags_count: 0)
end
context 'with cached values' do
before do
::Gitlab::Redis::Cache.with do |redis|
redis.set(cache_key('C'), rfc3339(1.month.ago))
end
end
it 'uses them' do
expect_mget(tags_and_created_ats.keys)
# because C is already in cache, it should not be cached again
expect_set(cacheable_tags.except('C'))
# We will ping the container registry for all tags *except* for C because it's cached
expect(ContainerRegistry::Blob)
.to receive(:new).with(repository, { "digest" => "sha256:configA" }).and_call_original
expect(ContainerRegistry::Blob)
.to receive(:new).with(repository, { "digest" => "sha256:configB" }).twice.and_call_original
expect(ContainerRegistry::Blob).not_to receive(:new).with(repository, { "digest" => "sha256:configC" })
expect(ContainerRegistry::Blob)
.to receive(:new).with(repository, { "digest" => "sha256:configD" }).and_call_original
expect(subject).to include(cached_tags_count: 1)
end
end
def expect_mget(keys)
Gitlab::Redis::Cache.with do |redis|
parameters = keys.map { |k| cache_key(k) }
expect(redis).to receive(:mget).with(parameters).and_call_original
end
end
def expect_set(tags)
selected_tags = tags.map do |tag_name, created_at|
ex = 1.day.seconds - (Time.zone.now - created_at).seconds
[tag_name, created_at, ex.to_i] if ex.positive?
end.compact
return if selected_tags.count.zero?
Gitlab::Redis::Cache.with do |redis|
expect(redis).to receive(:pipelined).and_call_original
times = Gitlab::Redis::ClusterUtil.cluster?(redis) ? 2 : 1
# Set 2 instances as redis is a MultiStore.
# Redis Cluster uses only 1 pipeline as the keys have hash-tags
expect_next_instances_of(Redis::PipelinedConnection, times) do |pipeline|
selected_tags.each do |tag_name, created_at, ex|
expect(pipeline).to receive(:set).with(cache_key(tag_name), rfc3339(created_at), ex: ex).and_call_original
end
end
end
end
def cache_key(tag_name)
"container_repository:{#{repository.id}}:tag:#{tag_name}:created_at"
end
def rfc3339(date_time)
# DateTime rfc3339 is different ActiveSupport::TimeWithZone rfc3339
# The caching will use DateTime rfc3339
DateTime.rfc3339(date_time.rfc3339).rfc3339
end
end
end
private
def stub_tag_digest(tag, digest)
allow(repository.client)
.to receive(:repository_tag_digest)
.with(repository.path, tag) { digest }
allow(repository.client)
.to receive(:repository_manifest)
.with(repository.path, tag) do
{ 'config' => { 'digest' => digest } } if digest
end
end
def stub_digest_config(digest, created_at)
allow(repository.client)
.to receive(:blob)
.with(repository.path, digest, nil) do
{ 'created' => created_at.to_datetime.rfc3339 }.to_json if created_at
end
end
def expect_caching
::Gitlab::Redis::Cache.with do |redis|
expect(redis).to receive(:mget).and_call_original
expect(redis).to receive(:pipelined).and_call_original
times = Gitlab::Redis::ClusterUtil.cluster?(redis) ? 2 : 1
# Set 2 instances as redis is a MultiStore
# Redis Cluster uses only 1 pipeline as the keys have hash-tags
expect_next_instances_of(Redis::PipelinedConnection, times) do |pipeline|
expect(pipeline).to receive(:set).and_call_original
end
end
end
end
|