install_prometheus.yaml 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. ---
  2. # namespace
  3. - name: Add prometheus project
  4. oc_project:
  5. state: present
  6. name: "{{ openshift_prometheus_namespace }}"
  7. node_selector: "{{ openshift_prometheus_node_selector | lib_utils_oo_selector_to_string_list() }}"
  8. description: Prometheus
  9. # secrets
  10. - name: Set alert and prometheus secrets
  11. oc_secret:
  12. state: present
  13. name: "{{ item }}-proxy"
  14. namespace: "{{ openshift_prometheus_namespace }}"
  15. contents:
  16. - path: session_secret
  17. data: "{{ 43 | lib_utils_oo_random_word }}="
  18. with_items:
  19. - prometheus
  20. - alerts
  21. # serviceaccount
  22. - name: create prometheus serviceaccount
  23. oc_serviceaccount:
  24. state: present
  25. name: prometheus
  26. namespace: "{{ openshift_prometheus_namespace }}"
  27. # TODO add annotations when supproted
  28. # annotations:
  29. # serviceaccounts.openshift.io/oauth-redirectreference.prom: '{"kind":"OAuthRedirectReference","apiVersion":"v1","reference":{"kind":"Route","name":"prometheus"}}'
  30. # serviceaccounts.openshift.io/oauth-redirectreference.alerts: '{"kind":"OAuthRedirectReference","apiVersion":"v1","reference":{"kind":"Route","name":"alerts"}}'
  31. secrets:
  32. - prometheus-secrets
  33. changed_when: no
  34. # TODO remove this when annotations are supported by oc_serviceaccount
  35. - name: annotate serviceaccount
  36. command: >
  37. {{ openshift_client_binary }} annotate --overwrite -n {{ openshift_prometheus_namespace }}
  38. serviceaccount prometheus
  39. serviceaccounts.openshift.io/oauth-redirectreference.prom='{"kind":"OAuthRedirectReference","apiVersion":"v1","reference":{"kind":"Route","name":"prometheus"}}'
  40. serviceaccounts.openshift.io/oauth-redirectreference.alerts='{"kind":"OAuthRedirectReference","apiVersion":"v1","reference":{"kind":"Route","name":"alerts"}}'
  41. # create clusterrolebinding for prometheus serviceaccount
  42. - name: Set cluster-reader permissions for prometheus
  43. oc_adm_policy_user:
  44. state: present
  45. namespace: "{{ openshift_prometheus_namespace }}"
  46. resource_kind: cluster-role
  47. resource_name: cluster-reader
  48. user: "system:serviceaccount:{{ openshift_prometheus_namespace }}:prometheus"
  49. # create prometheus and alerts services
  50. # TODO join into 1 task with loop
  51. - name: Create prometheus service
  52. oc_service:
  53. state: present
  54. name: "{{ item.name }}"
  55. namespace: "{{ openshift_prometheus_namespace }}"
  56. selector:
  57. app: prometheus
  58. labels:
  59. name: "{{ item.name }}"
  60. # TODO add annotations when supported
  61. # annotations:
  62. # service.alpha.openshift.io/serving-cert-secret-name: "{{item.name}}-tls"
  63. ports:
  64. - port: 443
  65. targetPort: 8443
  66. with_items:
  67. - name: prometheus
  68. - name: Create alerts service
  69. oc_service:
  70. state: present
  71. name: "{{ item.name }}"
  72. namespace: "{{ openshift_prometheus_namespace }}"
  73. selector:
  74. app: prometheus
  75. labels:
  76. name: "{{ item.name }}"
  77. # TODO add annotations when supported
  78. # annotations:
  79. # service.alpha.openshift.io/serving-cert-secret-name: "{{item.name}}-tls"
  80. ports:
  81. - port: 443
  82. targetPort: 9443
  83. with_items:
  84. - name: alerts
  85. # Annotate services with secret name
  86. # TODO remove this when annotations are supported by oc_service
  87. - name: annotate prometheus service
  88. command: >
  89. {{ openshift_client_binary }} annotate --overwrite -n {{ openshift_prometheus_namespace }}
  90. service prometheus
  91. prometheus.io/scrape='true'
  92. prometheus.io/scheme=https
  93. service.alpha.openshift.io/serving-cert-secret-name=prometheus-tls
  94. - name: annotate alerts service
  95. command: >
  96. {{ openshift_client_binary }} annotate --overwrite -n {{ openshift_prometheus_namespace }}
  97. service alerts 'service.alpha.openshift.io/serving-cert-secret-name=prometheus-alerts-tls'
  98. # create prometheus and alerts routes
  99. - name: create prometheus and alerts routes
  100. oc_route:
  101. state: present
  102. name: "{{ item.name }}"
  103. host: "{{ item.host }}"
  104. namespace: "{{ openshift_prometheus_namespace }}"
  105. service_name: "{{ item.name }}"
  106. tls_termination: reencrypt
  107. with_items:
  108. - name: prometheus
  109. host: "{{ openshift_prometheus_hostname }}"
  110. - name: alerts
  111. host: "{{ openshift_prometheus_alerts_hostname }}"
  112. # Storage
  113. - name: create prometheus pvc
  114. oc_pvc:
  115. namespace: "{{ openshift_prometheus_namespace }}"
  116. name: "{{ openshift_prometheus_pvc_name }}"
  117. access_modes: "{{ openshift_prometheus_pvc_access_modes }}"
  118. volume_capacity: "{{ openshift_prometheus_pvc_size }}"
  119. selector: "{{ openshift_prometheus_pvc_pv_selector }}"
  120. storage_class_name: "{{ openshift_prometheus_sc_name }}"
  121. when: openshift_prometheus_storage_type == 'pvc'
  122. - name: create alertmanager pvc
  123. oc_pvc:
  124. namespace: "{{ openshift_prometheus_namespace }}"
  125. name: "{{ openshift_prometheus_alertmanager_pvc_name }}"
  126. access_modes: "{{ openshift_prometheus_alertmanager_pvc_access_modes }}"
  127. volume_capacity: "{{ openshift_prometheus_alertmanager_pvc_size }}"
  128. selector: "{{ openshift_prometheus_alertmanager_pvc_pv_selector }}"
  129. storage_class_name: "{{ openshift_prometheus_alertmanager_sc_name }}"
  130. when: openshift_prometheus_alertmanager_storage_type == 'pvc'
  131. - name: create alertbuffer pvc
  132. oc_pvc:
  133. namespace: "{{ openshift_prometheus_namespace }}"
  134. name: "{{ openshift_prometheus_alertbuffer_pvc_name }}"
  135. access_modes: "{{ openshift_prometheus_alertbuffer_pvc_access_modes }}"
  136. volume_capacity: "{{ openshift_prometheus_alertbuffer_pvc_size }}"
  137. selector: "{{ openshift_prometheus_alertbuffer_pvc_pv_selector }}"
  138. storage_class_name: "{{ openshift_prometheus_alertbuffer_sc_name }}"
  139. when: openshift_prometheus_alertbuffer_storage_type == 'pvc'
  140. # prometheus configmap
  141. # Copy the additional rules file if it is defined
  142. - name: Copy additional rules file to host
  143. copy:
  144. src: "{{ openshift_prometheus_additional_rules_file }}"
  145. dest: "{{ tempdir }}/prometheus.additional.rules"
  146. when:
  147. - openshift_prometheus_additional_rules_file is defined
  148. - openshift_prometheus_additional_rules_file is not none
  149. - openshift_prometheus_additional_rules_file | trim | length > 0
  150. - stat:
  151. path: "{{ tempdir }}/prometheus.additional.rules"
  152. register: additional_rules_stat
  153. # The kubernetes version impacts the prometheus scraping endpoint
  154. # so gathering it before constructing the configmap
  155. - name: get oc version
  156. oc_version:
  157. register: oc_version
  158. - set_fact:
  159. kubernetes_version: "{{ oc_version.results.kubernetes_short | float }}"
  160. - template:
  161. src: prometheus.yml.j2
  162. dest: "{{ tempdir }}/prometheus.yml"
  163. changed_when: no
  164. - template:
  165. src: prometheus.rules.j2
  166. dest: "{{ tempdir }}/prometheus.rules"
  167. changed_when: no
  168. # In prometheus configmap create "additional.rules" section if file exists
  169. - name: Set prometheus configmap
  170. oc_configmap:
  171. state: present
  172. name: "prometheus"
  173. namespace: "{{ openshift_prometheus_namespace }}"
  174. from_file:
  175. prometheus.rules: "{{ tempdir }}/prometheus.rules"
  176. prometheus.additional.rules: "{{ tempdir }}/prometheus.additional.rules"
  177. prometheus.yml: "{{ tempdir }}/prometheus.yml"
  178. when: additional_rules_stat.stat.exists == True
  179. - name: Set prometheus configmap
  180. oc_configmap:
  181. state: present
  182. name: "prometheus"
  183. namespace: "{{ openshift_prometheus_namespace }}"
  184. from_file:
  185. prometheus.rules: "{{ tempdir }}/prometheus.rules"
  186. prometheus.yml: "{{ tempdir }}/prometheus.yml"
  187. when: additional_rules_stat.stat.exists == False
  188. # alertmanager configmap
  189. - template:
  190. src: alertmanager.yml.j2
  191. dest: "{{ tempdir }}/alertmanager.yml"
  192. changed_when: no
  193. - name: Set alertmanager configmap
  194. oc_configmap:
  195. state: present
  196. name: "prometheus-alerts"
  197. namespace: "{{ openshift_prometheus_namespace }}"
  198. from_file:
  199. alertmanager.yml: "{{ tempdir }}/alertmanager.yml"
  200. # create prometheus stateful set
  201. - name: Set prometheus template
  202. template:
  203. src: prometheus.j2
  204. dest: "{{ tempdir }}/templates/prometheus.yaml"
  205. vars:
  206. namespace: "{{ openshift_prometheus_namespace }}"
  207. # prom_replicas: "{{ openshift_prometheus_replicas }}"
  208. - name: Set prometheus stateful set
  209. oc_obj:
  210. state: present
  211. name: "prometheus"
  212. namespace: "{{ openshift_prometheus_namespace }}"
  213. kind: statefulset
  214. files:
  215. - "{{ tempdir }}/templates/prometheus.yaml"
  216. delete_after: true