main.yml 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. ---
  2. # Ensures all the operator artifacts are created and waits for CSV to succeed.
  3. #
  4. # The following variables must exist:
  5. #
  6. # op:
  7. # desired_csv
  8. # op_nsp
  9. # op_cat
  10. # op_pkg
  11. # op_chn
  12. # op_mod
  13. #
  14. # NOTE: Do NOT test by checking for presence of API resources - they do not always get cleaned up.
  15. - debug:
  16. msg: Deploying operator {{ op.op_pkg }} in namespace {{ op.op_nsp }}...
  17. # See if the CSV is available already (anywhere), meaning the operator is already installed.
  18. - name: Check if the CSV exists already
  19. k8s_info:
  20. kubeconfig: tmp/kubeconfig-ocp4
  21. validate_certs: no
  22. api_version: operators.coreos.com/v1alpha1
  23. kind: clusterserviceversion
  24. register: all_csv
  25. - name: Find the wanted CSV among all CSVs
  26. set_fact:
  27. found_csv: '{{ (all_csv | community.general.json_query("resources[?contains(metadata.name, `" + op.op_pkg + ".`)]")) }}'
  28. when:
  29. - all_csv.resources is defined
  30. - (all_csv.resources | length) > 0
  31. - name: Get details about the CSV if found
  32. set_fact:
  33. csv_ns: "{{ found_csv[0] | community.general.json_query('metadata.namespace') }}"
  34. csv_name: "{{ found_csv[0] | community.general.json_query('metadata.name') }}"
  35. when:
  36. - found_csv is defined
  37. - (found_csv | length) > 0
  38. - debug:
  39. msg: Operator {{ op.op_pkg }} is already installed in {{ csv_ns }} as {{ csv_name }} (desired_csv == {{ op.desired_csv }}).
  40. when:
  41. - csv_ns is defined
  42. - csv_name is defined
  43. - name: Only proceed here if the CSV was not found anywhere yet
  44. block:
  45. # Check if package provides desired_csv in any of the channels
  46. # XXX Due to the idiotic fact that some operators from different catalogs
  47. # have the same manifest name, this needs to be a two-phase query.
  48. # XXX First, query for all operators from the desired catalog.
  49. - name: Get packagemanifests from same catalog
  50. k8s_info:
  51. kubeconfig: tmp/kubeconfig-ocp4
  52. validate_certs: no
  53. api_version: packages.operators.coreos.com/v1
  54. kind: packagemanifest
  55. namespace: openshift-marketplace
  56. label_selectors:
  57. - "catalog = {{ op.op_cat }}"
  58. register: pkg_mft_samecat
  59. # XXX Then, search for the operator that has the status.packageName we need.
  60. - name: Weed out the manifest we need
  61. set_fact:
  62. pkg_mft: '{{ pkg_mft_samecat | community.general.json_query("resources[?status.packageName == `" + op.op_pkg + "`]") }}'
  63. - debug: var=pkg_mft
  64. - name: Search for any channels that provide desired_csv
  65. set_fact:
  66. found_chn: '{{ (pkg_mft | community.general.json_query("resources[0].status.channels[?currentCSV == `" + op.desired_csv + "`].name")) }}'
  67. when:
  68. - pkg_mft is defined
  69. - pkg_mft.resources | length > 0
  70. - name: Fail if no channel provides the desired_csv
  71. fail:
  72. msg: No {{ op.op_pkg }} channel provides {{ op.desired_csv }}
  73. when:
  74. - found_chn is defined
  75. - found_chn | length == 0
  76. - name: Fail if selected channel does not provide the desired_csv
  77. fail:
  78. msg: Operator {{ op.op_pkg }} selected channel {{ op.op_chn }} does not provide {{ op.desired_csv }} - {{ found_chn }}
  79. when:
  80. - found_chn is defined
  81. - found_chn | length > 0
  82. - op.op_chn not in found_chn
  83. # Create the namespace if necessary
  84. - name: Make sure the namespace is there
  85. k8s:
  86. kubeconfig: tmp/kubeconfig-ocp4
  87. validate_certs: no
  88. api_version: v1
  89. kind: namespace
  90. name: "{{ op.op_nsp }}"
  91. # Check for operator group - if existing, make sure it's compatible; otherwise create it
  92. - name: See if there are any OperatorGroups
  93. k8s_info:
  94. kubeconfig: tmp/kubeconfig-ocp4
  95. validate_certs: no
  96. api_version: operators.coreos.com/v1
  97. kind: operatorgroup
  98. namespace: "{{ op.op_nsp }}"
  99. register: found_opgrp
  100. - name: Die if more than one OG
  101. ansible.builtin.fail:
  102. msg: More than one OperatorGroup found in project.
  103. when: (found_opgrp.resources | length) > 1
  104. - name: Die if op_mod is not recognised
  105. ansible.builtin.fail:
  106. msg: Unrecognised op.op_mod ({{ op.op_mod }})
  107. when: op.op_mod not in ["all", "self"]
  108. - name: Die if op_mod does not match found OG mode
  109. ansible.builtin.fail:
  110. msg: Found OG mode does not match required ({{ op.op_mod }})
  111. when: |
  112. ((found_opgrp.resources | length) == 1 and op.op_mod == "all" and found_opgrp.resources[0].status.namespaces != [""]) or
  113. ((found_opgrp.resources | length) == 1 and op.op_mod == "self" and found_opgrp.resources[0].status.namespaces != [op.op_nsp])
  114. - name: Set fact for namespaces of OG if we require all
  115. set_fact:
  116. og_spec: []
  117. when:
  118. - op.op_mod == "all"
  119. - (found_opgrp.resources | length) == 0
  120. - name: Set fact for namespaces of OG if we require self
  121. set_fact:
  122. og_spec: ["{{op.op_nsp}}"]
  123. when:
  124. - op.op_mod == "self"
  125. - (found_opgrp.resources | length) == 0
  126. - name: Create the OperatorGroup if not there
  127. k8s:
  128. kubeconfig: tmp/kubeconfig-ocp4
  129. validate_certs: no
  130. api_version: operators.coreos.com/v1
  131. kind: operatorgroup
  132. namespace: "{{ op.op_nsp }}"
  133. name: "{{ op.op_nsp }}"
  134. definition:
  135. spec:
  136. targetNamespaces: "{{ og_spec }}"
  137. when: (found_opgrp.resources | length) == 0
  138. # create a subscription if not there, and wait for the CSV
  139. - name: Also make sure there is a subscription
  140. k8s:
  141. kubeconfig: tmp/kubeconfig-ocp4
  142. validate_certs: no
  143. api_version: operators.coreos.com/v1alpha1
  144. kind: subscription
  145. namespace: "{{ op.op_nsp }}"
  146. name: "{{ op.op_pkg }}"
  147. definition:
  148. spec:
  149. source: "{{ op.op_cat }}"
  150. sourceNamespace: openshift-marketplace
  151. name: "{{ op.op_pkg }}"
  152. channel: "{{ op.op_chn }}"
  153. installPlanApproval: Automatic
  154. # TODO: Finish this at some point.
  155. #- name: Wait for installPlan to show up
  156. # k8s_info:
  157. # kubeconfig: tmp/kubeconfig-ocp4
  158. # validate_certs: no
  159. # api_version: operators.coreos.com/v1alpha1
  160. # kind: installplan
  161. # namespace: "{{ op_nsp }}"
  162. # register: installplan
  163. # until:
  164. # - installplan.resources is defined
  165. # - (installplan.resources | length) > 0
  166. # - installplan.resources[0].spec.approved
  167. # retries: 12
  168. # delay: 10
  169. - name: Wait for CSV to show up and complete
  170. k8s_info:
  171. kubeconfig: tmp/kubeconfig-ocp4
  172. validate_certs: no
  173. api_version: operators.coreos.com/v1alpha1
  174. kind: clusterserviceversion
  175. namespace: "{{ op.op_nsp }}"
  176. name: "{{ op.desired_csv }}"
  177. register: new_csv
  178. until:
  179. - new_csv.resources is defined
  180. - (new_csv.resources | length) > 0
  181. - new_csv.resources[0].status is defined
  182. - new_csv.resources[0].status.phase == "Succeeded"
  183. retries: 30
  184. delay: 10
  185. # TODO: Finish this at some point.
  186. #- name: Finally, wait for the pod
  187. # k8s_info:
  188. # kubeconfig: tmp/kubeconfig-ocp4
  189. # validate_certs: no
  190. # api_version: v1
  191. # kind: pod
  192. # namespace: rhsso
  193. # label_selectors:
  194. # - name = rhsso-operator
  195. # register: sso_pod
  196. # until:
  197. # - sso_pod.resources is defined
  198. # - (sso_pod.resources | length) > 0
  199. # - sso_pod.resources[0].status is defined
  200. # - sso_pod.resources[0].status.phase == "Running"
  201. # retries: 30
  202. # delay: 10
  203. - debug:
  204. msg: Operator {{ op.op_pkg }} deployed in namespace {{ op.op_nsp }} with CSV {{ op.desired_csv }}!
  205. when: found_csv is not defined or (found_csv | length) == 0
  206. ...