| 1 |
--- |
| 2 |
- name: Read registry information |
| 3 |
community.okd.openshift_registry_info: |
| 4 |
check: yes |
| 5 |
register: registry |
| 6 |
|
| 7 |
- name: Display registry information |
| 8 |
debug: var=registry |
| 9 |
|
| 10 |
- block: |
| 11 |
- set_fact: |
| 12 |
prune_ns: "prune-images" |
| 13 |
prune_registry: "{{ registry.public_hostname }}" |
| 14 |
container: |
| 15 |
name: "httpd" |
| 16 |
from: "centos/python-38-centos7:20210629-304c7c8" |
| 17 |
pod_name: "test-pod" |
| 18 |
|
| 19 |
- name: Ensure namespace is created |
| 20 |
community.okd.k8s: |
| 21 |
kind: Namespace |
| 22 |
name: "{{ prune_ns }}" |
| 23 |
|
| 24 |
- name: Import image into internal registry |
| 25 |
community.okd.openshift_import_image: |
| 26 |
namespace: "{{ prune_ns }}" |
| 27 |
name: "{{ container.name }}" |
| 28 |
source: "{{ container.from }}" |
| 29 |
|
| 30 |
- name: Create simple Pod |
| 31 |
community.okd.k8s: |
| 32 |
namespace: "{{ prune_ns }}" |
| 33 |
wait: yes |
| 34 |
definition: |
| 35 |
apiVersion: v1 |
| 36 |
kind: Pod |
| 37 |
metadata: |
| 38 |
name: "{{ pod_name }}" |
| 39 |
spec: |
| 40 |
containers: |
| 41 |
- name: test-container |
| 42 |
image: "{{ prune_registry }}/{{ prune_ns }}/{{ container.name }}:latest" |
| 43 |
command: |
| 44 |
- /bin/sh |
| 45 |
- -c |
| 46 |
- while true;do date;sleep 5; done |
| 47 |
|
| 48 |
- name: Create limit range for images size |
| 49 |
community.okd.k8s: |
| 50 |
namespace: "{{ prune_ns }}" |
| 51 |
definition: |
| 52 |
kind: "LimitRange" |
| 53 |
metadata: |
| 54 |
name: "image-resource-limits" |
| 55 |
spec: |
| 56 |
limits: |
| 57 |
- type: openshift.io/Image |
| 58 |
max: |
| 59 |
storage: 1Gi |
| 60 |
|
| 61 |
- name: Prune images from namespace |
| 62 |
community.okd.openshift_adm_prune_images: |
| 63 |
registry_url: "{{ prune_registry }}" |
| 64 |
namespace: "{{ prune_ns }}" |
| 65 |
check_mode: yes |
| 66 |
register: prune |
| 67 |
|
| 68 |
- name: Assert that nothing to prune as image is in used |
| 69 |
assert: |
| 70 |
that: |
| 71 |
- prune is not changed |
| 72 |
- prune is successful |
| 73 |
- prune.deleted_images == [] |
| 74 |
- prune.updated_image_streams == [] |
| 75 |
|
| 76 |
- name: Delete Pod created before |
| 77 |
community.okd.k8s: |
| 78 |
state: absent |
| 79 |
name: "{{ pod_name }}" |
| 80 |
kind: Pod |
| 81 |
namespace: "{{ prune_ns }}" |
| 82 |
wait: yes |
| 83 |
|
| 84 |
- name: Prune images from namespace |
| 85 |
community.okd.openshift_adm_prune_images: |
| 86 |
registry_url: "{{ prune_registry }}" |
| 87 |
namespace: "{{ prune_ns }}" |
| 88 |
check_mode: yes |
| 89 |
register: prune |
| 90 |
|
| 91 |
- name: Read ImageStream |
| 92 |
kubernetes.core.k8s_info: |
| 93 |
version: image.openshift.io/v1 |
| 94 |
kind: ImageStream |
| 95 |
namespace: "{{ prune_ns }}" |
| 96 |
name: "{{ container.name }}" |
| 97 |
register: isinfo |
| 98 |
|
| 99 |
- set_fact: |
| 100 |
is_image_name: "{{ isinfo.resources.0.status.tags[0]['items'].0.image }}" |
| 101 |
|
| 102 |
- name: Assert that corresponding Image and ImageStream were candidate for pruning |
| 103 |
assert: |
| 104 |
that: |
| 105 |
- prune is changed |
| 106 |
- prune.deleted_images | length == 1 |
| 107 |
- prune.deleted_images.0.metadata.name == is_image_name |
| 108 |
- prune.updated_image_streams | length == 1 |
| 109 |
- prune.updated_image_streams.0.metadata.name == container.name |
| 110 |
- prune.updated_image_streams.0.metadata.namespace == prune_ns |
| 111 |
- prune.updated_image_streams.0.status.tags == [] |
| 112 |
|
| 113 |
- name: Prune images from namespace keeping images and referrer younger than 60minutes |
| 114 |
community.okd.openshift_adm_prune_images: |
| 115 |
registry_url: "{{ prune_registry }}" |
| 116 |
namespace: "{{ prune_ns }}" |
| 117 |
keep_younger_than: 60 |
| 118 |
check_mode: yes |
| 119 |
register: younger |
| 120 |
|
| 121 |
- assert: |
| 122 |
that: |
| 123 |
- younger is not changed |
| 124 |
- younger is successful |
| 125 |
- younger.deleted_images == [] |
| 126 |
- younger.updated_image_streams == [] |
| 127 |
|
| 128 |
- name: Prune images over size limit |
| 129 |
community.okd.openshift_adm_prune_images: |
| 130 |
registry_url: "{{ prune_registry }}" |
| 131 |
namespace: "{{ prune_ns }}" |
| 132 |
prune_over_size_limit: yes |
| 133 |
check_mode: yes |
| 134 |
register: prune_over_size |
| 135 |
|
| 136 |
- assert: |
| 137 |
that: |
| 138 |
- prune_over_size is not changed |
| 139 |
- prune_over_size is successful |
| 140 |
- prune_over_size.deleted_images == [] |
| 141 |
- prune_over_size.updated_image_streams == [] |
| 142 |
|
| 143 |
- name: Update limit range for images size |
| 144 |
community.okd.k8s: |
| 145 |
namespace: "{{ prune_ns }}" |
| 146 |
definition: |
| 147 |
kind: "LimitRange" |
| 148 |
metadata: |
| 149 |
name: "image-resource-limits" |
| 150 |
spec: |
| 151 |
limits: |
| 152 |
- type: openshift.io/Image |
| 153 |
max: |
| 154 |
storage: 1Ki |
| 155 |
|
| 156 |
- name: Prune images over size limit (check_mode=yes) |
| 157 |
community.okd.openshift_adm_prune_images: |
| 158 |
registry_url: "{{ prune_registry }}" |
| 159 |
namespace: "{{ prune_ns }}" |
| 160 |
prune_over_size_limit: yes |
| 161 |
check_mode: yes |
| 162 |
register: prune |
| 163 |
|
| 164 |
- name: Assert Images and ImageStream were candidate for prune |
| 165 |
assert: |
| 166 |
that: |
| 167 |
- prune is changed |
| 168 |
- prune.deleted_images | length == 1 |
| 169 |
- prune.deleted_images.0.metadata.name == is_image_name |
| 170 |
- prune.updated_image_streams | length == 1 |
| 171 |
- prune.updated_image_streams.0.metadata.name == container.name |
| 172 |
- prune.updated_image_streams.0.metadata.namespace == prune_ns |
| 173 |
- prune.updated_image_streams.0.status.tags == [] |
| 174 |
|
| 175 |
- name: Prune images over size limit |
| 176 |
community.okd.openshift_adm_prune_images: |
| 177 |
registry_url: "{{ prune_registry }}" |
| 178 |
namespace: "{{ prune_ns }}" |
| 179 |
prune_over_size_limit: yes |
| 180 |
register: prune |
| 181 |
|
| 182 |
- name: Assert that Images and ImageStream were candidate for prune |
| 183 |
assert: |
| 184 |
that: |
| 185 |
- prune is changed |
| 186 |
- prune.deleted_images | length == 1 |
| 187 |
- prune.deleted_images.0.details.name == is_image_name |
| 188 |
- prune.updated_image_streams | length == 1 |
| 189 |
- prune.updated_image_streams.0.metadata.name == container.name |
| 190 |
- prune.updated_image_streams.0.metadata.namespace == prune_ns |
| 191 |
- '"tags" not in prune.updated_image_streams.0.status' |
| 192 |
|
| 193 |
- name: Validate that ImageStream was updated |
| 194 |
kubernetes.core.k8s_info: |
| 195 |
version: image.openshift.io/v1 |
| 196 |
kind: ImageStream |
| 197 |
namespace: "{{ prune_ns }}" |
| 198 |
name: "{{ container.name }}" |
| 199 |
register: stream |
| 200 |
|
| 201 |
- name: Assert that ImageStream was updated |
| 202 |
assert: |
| 203 |
that: |
| 204 |
- stream.resources | length == 1 |
| 205 |
- '"tags" not in stream.resources.0.status' |
| 206 |
|
| 207 |
always: |
| 208 |
- name: Delete namespace |
| 209 |
community.okd.k8s: |
| 210 |
name: "{{ prune_ns }}" |
| 211 |
kind: Namespace |
| 212 |
state: absent |
| 213 |
ignore_errors: true |
| 214 |
|
| 215 |
when: |
| 216 |
- registry.public_hostname |
| 217 |
- registry.check.reached |