Changes for Helm v3 compatibility:
[infra/stack/kubernetes.git] / apps / prometheus / kubespray / playbooks / roles / install / tasks / main.yaml
1 ---
2 # ============LICENSE_START=======================================================
3 #  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
4 # ================================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 # SPDX-License-Identifier: Apache-2.0
18 # ============LICENSE_END=========================================================
19
20 - name: Load execution mode variables
21   include_vars: "{{ execution_mode }}.yaml"
22
23 - block:
24   - name: Create directories for helm repositories
25     file:
26       path: "{{ item.path }}"
27       state: "{{ item.state }}"
28     loop:
29       - {path: "{{ engine_workspace }}/offline/charts/stable", state: absent}
30       - {path: "{{ engine_workspace }}/offline/charts/stable", state: directory}
31       - {path: "{{ engine_workspace }}/offline/charts/local", state: absent}
32       - {path: "{{ engine_workspace }}/offline/charts/local", state: directory}
33
34   - name: Place index.yaml to webserver stable charts repository
35     template:
36       src: "index.yaml.j2"
37       dest: "{{ engine_workspace }}/offline/charts/stable/index.yaml"
38       force: true
39
40   - name: Create local repo index (empty initially)
41     command: "helm repo index {{ engine_workspace }}/offline/charts/local/"
42     args:
43       creates: "{{ engine_workspace }}/offline/charts/local/index.yaml"
44   when: execution_mode == "offline-deployment"
45
46 - name: Initialize Helm
47   command: helm init --client-only --local-repo-url {{ local_repo_url }} --stable-repo-url {{ stable_repo_url }}
48   register: helm_init_result
49   changed_when: true
50   when: helm_version is version('v3', '<')
51
52 - name: Check if stable helm repo exists (helm v3+)
53   command: "helm repo list"
54   changed_when: true
55   failed_when: false
56   register: helm_repo_list_result
57
58 - name: Add stable helm repo (helm v3+)
59   command: "helm repo add stable {{ stable_repo_url }}"
60   register: helm_repo_add_stable_result
61   changed_when: true
62   when: >
63     helm_version is version('v3.0.0', '>=')
64     and execution_mode == "online-deployment"
65     and helm_repo_list_result is not search(stable_repo_url)
66
67 - name: Add local helm repo (helm v3+)
68   command: "helm repo add local {{ local_repo_url }}"
69   register: helm_repo_add_local_result
70   changed_when: true
71   when: >
72     helm_version is version('v3.0.0', '>=')
73     and execution_mode == "offline-deployment"
74     and helm_repo_list_result is not search(local_repo_url)
75
76 - name: Clone Helm Charts repository
77   git:
78     repo: "{{ helm_charts_git_url }}"
79     dest: "{{ config_path }}/repos/charts"
80     version: "{{ charts_version }}"
81     force: true
82     recursive: true
83   environment:
84     http_proxy: "{{ lookup('env','http_proxy') }}"
85     https_proxy: "{{ lookup('env','https_proxy') }}"
86     no_proxy: "{{ lookup('env','no_proxy') }}"
87
88 - name: Generate values.yaml
89   template:
90     src: "values.yaml.j2"
91     dest: "{{ config_path }}/repos/charts/stable/prometheus/values.yaml"
92     force: true
93
94 - name: Remove previous installations of Prometheus
95   command: >
96     helm delete --purge "{{ prometheus_service }}"
97   changed_when: true
98   ignore_errors: true
99   tags: reset
100
101 - name: Remove Prometheus namespace
102   command: >
103     kubectl delete ns "{{ prometheus_namespace }}"
104   changed_when: true
105   ignore_errors: true
106   tags: reset
107
108 - name: Create Prometheus namespace
109   k8s:
110     state: present
111     definition:
112       apiVersion: v1
113       kind: Namespace
114       metadata:
115         name: "{{ prometheus_namespace }}"
116
117 - name: Install Prometheus using helm
118   command: >
119     helm install
120       {% if helm_version is version('v3.0.0', '<') %} --name {% endif %}"{{ prometheus_service }}"
121       --namespace "{{ prometheus_namespace }}"
122       --timeout "900{% if helm_version is version('v3.0.0', '>=') %}s{% endif %}"
123       {{ config_path }}/repos/charts/stable/prometheus
124   register: prometheus_helm_log
125   changed_when: true
126
127 - name: Log Prometheus helm output to console
128   debug:
129     msg: "{{ prometheus_helm_log.stdout_lines }}"
130
131 - name: Wait until Prometheus pods are available
132   k8s_facts:
133     kind: Pod
134     namespace: "{{ prometheus_namespace }}"
135     label_selectors:
136       - "app = {{ prometheus_service }}"
137     field_selectors:
138       - status.phase=Running
139   register: prometheus_pod_status
140   until:
141     - prometheus_pod_status.resources is defined
142     - prometheus_pod_status.resources
143   retries: 30
144   delay: 10
145
146 - name: Install Prometheus LoadBalancer service
147   k8s:
148     state: present
149     definition: "{{ lookup('template', 'prometheus_service.yaml.j2') }}"
150   register: prometheus_service_status
151
152 - name: Log Prometheus service information to console
153   debug:
154     msg:
155       - "------------------------------"
156       - "Prometheus Service information"
157       - "------------------------------"
158       - "clusterIP:  {{ prometheus_service_status.result.spec.clusterIP }}"
159       - "targetPort: {{ prometheus_service_status.result.spec.ports[0].targetPort }}"
160       - "nodePort:   {{ prometheus_service_status.result.spec.ports[0].nodePort }}"
161       - "------------------------------"
162
163 # vim: set ts=2 sw=2 expandtab: