Skip to content

Commit 5696cb6

Browse files
authored
Document how to clean container registry and script to prune. (#5670)
1 parent 39bfbb7 commit 5696cb6

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

docs/modules/ROOT/pages/installation/registry/registry.adoc

+61
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,67 @@ This should be within the standard convention adopted by https://docs.docker.com
5656

5757
NOTE: you can configure Camel K to use an insecure private registry. However, your Kubernetes cluster may not be able to https://github.com/apache/camel-k/issues/4720#issuecomment-1708228367[pull images from an insecure registry without proper configuration].
5858

59+
[[pruning-registry]]
60+
== Pruning unused images from container registry
61+
62+
Over time, while building integrations the produced images are stored in the container registry and it may become outdated and may require pruning old unused images.
63+
64+
NOTE: Each container registry vendor can provide unique details about the pruning policy, check your vendor documentation.
65+
66+
NOTE: This is an unsupported functionality, use at your own risk.
67+
68+
It's recommended only to delete container images from container registry if the corresponding `Integration` or `IntegrationKit` doesn't exist anymore or has no expectation to be used. Then if you delete the container image, you should also delete corresponding `Integrationkit` custom resource object.
69+
70+
Camel K materializes the Camel integration in one of the two kubernetes objects: `Deployment` or `CronJob`.
71+
72+
You have to check if the `Integration` is running or scaled down to zero pods, which is the case for CronJobs or Knative deployments.
73+
74+
Then, we can provide some general guide about how to inspect the Camel K objects to prune unused images.
75+
76+
For this guide, we assume you are connected to the container registry with `docker login`.
77+
78+
Step 1: List all Camel K container images, prefixed with `camel-k`
79+
80+
```
81+
$ docker images |grep k-kit
82+
10.98.248.245/camel-k/camel-k-kit-cpth0mtf799b89lheon0 <none> bd52ae6e32af 54 years ago 481MB
83+
10.98.248.245/camel-k/camel-k-kit-cptguntf799b89lheok0 <none> b7f347193b3c 54 years ago 471MB
84+
10.98.248.245/camel-k/camel-k-kit-cptgv0tf799b89lheokg <none> 8d2d963396ca 54 years ago 477MB
85+
10.98.248.245/camel-k/camel-k-kit-cpth0mtf799b89lheomg <none> dc11800ef203 54 years ago 481MB
86+
10.98.248.245/camel-k/camel-k-kit-cptgvd5f799b89lheol0 <none> 0bbdf20f2f49 54 years ago 479MB
87+
```
88+
89+
Step 2: List the container images of the Camel K Integrations (don't print the sha256 digest)
90+
```
91+
$ kubectl get -A it -oyaml|grep 'image:'|sed 's/^\s*image: //g;s/@sha256.*//g'|sort|uniq
92+
10.98.248.245/camel-k/camel-k-kit-cptguntf799b89lheok0
93+
10.98.248.245/camel-k/camel-k-kit-cptgv0tf799b89lheokg
94+
10.98.248.245/camel-k/camel-k-kit-cptgvd5f799b89lheol0
95+
10.98.248.245/camel-k/camel-k-kit-cpth0mtf799b89lheon0
96+
```
97+
98+
Step 3: Compare them and remove the container images and `IntegrationKit` from list 1 not found in list 2
99+
```
100+
docker rmi dc11800ef203
101+
kubectl delete ik/kit-cpth0mtf799b89lheomg
102+
```
103+
104+
There is a https://github.com/apache/camel-k/blob/main/script/prune-camel-k-kit-images.sh[prune-camel-k-kit-images.sh] script to help you in this task. This script requires the following cli tools: `kubectl, comm, docker`.
105+
The script lists the dangling images from the container registry, it accepts two parameters with no arguments: `-v` (verbose) and `-p` (prune images).
106+
107+
An example of an execution:
108+
```
109+
$ prune-camel-k-kit-images.sh -p
110+
> Images from container registry, eligible for pruning.
111+
10.98.248.245/camel-k/camel-k-kit-cpth0mtf799b89lheom0
112+
113+
> Delete Container Images
114+
integrationkit.camel.apache.org "kit-cpth0mtf799b89lheom0" deleted
115+
Untagged: 10.98.248.245/camel-k/camel-k-kit-cpth0mtf799b89lheom0@sha256:3857f8e331e50ded6529641e668de8781eb3cb7b881ea14b89cfc4f6b6e9d455
116+
Deleted: sha256:1015a6b18f164e9b086337e69a98e5850149c158cb778bac6059984756dc0528
117+
Deleted: sha256:2f0d224916e77654c4401f6fc4b1147a9a6e3ccf713213c38e877d7b939bab81
118+
```
119+
59120
[[configuring-registry-list]]
60121
=== Special container registry requirements
61122
We have some hints that can help you configuring on the most common platforms:

script/prune-camel-k-kit-images.sh

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/bin/bash
2+
3+
# Licensed to the Apache Software Foundation (ASF) under one or more
4+
# contributor license agreements. See the NOTICE file distributed with
5+
# this work for additional information regarding copyright ownership.
6+
# The ASF licenses this file to You under the Apache License, Version 2.0
7+
# (the "License"); you may not use this file except in compliance with
8+
# the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
# list and remove dangling camel-k kit images
19+
20+
remove=0
21+
verbose=0
22+
while getopts "pv" opt; do
23+
case "${opt}" in
24+
v)
25+
verbose=1
26+
;;
27+
p)
28+
remove=1
29+
;;
30+
\?)
31+
;;
32+
esac
33+
done
34+
shift $((OPTIND-1))
35+
36+
file_images=$(mktemp)
37+
file_images_from_ints=$(mktemp)
38+
39+
trap "rm -f $file_images $file_images_from_ints" SIGINT SIGTERM ERR EXIT
40+
41+
IFS=''
42+
43+
# images from container registry
44+
docker images |grep k-kit|awk '{print $1}'|sort|uniq > $file_images
45+
46+
if [ $verbose -eq 1 ]; then
47+
echo "> Images from container registry:"
48+
cat $file_images
49+
echo
50+
fi
51+
52+
# images from integrations
53+
kubectl get -A it -oyaml|grep 'image:'|sed 's/^\s*image: //g;s/@sha256.*//g'|sort|uniq > $file_images_from_ints
54+
if [ $verbose -eq 1 ]; then
55+
echo "> Images of Camel K Integrations"
56+
cat $file_images_from_ints
57+
echo
58+
fi
59+
60+
# use comm utility to show only the images with no associated integration
61+
dangling=$(comm -3 $file_images $file_images_from_ints)
62+
if [ -z $dangling ] ; then
63+
echo "> No dangling container images to prune."
64+
else
65+
echo "> Images from container registry, eligible for pruning."
66+
echo $dangling
67+
if [ $remove -eq 1 ] ; then
68+
echo
69+
echo "> Delete Container Images"
70+
echo $dangling|while read imgaddr; do
71+
ns=$(echo $imgaddr|awk -F '/' '{print $2}');
72+
kit=$(echo $imgaddr|awk -F '/' '{print $3}'|sed 's/camel-k-//g');
73+
kubectl -n $ns delete ik/$kit
74+
imgid=$(docker images|grep $imgaddr|awk '{print $3}')
75+
docker rmi $imgid
76+
done
77+
fi
78+
fi

0 commit comments

Comments
 (0)