| 1 |
--- |
| 2 |
- block: |
| 3 |
- set_fact: |
| 4 |
build_ns: "builds" |
| 5 |
build_config: "start-build" |
| 6 |
is_name: "ruby" |
| 7 |
prune_build: "prune-build" |
| 8 |
|
| 9 |
- name: Ensure namespace |
| 10 |
kubernetes.core.k8s: |
| 11 |
kind: Namespace |
| 12 |
name: "{{ build_ns }}" |
| 13 |
|
| 14 |
- name: Create ImageStream |
| 15 |
community.okd.k8s: |
| 16 |
namespace: "{{ build_ns }}" |
| 17 |
definition: |
| 18 |
apiVersion: image.openshift.io/v1 |
| 19 |
kind: ImageStream |
| 20 |
metadata: |
| 21 |
name: "{{ is_name }}" |
| 22 |
spec: |
| 23 |
lookupPolicy: |
| 24 |
local: false |
| 25 |
tags: [] |
| 26 |
|
| 27 |
- name: Create build configuration |
| 28 |
community.okd.k8s: |
| 29 |
namespace: "{{ build_ns }}" |
| 30 |
definition: |
| 31 |
kind: BuildConfig |
| 32 |
apiVersion: build.openshift.io/v1 |
| 33 |
metadata: |
| 34 |
name: "{{ build_config }}" |
| 35 |
spec: |
| 36 |
source: |
| 37 |
dockerfile: | |
| 38 |
FROM openshift/ruby-22-centos7 |
| 39 |
RUN sleep 60s |
| 40 |
USER ansible |
| 41 |
strategy: |
| 42 |
type: Docker |
| 43 |
output: |
| 44 |
to: |
| 45 |
kind: "ImageStreamTag" |
| 46 |
name: "{{ is_name }}:latest" |
| 47 |
|
| 48 |
- name: Start Build from Build configuration |
| 49 |
community.okd.openshift_build: |
| 50 |
namespace: "{{ build_ns }}" |
| 51 |
build_config_name: "{{ build_config }}" |
| 52 |
register: new_build |
| 53 |
|
| 54 |
- name: Assert that a build has been created |
| 55 |
assert: |
| 56 |
that: |
| 57 |
- new_build is changed |
| 58 |
- new_build.builds.0.metadata.name == build_config+"-1" |
| 59 |
|
| 60 |
- name: Start a new Build from previous Build |
| 61 |
community.okd.openshift_build: |
| 62 |
namespace: "{{ build_ns }}" |
| 63 |
build_name: "{{ new_build.builds.0.metadata.name }}" |
| 64 |
register: rerun_build |
| 65 |
|
| 66 |
- name: Assert that another build has been created |
| 67 |
assert: |
| 68 |
that: |
| 69 |
- rerun_build is changed |
| 70 |
- rerun_build.builds.0.metadata.name == build_config+"-2" |
| 71 |
|
| 72 |
- name: Cancel first build created |
| 73 |
community.okd.openshift_build: |
| 74 |
namespace: "{{ build_ns }}" |
| 75 |
build_name: "{{ build_config }}-1" |
| 76 |
state: cancelled |
| 77 |
wait: yes |
| 78 |
register: cancel |
| 79 |
|
| 80 |
- name: Assert that the Build was cancelled |
| 81 |
assert: |
| 82 |
that: |
| 83 |
- cancel is changed |
| 84 |
- cancel.builds | length == 1 |
| 85 |
- cancel.builds.0.metadata.name == build_config+"-1" |
| 86 |
- cancel.builds.0.metadata.namespace == build_ns |
| 87 |
- '"cancelled" in cancel.builds.0.status' |
| 88 |
- cancel.builds.0.status.cancelled |
| 89 |
|
| 90 |
- name: Get info for 1st Build |
| 91 |
kubernetes.core.k8s_info: |
| 92 |
version: build.openshift.io/v1 |
| 93 |
kind: Build |
| 94 |
namespace: "{{ build_ns }}" |
| 95 |
name: "{{ cancel.builds.0.metadata.name }}" |
| 96 |
register: build |
| 97 |
|
| 98 |
- name: Assert that build phase is cancelled |
| 99 |
assert: |
| 100 |
that: |
| 101 |
- build.resources | length == 1 |
| 102 |
- '"cancelled" in build.resources.0.status' |
| 103 |
- build.resources.0.status.cancelled |
| 104 |
- build.resources.0.status.phase == 'Cancelled' |
| 105 |
|
| 106 |
- name: Cancel and restart Build using build config name |
| 107 |
community.okd.openshift_build: |
| 108 |
namespace: "{{ build_ns }}" |
| 109 |
build_config_name: "{{ build_config }}" |
| 110 |
state: restarted |
| 111 |
build_phases: |
| 112 |
- Pending |
| 113 |
- Running |
| 114 |
- New |
| 115 |
register: restart |
| 116 |
|
| 117 |
- name: assert that new build was created |
| 118 |
assert: |
| 119 |
that: |
| 120 |
- restart is changed |
| 121 |
- restart.builds | length == 1 |
| 122 |
- restart.builds.0.metadata.name == build_config+"-3" |
| 123 |
|
| 124 |
- name: Get info for 2nd Build |
| 125 |
kubernetes.core.k8s_info: |
| 126 |
version: build.openshift.io/v1 |
| 127 |
kind: Build |
| 128 |
namespace: "{{ build_ns }}" |
| 129 |
name: "{{ build_config }}-2" |
| 130 |
register: build |
| 131 |
|
| 132 |
- name: Assert that build phase is cancelled |
| 133 |
assert: |
| 134 |
that: |
| 135 |
- build.resources | length == 1 |
| 136 |
- '"cancelled" in build.resources.0.status' |
| 137 |
- build.resources.0.status.cancelled |
| 138 |
- build.resources.0.status.phase == 'Cancelled' |
| 139 |
|
| 140 |
- name: Get info for 3rd build |
| 141 |
kubernetes.core.k8s_info: |
| 142 |
version: build.openshift.io/v1 |
| 143 |
kind: Build |
| 144 |
namespace: "{{ build_ns }}" |
| 145 |
name: "{{ build_config }}-3" |
| 146 |
register: build |
| 147 |
|
| 148 |
- name: Assert that Build is not cancelled |
| 149 |
assert: |
| 150 |
that: |
| 151 |
- build.resources | length == 1 |
| 152 |
- '"cancelled" not in build.resources.0.status' |
| 153 |
- "build.resources.0.status.phase in ('New', 'Pending', 'Running')" |
| 154 |
|
| 155 |
- name: Prune Builds keep younger than 30min |
| 156 |
community.okd.openshift_adm_prune_builds: |
| 157 |
keep_younger_than: 30 |
| 158 |
namespace: "{{ build_ns }}" |
| 159 |
register: prune |
| 160 |
check_mode: yes |
| 161 |
|
| 162 |
- name: Assert that no Builds were found |
| 163 |
assert: |
| 164 |
that: |
| 165 |
- not prune.changed |
| 166 |
- prune.builds | length == 0 |
| 167 |
|
| 168 |
- name: Prune Builds without namespace |
| 169 |
community.okd.openshift_adm_prune_builds: |
| 170 |
register: prune_without_ns |
| 171 |
check_mode: yes |
| 172 |
|
| 173 |
- name: Assert that completed build are candidate for prune |
| 174 |
assert: |
| 175 |
that: |
| 176 |
- prune_without_ns is changed |
| 177 |
- prune_without_ns.builds | length > 0 |
| 178 |
- 'build_config+"-1" in build_names' |
| 179 |
- 'build_config+"-2" in build_names' |
| 180 |
vars: |
| 181 |
build_names: '{{ prune_without_ns.builds | map(attribute="metadata") | flatten | map(attribute="name") | list }}' |
| 182 |
|
| 183 |
- name: Prune Builds using namespace |
| 184 |
community.okd.openshift_adm_prune_builds: |
| 185 |
namespace: "{{ build_ns }}" |
| 186 |
register: prune_with_ns |
| 187 |
check_mode: yes |
| 188 |
|
| 189 |
- name: Assert that prune operation found the completed build |
| 190 |
assert: |
| 191 |
that: |
| 192 |
- prune_with_ns is changed |
| 193 |
- prune_with_ns.builds | length == 2 |
| 194 |
|
| 195 |
- name: Check Build before prune |
| 196 |
kubernetes.core.k8s_info: |
| 197 |
kind: Build |
| 198 |
api_version: build.openshift.io/v1 |
| 199 |
name: "{{ build_config }}-1" |
| 200 |
namespace: "{{ build_ns }}" |
| 201 |
register: resource |
| 202 |
|
| 203 |
- name: Validate that any previous build operation executed with check_mode did not deleted the build |
| 204 |
assert: |
| 205 |
that: |
| 206 |
- resource.resources | length == 1 |
| 207 |
|
| 208 |
- name: Execute prune operation |
| 209 |
community.okd.openshift_adm_prune_builds: |
| 210 |
namespace: "{{ build_ns }}" |
| 211 |
register: prune |
| 212 |
|
| 213 |
- name: assert prune is changed |
| 214 |
assert: |
| 215 |
that: |
| 216 |
- prune is changed |
| 217 |
|
| 218 |
- name: Check Build |
| 219 |
kubernetes.core.k8s_info: |
| 220 |
kind: Build |
| 221 |
api_version: build.openshift.io/v1 |
| 222 |
name: "{{ build_config }}-1" |
| 223 |
namespace: "{{ build_ns }}" |
| 224 |
register: resource |
| 225 |
|
| 226 |
- name: Assert that the Build does not exist anymore |
| 227 |
assert: |
| 228 |
that: |
| 229 |
- resource.resources | length == 0 |
| 230 |
|
| 231 |
- name: Check Build |
| 232 |
kubernetes.core.k8s_info: |
| 233 |
kind: Build |
| 234 |
api_version: build.openshift.io/v1 |
| 235 |
name: "{{ build_config }}-2" |
| 236 |
namespace: "{{ build_ns }}" |
| 237 |
register: resource |
| 238 |
|
| 239 |
- name: Assert that the Build does not exist anymore |
| 240 |
assert: |
| 241 |
that: |
| 242 |
- resource.resources | length == 0 |
| 243 |
|
| 244 |
always: |
| 245 |
- name: Ensure namespace is deleted |
| 246 |
kubernetes.core.k8s: |
| 247 |
state: absent |
| 248 |
kind: Namespace |
| 249 |
name: "{{ build_ns }}" |
| 250 |
ignore_errors: true |