main.yml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ---
  2. # Ensures Grafana is deployed and connected to UWM Prometheus data source.
  3. - name: See if the Grafana project is there.
  4. kubernetes.core.k8s_info:
  5. kubeconfig: tmp/kubeconfig-ocp4
  6. validate_certs: no
  7. api_version: v1
  8. kind: namespace
  9. name: grafana
  10. register: grafana_ns
  11. - name: Fail if not so.
  12. ansible.builtin.assert:
  13. that:
  14. - grafana_ns.resources is defined
  15. - grafana_ns.resources | length == 1
  16. success_msg: "OK, grafana namespace found."
  17. fail_msg: "FATAL: grafana namespace is missing. Ensure the operator is deployed before proceeding."
  18. - name: See if the Grafana CSV is there as well.
  19. kubernetes.core.k8s_info:
  20. kubeconfig: tmp/kubeconfig-ocp4
  21. validate_certs: no
  22. api_version: operators.coreos.com/v1alpha1
  23. kind: clusterserviceversion
  24. namespace: grafana
  25. label_selectors:
  26. - operators.coreos.com/grafana-operator.grafana=
  27. register: grafana_csv
  28. - name: Fail if not so.
  29. ansible.builtin.assert:
  30. that:
  31. - grafana_csv.resources is defined
  32. - grafana_csv.resources | length > 0
  33. success_msg: "OK, grafana CSV found."
  34. fail_msg: "FATAL: grafana CSV is missing. Ensure the operator is deployed before proceeding."
  35. # https://github.com/rh-mobb/helm-charts/blob/main/charts/grafana-cr/templates/grafana.yaml
  36. # https://rhthsa.github.io/openshift-demo/application-metrics.html
  37. # https://rhthsa.github.io/openshift-demo/manifests/frontend-v1-and-backend-v1-JVM.yaml
  38. - name: Ensure a Grafana instance is there.
  39. kubernetes.core.k8s:
  40. kubeconfig: tmp/kubeconfig-ocp4
  41. validate_certs: no
  42. api_version: grafana.integreatly.org/v1beta1
  43. kind: grafana
  44. namespace: grafana
  45. name: dashboards
  46. resource_definition:
  47. metadata:
  48. labels:
  49. dashboards: dashboards
  50. spec:
  51. route:
  52. spec:
  53. host: dashboards-grafana.apps.ocp4.example.com
  54. port:
  55. targetPort: 3000
  56. tls:
  57. termination: edge
  58. to:
  59. kind: Service
  60. name: dashboards-service
  61. weight: 100
  62. wildcardPolicy: None
  63. - name: Give the service account permission to collect metrics.
  64. kubernetes.core.k8s:
  65. kubeconfig: tmp/kubeconfig-ocp4
  66. validate_certs: no
  67. api_version: rbac.authorization.k8s.io/v1
  68. kind: clusterrolebinding
  69. name: grafana-dashboards-sa
  70. resource_definition:
  71. roleRef:
  72. apiGroup: rbac.authorization.k8s.io
  73. kind: ClusterRole
  74. name: cluster-monitoring-view
  75. subjects:
  76. - kind: ServiceAccount
  77. name: dashboards-sa
  78. namespace: grafana
  79. - name: Ensure there is a token secret.
  80. kubernetes.core.k8s:
  81. kubeconfig: tmp/kubeconfig-ocp4
  82. validate_certs: no
  83. api_version: v1
  84. kind: secret
  85. namespace: grafana
  86. name: dashboards-sa-token
  87. resource_definition:
  88. type: kubernetes.io/service-account-token
  89. metadata:
  90. annotations:
  91. kubernetes.io/service-account.name: dashboards-sa
  92. - name: Obtain the token from the secret.
  93. kubernetes.core.k8s_info:
  94. kubeconfig: tmp/kubeconfig-ocp4
  95. validate_certs: no
  96. api_version: v1
  97. kind: secret
  98. namespace: grafana
  99. name: dashboards-sa-token
  100. register: sa_token_secret
  101. - name: Register the token as a fact (grafana_token).
  102. ansible.builtin.set_fact:
  103. grafana_token: "{{ sa_token_secret.resources[0].data.token | b64decode }}"
  104. - name: Ensure a GrafanaDataSource is defined.
  105. kubernetes.core.k8s:
  106. kubeconfig: tmp/kubeconfig-ocp4
  107. validate_certs: no
  108. api_version: grafana.integreatly.org/v1beta1
  109. kind: grafanadatasource
  110. namespace: grafana
  111. name: user-workload-monitoring
  112. resource_definition:
  113. spec:
  114. instanceSelector:
  115. matchLabels:
  116. dashboards: dashboards
  117. datasource:
  118. name: UserMetrics
  119. type: prometheus
  120. url: 'https://thanos-querier.openshift-monitoring.svc:9091'
  121. access: proxy
  122. editable: false
  123. isDefault: true
  124. jsonData:
  125. httpHeaderName1: 'Authorization'
  126. timeInterval: 5s
  127. tlsSkipVerify: true
  128. secureJsonData:
  129. httpHeaderValue1: 'Bearer {{grafana_token}}'
  130. ...