#   Copyright 2020 Minsait - Indra S.A.
#
#   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.
#   Author: Jose Manuel Palacios (jmpalacios@minsait.com)
#   Author: Alberto Limon (alimonj@minsait.com)

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  labels:
    app: grafana
  name: grafana-clusterrole
rules:
- apiGroups:
  - ""
  resources:
  - configmaps
  - secrets
  verbs:
  - get
  - watch
  - list
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  labels:
    app: grafana
  name: grafana-clusterrolebinding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: grafana-clusterrole
subjects:
- kind: ServiceAccount
  name: grafana
  namespace: osm
---
apiVersion: v1
data:
  admin-password: YWRtaW4=
  admin-user: YWRtaW4=
kind: Secret
metadata:
  labels:
    app: grafana
  name: grafana
type: Opaque
---
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app: grafana
  name: grafana
---
apiVersion: v1
data:
  provider.yaml: |-
    apiVersion: 1
    providers:
    - name: 'Kubernetes Cluster'
      orgId: 1
      folder: 'Kubernetes Cluster'
      type: file
      disableDeletion: false
      options:
        path: '/tmp/dashboards/Kubernetes Cluster'
    - name: 'Open Source MANO'
      orgId: 1
      folder: 'Open Source MANO'
      type: file
      disableDeletion: false
      options:
        path: '/tmp/dashboards/Open Source MANO'
kind: ConfigMap
metadata:
  labels:
    app: grafana
  name: grafana-dashboard-provider
---
apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    grafana_datasource: "1"
  name: grafana-datasource
data:
  datasource.yaml: |-
    apiVersion: 1
    datasources:
    - name: osm_prometheus
      type: prometheus
      url: http://prometheus:9090
      access: proxy
      allowUiUpdates: true
      isDefault: true
    - name: Prometheus
      type: prometheus
      url: http://osm-monitoring-prometheus-prometheus.monitoring:9090
      access: proxy
      allowUiUpdates: true
      isDefault: false
---
apiVersion: v1
data:
  grafana.ini: |
    [log]
    mode = console
    [paths]
    data = /var/lib/grafana/data
    logs = /var/log/grafana
    plugins = /var/lib/grafana/plugins
    provisioning = /etc/grafana/provisioning
kind: ConfigMap
metadata:
  labels:
    app: grafana
  name: grafana
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: grafana
  name: grafana
spec:
  replicas: 1
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      containers:
      - env:
        - name: LABEL
          value: grafana_dashboard
        - name: FOLDER
          value: "/tmp/dashboards/Kubernetes Cluster"
        - name: RESOURCE
          value: both
        - name: NAMESPACE
          value: monitoring
        image: kiwigrid/k8s-sidecar:0.1.20
        imagePullPolicy: IfNotPresent
        name: grafana-sc-dashboard
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: "/tmp/dashboards/Kubernetes Cluster"
          name: sc-dashboard-volume-k8s
        - mountPath: "/tmp/dashboards/Open Source MANO"
          name: sc-dashboard-volume-osm
      - env:
        - name: GF_SECURITY_ADMIN_USER
          valueFrom:
            secretKeyRef:
              key: admin-user
              name: grafana
        - name: GF_SECURITY_ADMIN_PASSWORD
          valueFrom:
            secretKeyRef:
              key: admin-password
              name: grafana
        image: grafana/grafana:6.3.5
        imagePullPolicy: IfNotPresent
        livenessProbe:
          failureThreshold: 10
          httpGet:
            path: /api/health
            port: 3000
            scheme: HTTP
          initialDelaySeconds: 60
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 30
        name: grafana
        ports:
        - containerPort: 80
          name: service
          protocol: TCP
        - containerPort: 3000
          name: grafana
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /api/health
            port: 3000
            scheme: HTTP
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /etc/grafana/grafana.ini
          name: config
          subPath: grafana.ini
        - mountPath: /var/lib/grafana
          name: storage
        - mountPath: "/tmp/dashboards/Kubernetes Cluster"
          name: sc-dashboard-volume-k8s
        - mountPath: "/tmp/dashboards/Open Source MANO"
          name: sc-dashboard-volume-osm
        - mountPath: /etc/grafana/provisioning/dashboards/sc-dashboardproviders.yaml
          name: sc-dashboard-provider
          subPath: provider.yaml
        - mountPath: /etc/grafana/provisioning/datasources
          name: sc-datasources-volume
      dnsPolicy: ClusterFirst
      initContainers:
      - env:
        - name: METHOD
          value: LIST
        - name: LABEL
          value: grafana_datasource
        - name: FOLDER
          value: /etc/grafana/provisioning/datasources
        - name: RESOURCE
          value: both
        image: kiwigrid/k8s-sidecar:0.1.20
        imagePullPolicy: IfNotPresent
        name: grafana-sc-datasources
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /etc/grafana/provisioning/datasources
          name: sc-datasources-volume
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext:
        fsGroup: 472
        runAsUser: 472
      serviceAccount: grafana
      serviceAccountName: grafana
      terminationGracePeriodSeconds: 30
      volumes:
      - configMap:
          defaultMode: 420
          name: grafana
        name: config
      - emptyDir: {}
        name: storage
      - emptyDir: {}
        name: sc-dashboard-volume-k8s
      - emptyDir: {}
        name: sc-dashboard-volume-osm
      - configMap:
          defaultMode: 420
          name: grafana-dashboard-provider
        name: sc-dashboard-provider
      - emptyDir: {}
        name: sc-datasources-volume
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: grafana
  name: grafana
spec:
  ports:
  - name: service
    nodePort: 3000
    port: 3000
    protocol: TCP
    targetPort: 3000
  selector:
    app: grafana
  sessionAffinity: None
  type: NodePort
