Debian Codegraph @

ansible_collections/community/okd/plugins/modules/openshift_adm_prune_images.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2021, Red Hat
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 # STARTREMOVE (downstream)
12 DOCUMENTATION = r"""
13
14 module: openshift_adm_prune_images
15
16 short_description: Remove unreferenced images
17
18 version_added: "2.2.0"
19
20 author:
21 - Aubin Bikouo (@abikouo)
22
23 description:
24 - This module allow administrators to remove references images.
25 - Note that if the C(namespace) is specified, only references images on Image stream for the corresponding
26 namespace will be candidate for prune if only they are not used or references in another Image stream from
27 another namespace.
28 - Analogous to C(oc adm prune images).
29
30 extends_documentation_fragment:
31 - kubernetes.core.k8s_auth_options
32
33 options:
34 namespace:
35 description:
36 - Use to specify namespace for objects.
37 type: str
38 all_images:
39 description:
40 - Include images that were imported from external registries as candidates for pruning.
41 - If pruned, all the mirrored objects associated with them will also be removed from the integrated registry.
42 type: bool
43 default: True
44 keep_younger_than:
45 description:
46 - Specify the minimum age (in minutes) of an image and its referrers for it to be considered a candidate for pruning.
47 type: int
48 prune_over_size_limit:
49 description:
50 - Specify if images which are exceeding LimitRanges specified in the same namespace,
51 should be considered for pruning.
52 type: bool
53 default: False
54 registry_url:
55 description:
56 - The address to use when contacting the registry, instead of using the default value.
57 - This is useful if you can't resolve or reach the default registry but you do have an
58 alternative route that works.
59 - Particular transport protocol can be enforced using '<scheme>://' prefix.
60 type: str
61 registry_ca_cert:
62 description:
63 - Path to a CA certificate used to contact registry. The full certificate chain must be provided to
64 avoid certificate validation errors.
65 type: path
66 registry_validate_certs:
67 description:
68 - Whether or not to verify the API server's SSL certificates. Can also be specified via K8S_AUTH_VERIFY_SSL
69 environment variable.
70 type: bool
71 prune_registry:
72 description:
73 - If set to I(False), the prune operation will clean up image API objects, but
74 none of the associated content in the registry is removed.
75 type: bool
76 default: True
77 ignore_invalid_refs:
78 description:
79 - If set to I(True), the pruning process will ignore all errors while parsing image references.
80 - This means that the pruning process will ignore the intended connection between the object and the referenced image.
81 - As a result an image may be incorrectly deleted as unused.
82 type: bool
83 default: False
84 requirements:
85 - python >= 3.6
86 - kubernetes >= 12.0.0
87 - docker-image-py
88 """
89
90 EXAMPLES = r"""
91 # Prune if only images and their referrers were more than an hour old
92 - name: Prune image with referrer been more than an hour old
93 community.okd.openshift_adm_prune_images:
94 keep_younger_than: 60
95
96 # Remove images exceeding currently set limit ranges
97 - name: Remove images exceeding currently set limit ranges
98 community.okd.openshift_adm_prune_images:
99 prune_over_size_limit: true
100
101 # Force the insecure http protocol with the particular registry host name
102 - name: Prune images using custom registry
103 community.okd.openshift_adm_prune_images:
104 registry_url: http://registry.example.org
105 registry_validate_certs: false
106 """
107
108
109 RETURN = r"""
110 updated_image_streams:
111 description:
112 - The images streams updated.
113 returned: success
114 type: list
115 elements: dict
116 sample: [
117 {
118 "apiVersion": "image.openshift.io/v1",
119 "kind": "ImageStream",
120 "metadata": {
121 "annotations": {
122 "openshift.io/image.dockerRepositoryCheck": "2021-12-07T07:55:30Z"
123 },
124 "creationTimestamp": "2021-12-07T07:55:30Z",
125 "generation": 1,
126 "name": "python",
127 "namespace": "images",
128 "resourceVersion": "1139215",
129 "uid": "443bad2c-9fd4-4c8f-8a24-3eca4426b07f"
130 },
131 "spec": {
132 "lookupPolicy": {
133 "local": false
134 },
135 "tags": [
136 {
137 "annotations": null,
138 "from": {
139 "kind": "DockerImage",
140 "name": "python:3.8.12"
141 },
142 "generation": 1,
143 "importPolicy": {
144 "insecure": true
145 },
146 "name": "3.8.12",
147 "referencePolicy": {
148 "type": "Source"
149 }
150 }
151 ]
152 },
153 "status": {
154 "dockerImageRepository": "image-registry.openshift-image-registry.svc:5000/images/python",
155 "publicDockerImageRepository": "default-route-openshift-image-registry.apps-crc.testing/images/python",
156 "tags": []
157 }
158 },
159 ...
160 ]
161 deleted_images:
162 description:
163 - The images deleted.
164 returned: success
165 type: list
166 elements: dict
167 sample: [
168 {
169 "apiVersion": "image.openshift.io/v1",
170 "dockerImageLayers": [
171 {
172 "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
173 "name": "sha256:5e0b432e8ba9d9029a000e627840b98ffc1ed0c5172075b7d3e869be0df0fe9b",
174 "size": 54932878
175 },
176 {
177 "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
178 "name": "sha256:a84cfd68b5cea612a8343c346bfa5bd6c486769010d12f7ec86b23c74887feb2",
179 "size": 5153424
180 },
181 {
182 "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
183 "name": "sha256:e8b8f2315954535f1e27cd13d777e73da4a787b0aebf4241d225beff3c91cbb1",
184 "size": 10871995
185 },
186 {
187 "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
188 "name": "sha256:0598fa43a7e793a76c198e8d45d8810394e1cfc943b2673d7fcf5a6fdc4f45b3",
189 "size": 54567844
190 },
191 {
192 "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
193 "name": "sha256:83098237b6d3febc7584c1f16076a32ac01def85b0d220ab46b6ebb2d6e7d4d4",
194 "size": 196499409
195 },
196 {
197 "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
198 "name": "sha256:b92c73d4de9a6a8f6b96806a04857ab33cf6674f6411138603471d744f44ef55",
199 "size": 6290769
200 },
201 {
202 "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
203 "name": "sha256:ef9b6ee59783b84a6ec0c8b109c409411ab7c88fa8c53fb3760b5fde4eb0aa07",
204 "size": 16812698
205 },
206 {
207 "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
208 "name": "sha256:c1f6285e64066d36477a81a48d3c4f1dc3c03dddec9e72d97da13ba51bca0d68",
209 "size": 234
210 },
211 {
212 "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
213 "name": "sha256:a0ee7333301245b50eb700f96d9e13220cdc31871ec9d8e7f0ff7f03a17c6fb3",
214 "size": 2349241
215 }
216 ],
217 "dockerImageManifestMediaType": "application/vnd.docker.distribution.manifest.v2+json",
218 "dockerImageMetadata": {
219 "Architecture": "amd64",
220 "Config": {
221 "Cmd": [
222 "python3"
223 ],
224 "Env": [
225 "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
226 "LANG=C.UTF-8",
227 "GPG_KEY=E3FF2839C048B25C084DEBE9B26995E310250568",
228 "PYTHON_VERSION=3.8.12",
229 "PYTHON_PIP_VERSION=21.2.4",
230 "PYTHON_SETUPTOOLS_VERSION=57.5.0",
231 "PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/3cb8888cc2869620f57d5d2da64da38f516078c7/public/get-pip.py",
232 "PYTHON_GET_PIP_SHA256=c518250e91a70d7b20cceb15272209a4ded2a0c263ae5776f129e0d9b5674309"
233 ],
234 "Image": "sha256:cc3a2931749afa7dede97e32edbbe3e627b275c07bf600ac05bc0dc22ef203de"
235 },
236 "Container": "b43fcf5052feb037f6d204247d51ac8581d45e50f41c6be2410d94b5c3a3453d",
237 "ContainerConfig": {
238 "Cmd": [
239 "/bin/sh",
240 "-c",
241 "#(nop) ",
242 "CMD [\"python3\"]"
243 ],
244 "Env": [
245 "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
246 "LANG=C.UTF-8",
247 "GPG_KEY=E3FF2839C048B25C084DEBE9B26995E310250568",
248 "PYTHON_VERSION=3.8.12",
249 "PYTHON_PIP_VERSION=21.2.4",
250 "PYTHON_SETUPTOOLS_VERSION=57.5.0",
251 "PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/3cb8888cc2869620f57d5d2da64da38f516078c7/public/get-pip.py",
252 "PYTHON_GET_PIP_SHA256=c518250e91a70d7b20cceb15272209a4ded2a0c263ae5776f129e0d9b5674309"
253 ],
254 "Hostname": "b43fcf5052fe",
255 "Image": "sha256:cc3a2931749afa7dede97e32edbbe3e627b275c07bf600ac05bc0dc22ef203de"
256 },
257 "Created": "2021-12-03T01:53:41Z",
258 "DockerVersion": "20.10.7",
259 "Id": "sha256:f746089c9d02d7126bbe829f788e093853a11a7f0421049267a650d52bbcac37",
260 "Size": 347487141,
261 "apiVersion": "image.openshift.io/1.0",
262 "kind": "DockerImage"
263 },
264 "dockerImageMetadataVersion": "1.0",
265 "dockerImageReference": "python@sha256:a874dcabc74ca202b92b826521ff79dede61caca00ceab0b65024e895baceb58",
266 "kind": "Image",
267 "metadata": {
268 "annotations": {
269 "image.openshift.io/dockerLayersOrder": "ascending"
270 },
271 "creationTimestamp": "2021-12-07T07:55:30Z",
272 "name": "sha256:a874dcabc74ca202b92b826521ff79dede61caca00ceab0b65024e895baceb58",
273 "resourceVersion": "1139214",
274 "uid": "33be6ab4-af79-4f44-a0fd-4925bd473c1f"
275 }
276 },
277 ...
278 ]
279 """
280 # ENDREMOVE (downstream)
281
282 import copy
283
284 from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (
285 AUTH_ARG_SPEC,
286 )
287
288
289 def argument_spec():
290 args = copy.deepcopy(AUTH_ARG_SPEC)
291 args.update(
292 dict(
293 namespace=dict(type="str"),
294 all_images=dict(type="bool", default=True),
295 keep_younger_than=dict(type="int"),
296 prune_over_size_limit=dict(type="bool", default=False),
297 registry_url=dict(type="str"),
298 registry_validate_certs=dict(type="bool"),
299 registry_ca_cert=dict(type="path"),
300 prune_registry=dict(type="bool", default=True),
301 ignore_invalid_refs=dict(type="bool", default=False),
302 )
303 )
304 return args
305
306
307 def main():
308 from ansible_collections.community.okd.plugins.module_utils.openshift_adm_prune_images import (
309 OpenShiftAdmPruneImages,
310 )
311
312 module = OpenShiftAdmPruneImages(
313 argument_spec=argument_spec(), supports_check_mode=True
314 )
315 module.run_module()
316
317
318 if __name__ == "__main__":
319 main()