# Copyright 2022 Dell Inc. or its subsidiaries. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
---

- name: Grafana pod
  kubernetes.core.k8s:
    state: present
    definition:
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: "{{ grafana_k8s }}"
        namespace: "{{ grafana_namespace }}"
        labels:
          app: "{{ grafana_k8s }}"
      spec:
        selector:
          matchLabels:
            app: "{{ grafana_k8s }}"
        replicas: 1
        strategy:
          type: RollingUpdate
        template:
          metadata:
            labels:
              app: "{{ grafana_k8s }}"
          spec:
            containers:
              - name: grafana
                image: "{{ grafana_image }}"
                imagePullPolicy: "IfNotPresent"
                env:
                - name: GF_SERVER_HTTP_PORT
                  value: "{{ grafana_port }}"
                - name: GF_PLUGINS_PLUGIN_ADMIN_ENABLED
                  value: "true"
                - name: GF_PLUGINS_ALLOW_LOADING_UNSIGNED_PLUGINS
                  value: "{{ grafana_plugins_names }}"
                - name: GF_SECURITY_ADMIN_USER
                  valueFrom:
                    secretKeyRef:
                      name: "{{ grafana_secrets }}"
                      key: grafana_username
                - name: GF_SECURITY_ADMIN_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: "{{ grafana_secrets }}"
                      key: grafana_password
                ports:
                - containerPort: "{{ grafana_http_port }}"
                volumeMounts:
                - mountPath: /var/lib/grafana/
                  name: grafana-data
            volumes:
            - name: grafana-data
              persistentVolumeClaim:
                claimName: grafana-volume-claim

- name: Service for grafana
  kubernetes.core.k8s:
    state: present
    definition:
      apiVersion: v1
      kind: Service
      metadata:
        name: "{{ grafana_k8s }}"
        namespace: "{{ grafana_namespace }}"
        labels:
          app: "{{ grafana_k8s }}"
      spec:
        type: ClusterIP
        ports:
          - name: http
            port: "{{ grafana_http_port }}"
        selector:
          app: "{{ grafana_k8s }}"