-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathctre
executable file
·700 lines (586 loc) · 22.3 KB
/
ctre
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#!/usr/bin/env python3
# ctre (part of ossobv/vcutil) // wdoekes/2023-2024 // Public Domain
#
# Enhanced CTR (containerd CLI)
#
# Right now it only does pruning of unused images. This is useful in
# Kubernetes (K8S) environments where stale content can pile up.
#
# Usage:
#
# ctre prune # lists all images to prune
# ctre prune -f # prunes the images
# ctre record-image-starts # daemon process that keeps track of starts
#
# This differs from 'crictl prune' because it records image use first.
# Making sure that we're not removing images that were recently used.
# Recording is done in /var/spool/ctre/image_starts by this application
# run in daemon mode.
#
# BEWARE: stoptime can record the stopping of the _previous_ container after
# starting the next one. This can confuse you into thinking a container is
# started/stopped quickly.
#
from collections import defaultdict, namedtuple
from datetime import datetime
from json import loads
from re import compile
from os import makedirs, rename
from os.path import dirname, basename
from subprocess import PIPE, STDOUT, CalledProcessError, Popen, check_output
from sys import argv, stderr
from time import sleep, time
import warnings
from warnings import warn
KEEP_IF_YOUNGER_THAN = (14 * 86400)
NULL_TIME = '----------'
TABSPLIT_RE = compile(r'\t+')
SIZE_UNITS = {
'B': 1,
'kB': 1024,
'MB': 1024 * 1024,
'GB': 1024 * 1024 * 1024,
' KiB': 1024,
' MiB': 1024 * 1024,
' GiB': 1024 * 1024 * 1024,
}
IMAGE_STARTS_DB = '/var/spool/ctre/image_starts'
Blob = namedtuple('Blob', 'ns digest size age')
Event = namedtuple('Event', 'unixtime ns action data')
Image = namedtuple('Image', 'ns digest ref size age')
class Skip(ValueError):
pass
def split_by_header(lines):
"""
Return lines (with header) as dictionaries
In ["KEY1 KEY2 KEY3", "VAL 1 VAL 2 VAL 3"]
Out [{"KEY1": "VAL 1", "KEY2": "VAL 2", "KEY3": "VAL 3"}]
.. or ..
In ["KEY1<TAB>KEY2<TAB>KEY3", "VAL 1<TAB>VAL 2<TAB>VAL 3"]
Out [{"KEY1": "VAL 1", "KEY2": "VAL 2", "KEY3": "VAL 3"}]
"""
def as_value(value):
if value == '-':
return None
return value
header = lines[0]
parts = header.strip().split()
ret = []
if '\t' in header:
for line in lines[1:]:
data = TABSPLIT_RE.split(line)
assert len(data) == len(parts), (parts, data)
item = {}
for n, part in enumerate(parts):
item[part] = as_value(data[n])
ret.append(item)
else:
sizes = []
last_idx = 0
for next_part in parts[1:]:
idx = header.find(next_part, last_idx)
assert idx > 0, (next_part, idx, header)
assert (
len(header[idx:]) == len(next_part) or
header[idx + len(next_part)] in ' \t'), (
next_part, idx, header)
assert header[idx - 1] in ' \t', (next_part, idx, header)
sizes.append((last_idx, idx))
last_idx = idx
sizes.append((last_idx, -1))
for line in lines[1:]:
item = {}
for part, size in zip(parts, sizes):
item[part] = as_value(line[size[0]:size[1]].rstrip())
ret.append(item)
return ret
def from_size(s):
size = []
for n, ch in enumerate(s):
if ch not in '0123456789.':
size = float(''.join(size))
unit = s[n:]
break
else:
size.append(ch)
return int(SIZE_UNITS[unit] * size)
def from_age(s):
n, tail = s.split(' ', 1)
if n.isdigit():
n = int(n)
if tail == 'years':
return int(n * 86400 * 365.25)
if tail == 'months':
return int(n * 86400 * 30.4375)
if tail == 'weeks':
return (n * 86400 * 7)
if tail == 'days':
return (n * 86400)
if tail == 'hours':
return (n * 3600)
if tail == 'minutes':
return (n * 60)
if tail == 'seconds':
return n
if n == 'About':
if tail == 'an hour':
return 3600
if tail == 'a minute':
return 60
raise NotImplementedError(s)
def namespaces():
out = check_output(['ctr', 'namespace', 'ls', '--quiet']).decode().strip()
return out.split('\n')
def blobs(ns):
def dict_to_blob(ns, d):
assert len(d) == 4, d
return Blob(
ns=ns,
digest=d['DIGEST'],
size=from_size(d['SIZE']),
age=from_age(d['AGE']),
)
out = check_output(['ctr', '-n', ns, 'content', 'ls']).decode().strip()
# DIGEST SIZE AGE LABELS
# sha.. 123K 1 mo ..
items = split_by_header(out.split('\n'))
blobs = [dict_to_blob(ns, item) for item in items]
return dict((blob.digest, blob) for blob in blobs)
def images(ns):
def dict_to_image(ns, item, blob_dict):
blob = blob_dict[item['DIGEST']]
# assert from_size(item['SIZE']) == blob.size, (item['SIZE'], blob)
return Image(
ns=ns,
digest=blob.digest,
ref=item['REF'],
size=from_size(item['SIZE']),
age=blob.age,
)
blob_dict = blobs(ns)
out = check_output(['ctr', '-n', ns, 'image', 'ls']).decode().strip()
# REF TYPE DIGEST SIZE PLATFORMS LABELS
# name sha.. 1 MiB ... ...
items = split_by_header(out.split('\n'))
images = [dict_to_image(ns, item, blob_dict) for item in items]
return images
def images_not_in_use(ns):
out = check_output(['ctr', '-n', ns, 'container', 'ls']).decode().strip()
# CONTAINER IMAGE RUNTIME
# can contain empty images "-" when they are not linked through containerd
# (as seen with moby/dockerd)
items = split_by_header(out.split('\n'))
images_in_use = set([item['IMAGE'] for item in items])
image_list = images(ns)
image_dict = dict((image.ref, image.digest) for image in image_list)
image_digest_in_use = set()
for image_ref in images_in_use:
if image_ref is not None:
try:
image_digest_in_use.add(image_dict[image_ref])
except KeyError:
warn(f'Container ref {image_ref!r} not found in images')
# Order by digest
by_digest = defaultdict(list)
for image in image_list:
by_digest[image.digest].append(image)
# Prepare results
not_in_use = []
for image_digest, images_for_digest in by_digest.items():
assert images_for_digest, images_for_digest
if image_digest in image_digest_in_use:
# For those that _are_ in use, report about those with multiple
# labels. We expect:
# - img:tag
# - img@sha256:abc..
# - sha256:abc..
img_tag = []
img_sha = []
for image in images_for_digest:
if image.ref.startswith('sha256:'):
# assert image.ref == image_digest, (ref, image_digest)
assert len(image.ref) == 71, image.ref
elif '@' in image.ref:
baseref, digest = image.ref.split('@')
assert digest == image_digest, (baseref, image_digest)
assert ':' not in baseref, image.ref
img_sha.append(baseref)
elif ':' in image.ref:
baseref, tag = image.ref.split(':', 1)
img_tag.append(baseref)
else:
assert False, image.ref
# Do not report duplicate tags. But do report duplicate names.
if len(set(img_sha) | set(img_tag)) > 1:
all_refs = ' + '.join(sorted(
image.ref for image in images_for_digest))
image = images_for_digest[0]
warn(
f'Dupe refs found for ns {image.ns} image {image.digest}, '
f'keeping all: {all_refs}')
else:
# For those _not_ in use, add to our response.
not_in_use.extend(images_for_digest)
return not_in_use
def prunable_images(ns):
ret = []
for image in images_not_in_use(ns):
# Check image creation time.
if image.age >= KEEP_IF_YOUNGER_THAN:
ret.append(image)
return ret
def get_all_prunable_images():
images = []
for ns in namespaces():
for image in prunable_images(ns):
images.append((ns, image.ref))
return images
def prunable_images_after_double_check():
prunable = set(get_all_prunable_images())
tnow = time()
tmax = tnow + 15
# Load data collected by ctre record-image-starts daemon
kv = load_image_starts(max_age=KEEP_IF_YOUNGER_THAN)
used_to_start_a_container_recently = set(kv.keys())
prunable -= used_to_start_a_container_recently
while tnow <= tmax and prunable:
left = int(tmax - tnow)
if stderr.isatty():
msg = 'double check to avoid cleaning mid-restart'
stderr.write(f'\x1b[1K\r({msg}... {left} secs)')
stderr.flush()
sleep(1.5)
tnow = time()
prunable_next = set(get_all_prunable_images())
prunable &= prunable_next # only keep those in both lists
if stderr.isatty():
stderr.write('\x1b[1K\r')
stderr.flush()
return list(sorted(prunable))
def remove_images(images):
for ns, ref in images:
ret = check_output(
['ctr', '-n', ns, 'image', 'rm', ref]).decode().strip()
print('(deleted)', ns, ret)
def remove_containers():
# Simply remove all containers. Every container that fails is still
# running. Don't worry about that.
for ns in namespaces():
containers = check_output(
['ctr', '-n', ns, 'containers', 'list', '--quiet']
).decode().strip()
if not containers:
continue
container_objects = []
for container_id in containers.split('\n'):
try:
container_objects.append(_get_container_info(ns, container_id))
except Skip:
pass
except (AttributeError, KeyError) as e:
print('(container info error)', ns, container_id, e)
breakpoint()
container_objects.sort(key=(
lambda x: (x['k8s.ns'], x['k8s.pod'], x['updated'])))
for obj in container_objects:
try:
out = check_output(
['ctr', '-n', ns, 'containers', 'remove', obj['id']],
stderr=STDOUT,
).decode()
except CalledProcessError:
# TODO: Hide this.
print(
'(keeping container)', ns, obj['k8s.ns'], obj['k8s.pod'],
obj['k8s.container'], obj['created'])
else:
# TODO: Maybe not list the timestamps..
print(
'(deleted container)', ns, obj['k8s.ns'], obj['k8s.pod'],
obj['k8s.container'], obj['created'])
def _get_container_info(ns, container_id):
out = check_output(
['ctr', '-n', ns, 'containers', 'info', container_id]
).decode().strip()
json = loads(out)
assert json['ID'] == container_id
if 'Labels' not in json or json['Labels'] is None:
assert json['Image'] == '', (ns, container_id, json['Image'])
print('(no labels in container, skipping)', ns, container_id)
raise Skip
return {
'id': container_id,
'k8s.ns': json['Labels'].get(
'io.kubernetes.pod.namespace', container_id),
'k8s.pod': json['Labels'].get(
'io.kubernetes.pod.name', container_id),
'k8s.container': json['Labels'].get(
'io.kubernetes.container.name', '[{}]'.format(
json['Labels'].get('io.cri-containerd.kind'))),
# TODO: Use timestamps?
'created': json['CreatedAt'],
'updated': json['UpdatedAt'],
}
def prune_content_references():
# Not entirely sure what this does, but it shrinks the content down.
# We'll likely want to do this last.
for ns in namespaces():
before = check_output(
['ctr', '-n', ns, 'content', 'list', '--quiet']
).decode().count('\n')
ret = check_output(
['ctr', '-n', ns, 'content', 'prune', 'references']
).decode().strip()
if ret != '':
print('(unexpected ctr content prune references output)', ns, ret)
after = check_output(
['ctr', '-n', ns, 'content', 'list', '--quiet']
).decode().count('\n')
print('(pruned content)', ns, (before - after))
def container_image_ref(ns, container):
out = (
check_output(['ctr', '-n', ns, 'containers', 'info', container])
.decode('utf-8').strip())
data = loads(out)
return data['Image']
def readlines(fp):
ret = []
while True:
ch = fp.read(1) # or os.read(fp.fileno(), 1)
if not ch:
raise ValueError('got EOF from ctr event')
ret.append(ch)
if ch == b'\n':
yield b''.join(ret)
ret = []
def event_lines(it):
for line in it:
line = line.decode('utf-8', 'replace')
date, time, toff, tzone, ns, action, data = line.split(' ', 6)
time = time[0:15] # strip to microseconds: "%H:%M:%S.%fffff"
when = datetime.strptime(
f'{date} {time} {toff} {tzone}', '%Y-%m-%d %H:%M:%S.%f %z %Z')
unixtime = when.timestamp()
data = loads(data)
yield Event(unixtime=unixtime, ns=ns, action=action, data=data)
def from_kv_old(s):
"""
<starttime> <namespace> <image> # old
<starttime> <image> # oldest
"""
ret = {}
for line in s.split('\n'):
v, k = line.split(' ', 1)
if ' ' in k:
k = tuple(k.split())
ret[k] = v
return ret
def from_kv(s):
"""
<starttime> <stoptime|-> <namespace> <image>
BEWARE: stoptime can record the stopping of the _previous_ container after
starting the next one. This can confuse you into thinking a container is
started/stopped quickly.
"""
s = s.rstrip()
if not s:
return {}
first_line = s.split('\n', 1)[0]
first_line_items = first_line.split(' ', 3)
if len(first_line_items) < 4 or not (
first_line_items[1].isdigit() or
first_line_items[1] == NULL_TIME):
oldret = from_kv_old(s)
ret = dict((k, (int(v), None)) for k, v in oldret.items())
return ret
ret = {}
for line in s.split('\n'):
start, stop, namespace, image = line.split(' ', 3)
k = (namespace, image) # ctr namespace
v = (int(start), None if stop == NULL_TIME else int(stop))
ret[k] = v
return ret
def to_kv(kv):
ret = []
for k, v in sorted(kv.items()):
flatk = ' '.join(k)
starttime, stoptime = v
if stoptime is None:
stoptime = NULL_TIME
ret.append(f'{starttime} {stoptime} {flatk}\n')
return ''.join(ret)
def load_image_starts(max_age):
filename = IMAGE_STARTS_DB
# Load
try:
with open(filename, 'r') as fp:
all_ = fp.read()
except FileNotFoundError:
return {}
kv = from_kv(all_)
# Prune
old = time() - max_age
to_rm = set()
for image_ref, (start, stop) in kv.items():
# Check start/stop time of containers with this image.
if start < old and (stop is None or stop < old):
to_rm.add(image_ref)
for rm in to_rm:
del kv[rm]
return kv
def update_image_startstop(starttime, stoptime, ns, image_ref):
filename = IMAGE_STARTS_DB
temp = f'{filename}.tmp'
# Load
kv = load_image_starts(
max_age=max(KEEP_IF_YOUNGER_THAN, 14 * 86400)) # prune to 14 days
# Add
try:
prev_starttime, prev_stoptime = kv[(ns, image_ref)]
except KeyError:
prev_starttime = prev_stoptime = None
starttime = starttime or prev_starttime or stoptime
stoptime = stoptime or prev_stoptime
kv[(ns, image_ref)] = (starttime, stoptime)
# Save
all_ = to_kv(kv)
try:
with open(temp, 'w') as fp:
fp.write(all_)
except FileNotFoundError:
makedirs(dirname(temp))
with open(temp, 'w') as fp:
fp.write(all_)
rename(temp, filename)
def record_image_starts():
print('Listening for container start events')
with Popen(['ctr', 'event'], stdout=PIPE) as fp:
ignored_events = set()
for event in event_lines(readlines(fp.stdout)):
try:
handle_event(event, ignored_events, debug=False)
except Exception as e:
print(f'ERROR event: {event!r} -- {e}', file=stderr)
raise
def handle_event(event, ignored_events, debug=False):
# Auto-debug stuff during startup: print all.
if event.action not in ignored_events:
# CREATE POD
# - /snapshot/prepare Create snapshot [key]
# - /containers/create Create container (for pod) [id]
# - /containers/update Set container info/config [id]
# - /tasks/create Create task (start pod) [container_id]
# - /tasks/start Start task (start pod) [container_id]
# END POD
# - /tasks/exit Ends container (for pod)
# [id and container_id (same value)]
# - /snapshot/remove Clear snapshot [key]
# - /containers/delete Clear container [id]
#
# CREATE EXEC
# - /tasks/exec-added Create exec [container_id, exec_id]
# - /tasks/exec-started Start exec [container_id, exec_id]
# END EXEC
# - /tasks/exit Ends container (for exec)
# [id and container_id (different value)]
#
# OTHER
# - /images/update Update image info after pod start
# (add 'io.cri-containerd.image' label)
print(
f'D (first visual of event action {event.action!r}) '
f'data: {event.data!r}')
ignored_events.add(event.action)
# In container.io env, we first see a container create event.
if event.action == '/containers/create':
unixtime = int(event.unixtime)
if 'image' in event.data:
container_id = event.data['id']
image_ref = event.data['image']
update_image_startstop(unixtime, None, event.ns, image_ref)
print('C', unixtime, event.ns, image_ref, (
event.data if debug else ''))
else:
print('C', unixtime, event.ns, 'NO_IMAGE_REF', (
event.data if (event.ns != 'moby' or debug) else ''))
# Tasks create/start is how we see PODs getting started, both in
# docker and pure container.io environments.
elif event.action in (
# Typical POD startup on ZFS:
# /snapshot/prepare {"key":"9bd","parent":"sha256:dee"}
# /containers/create {"id":"9bd","image":"k8s.gcr.io/...
# /containers/update {"id":"9bd","image":"k8s.gcr.io/...
# /tasks/create {"container_id":"9bd","bundle":"/run/...
# "rootfs":[{"type":"zfs",
# "source":"local-storage/containerd/43307"}],...
# /tasks/start {"container_id":"9bd","pid":2286326}
# /images/update {'name': 'docker.io/..', 'labels':..
'/tasks/create'):
container_id = event.data['container_id']
unixtime = int(event.unixtime)
image_ref = container_image_ref(event.ns, container_id)
if image_ref:
update_image_startstop(unixtime, None, event.ns, image_ref)
print('T', unixtime, event.ns, image_ref, (
event.data if debug else ''))
else:
print('T', unixtime, event.ns, 'NO_IMAGE_REF', (
event.data if (event.ns != 'moby' or debug) else ''))
# Stop/end tasks
elif event.action == '/tasks/exit' and (
event.data['id'] == event.data['container_id']):
container_id = event.data['container_id']
unixtime = int(event.unixtime)
image_ref = container_image_ref(event.ns, container_id)
if image_ref:
update_image_startstop(None, unixtime, event.ns, image_ref)
print('t', unixtime, event.ns, image_ref, (
event.data if debug else ''))
else:
print('t', unixtime, event.ns, 'NO_IMAGE_REF', (
event.data if (event.ns != 'moby' or debug) else ''))
def formatwarning(message, category, filename, lineno, line=None):
"""
Override default Warning layout, from:
/PATH/TO/ctre:123: UserWarning:
Dupe ref found for image sha256:113...
warn(
To:
ctre:123: UserWarning: Dupe ref found for image sha256:abc...
"""
return '{basename}:{lineno}: {category}: {message}\n'.format(
basename=basename(filename), lineno=lineno,
category=category.__name__, message=message)
warnings.formatwarning = formatwarning # noqa
def main():
if len(argv) in (2, 3) and argv[1] == 'prune':
force = (len(argv) == 3 and argv[2] in ('-f', '--force'))
res = prunable_images_after_double_check()
if res:
if not force:
print('NS', 'REF_TO_PRUNE')
for ns, image in res:
print(ns, image)
yn = ''
while not yn.strip() or yn.strip() not in 'YyNn':
yn = input('Prune (y/n)? ')
if yn.lower() not in 'Yy':
exit(1)
# Remove the curated images.
remove_images(res)
# We could do the following even when !res, but we choose to only
# do them when we appear to have more work anyway:
# - Enumerate and try to remove all containers. Those that are
# running cannot be removed.
remove_containers()
# - Prune the content. Not entirely sure what this does, but it
# does clean up stuff that is unused.
prune_content_references()
elif len(argv) == 2 and argv[1] == 'record-image-starts':
# Listen for events, check for container-add, lookup container image,
# store last-used-time.
record_image_starts()
else:
raise ValueError('please use: ctre prune [-f]')
if __name__ == '__main__':
main()