Debian Codegraph @

ansible_collections/community/okd/molecule/default/tasks/openshift_adm_prune_deployments.yml

raw
1 ---
2 - name: Prune deployments
3 block:
4 - set_fact:
5 dc_name: "hello"
6 deployment_ns: "prune-deployments"
7 deployment_ns_2: "prune-deployments-2"
8
9 - name: Ensure namespace
10 community.okd.k8s:
11 kind: Namespace
12 name: '{{ deployment_ns }}'
13
14 - name: Create deployment config
15 community.okd.k8s:
16 namespace: '{{ deployment_ns }}'
17 definition:
18 kind: DeploymentConfig
19 apiVersion: apps.openshift.io/v1
20 metadata:
21 name: '{{ dc_name }}'
22 spec:
23 replicas: 1
24 selector:
25 name: '{{ dc_name }}'
26 template:
27 metadata:
28 labels:
29 name: '{{ dc_name }}'
30 spec:
31 containers:
32 - name: hello-openshift
33 imagePullPolicy: IfNotPresent
34 image: python:3.7-alpine
35 command: [ "/bin/sh", "-c", "while true;do date;sleep 2s; done"]
36 wait: yes
37
38 - name: prune deployments (no candidate DeploymentConfig)
39 community.okd.openshift_adm_prune_deployments:
40 namespace: "{{ deployment_ns }}"
41 register: test_prune
42
43 - assert:
44 that:
45 - test_prune is not changed
46 - test_prune.replication_controllers | length == 0
47
48 - name: Update DeploymentConfig - set replicas to 0
49 community.okd.k8s:
50 namespace: "{{ deployment_ns }}"
51 definition:
52 kind: DeploymentConfig
53 apiVersion: "apps.openshift.io/v1"
54 metadata:
55 name: "{{ dc_name }}"
56 spec:
57 replicas: 0
58 selector:
59 name: "{{ dc_name }}"
60 template:
61 metadata:
62 labels:
63 name: "{{ dc_name }}"
64 spec:
65 containers:
66 - name: hello-openshift
67 imagePullPolicy: IfNotPresent
68 image: python:3.7-alpine
69 command: [ "/bin/sh", "-c", "while true;do date;sleep 2s; done"]
70 wait: yes
71
72 - name: Wait for ReplicationController candidate for pruning
73 kubernetes.core.k8s_info:
74 kind: ReplicationController
75 namespace: "{{ deployment_ns }}"
76 register: result
77 retries: 10
78 delay: 30
79 until:
80 - result.resources.0.metadata.annotations["openshift.io/deployment.phase"] in ("Failed", "Complete")
81
82 - name: Prune deployments - should delete 1 ReplicationController
83 community.okd.openshift_adm_prune_deployments:
84 namespace: "{{ deployment_ns }}"
85 check_mode: yes
86 register: test_prune
87
88 - name: Read ReplicationController
89 kubernetes.core.k8s_info:
90 kind: ReplicationController
91 namespace: "{{ deployment_ns }}"
92 register: replications
93
94 - name: Assert that Replication controller was not deleted
95 assert:
96 that:
97 - replications.resources | length == 1
98 - replications.resources.0.metadata.name is match(dc_name+"-*")
99
100 - name: Assure that candidate ReplicationController was found for pruning
101 assert:
102 that:
103 - test_prune is changed
104 - test_prune.replication_controllers | length == 1
105 - test_prune.replication_controllers.0.metadata.name == replications.resources.0.metadata.name
106 - test_prune.replication_controllers.0.metadata.namespace == replications.resources.0.metadata.namespace
107
108 - name: Prune deployments - keep younger than 45min (check_mode)
109 community.okd.openshift_adm_prune_deployments:
110 keep_younger_than: 45
111 namespace: "{{ deployment_ns }}"
112 check_mode: true
113 register: keep_younger
114
115 - name: assert no candidate was found
116 assert:
117 that:
118 - keep_younger is not changed
119 - keep_younger.replication_controllers == []
120
121 - name: Ensure second namespace is created
122 community.okd.k8s:
123 kind: Namespace
124 name: '{{ deployment_ns_2 }}'
125
126 - name: Create deployment config from 2nd namespace
127 community.okd.k8s:
128 namespace: '{{ deployment_ns_2 }}'
129 definition:
130 kind: DeploymentConfig
131 apiVersion: apps.openshift.io/v1
132 metadata:
133 name: '{{ dc_name }}2'
134 spec:
135 replicas: 1
136 selector:
137 name: '{{ dc_name }}2'
138 template:
139 metadata:
140 labels:
141 name: '{{ dc_name }}2'
142 spec:
143 containers:
144 - name: hello-openshift
145 imagePullPolicy: IfNotPresent
146 image: python:3.7-alpine
147 command: [ "/bin/sh", "-c", "while true;do date;sleep 2s; done"]
148 wait: yes
149
150 - name: Stop deployment config - replicas = 0
151 community.okd.k8s:
152 namespace: '{{ deployment_ns_2 }}'
153 definition:
154 kind: DeploymentConfig
155 apiVersion: apps.openshift.io/v1
156 metadata:
157 name: '{{ dc_name }}2'
158 spec:
159 replicas: 0
160 selector:
161 name: '{{ dc_name }}2'
162 template:
163 metadata:
164 labels:
165 name: '{{ dc_name }}2'
166 spec:
167 containers:
168 - name: hello-openshift
169 imagePullPolicy: IfNotPresent
170 image: python:3.7-alpine
171 command: [ "/bin/sh", "-c", "while true;do date;sleep 2s; done"]
172 wait: yes
173
174 - name: Wait for ReplicationController candidate for pruning
175 kubernetes.core.k8s_info:
176 kind: ReplicationController
177 namespace: "{{ deployment_ns_2 }}"
178 register: result
179 retries: 10
180 delay: 30
181 until:
182 - result.resources.0.metadata.annotations["openshift.io/deployment.phase"] in ("Failed", "Complete")
183
184 # Prune from one namespace should not have any effect on others namespaces
185 - name: Prune deployments from 2nd namespace
186 community.okd.openshift_adm_prune_deployments:
187 namespace: "{{ deployment_ns_2 }}"
188 check_mode: yes
189 register: test_prune
190
191 - name: Assure that candidate ReplicationController was found for pruning
192 assert:
193 that:
194 - test_prune is changed
195 - test_prune.replication_controllers | length == 1
196 - "test_prune.replication_controllers.0.metadata.namespace == deployment_ns_2"
197
198 # Prune without namespace option
199 - name: Prune from all namespace should update more deployments
200 community.okd.openshift_adm_prune_deployments:
201 check_mode: yes
202 register: no_namespace_prune
203
204 - name: Assure multiple ReplicationController were found for pruning
205 assert:
206 that:
207 - no_namespace_prune is changed
208 - no_namespace_prune.replication_controllers | length == 2
209
210 # Execute Prune from 2nd namespace
211 - name: Read ReplicationController before Prune operation
212 kubernetes.core.k8s_info:
213 kind: ReplicationController
214 namespace: "{{ deployment_ns_2 }}"
215 register: replications
216
217 - assert:
218 that:
219 - replications.resources | length == 1
220
221 - name: Prune DeploymentConfig from 2nd namespace
222 community.okd.openshift_adm_prune_deployments:
223 namespace: "{{ deployment_ns_2 }}"
224 register: _prune
225
226 - name: Assert DeploymentConfig was deleted
227 assert:
228 that:
229 - _prune is changed
230 - _prune.replication_controllers | length == 1
231 - _prune.replication_controllers.0.details.name == replications.resources.0.metadata.name
232
233 # Execute Prune without namespace option
234 - name: Read ReplicationController before Prune operation
235 kubernetes.core.k8s_info:
236 kind: ReplicationController
237 namespace: "{{ deployment_ns }}"
238 register: replications
239
240 - assert:
241 that:
242 - replications.resources | length == 1
243
244 - name: Prune from all namespace should update more deployments
245 community.okd.openshift_adm_prune_deployments:
246 register: _prune
247
248 - name: Assure multiple ReplicationController were found for pruning
249 assert:
250 that:
251 - _prune is changed
252 - _prune.replication_controllers | length > 0
253
254 always:
255 - name: Delete 1st namespace
256 community.okd.k8s:
257 state: absent
258 kind: Namespace
259 name: "{{ deployment_ns }}"
260 ignore_errors: yes
261 when: deployment_ns is defined
262
263 - name: Delete 2nd namespace
264 community.okd.k8s:
265 state: absent
266 kind: Namespace
267 name: "{{ deployment_ns_2 }}"
268 ignore_errors: yes
269 when: deployment_ns_2 is defined