#Ref:https://medium.com/@gurpreets0610/deploy-prometheus-grafana-on-kubernetes-cluster-e8395cc16f91
#  First, we will create a Kubernetes namespace for all our monitoring components
apiVersion: v1
kind: Namespace
metadata:
  name: monitoring
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: monitoring
data:
  prometheus.yml: |
    # my global config
    global:
      scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
      evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
    # A scrape configuration containing exactly one endpoint to scrape:
    # Here it's Prometheus itself.
    scrape_configs:
      # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
      - job_name: 'prometheus'
        # metrics_path defaults to '/metrics'
        # scheme defaults to 'http'.
        static_configs:
        - targets: ['localhost:9090']
    # MS-Demo App
      - job_name: 'ms-demo-service'
        # Override the global default and scrape targets from this job every 5 seconds.
        scrape_interval: 5s
        metrics_path: /actuator/prometheus
        static_configs:
          # Use the service name (see docker-compose-desktop.yml) instead of localhost
          - targets: ['ms-demo-service.default.svc:8080']
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-manual-data
  namespace: monitoring
  labels:
    type: manual
    app: prometheus-deployment
spec:
#spec:https://stackoverflow.com/questions/54676429/how-to-access-persistentvolume-files-on-docker-for-desktop
  storageClassName: manual
  #    https://stackoverflow.com/questions/71018631/kubernetes-on-docker-for-windows-persistent-volume-with-hostpath-gives-operatio
  hostPath:
    path: '/run/desktop/mnt/host/d/promdata'
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-manual-data
  namespace: monitoring
  labels:
    app: prometheus-deployment
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 500Mi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
  namespace: monitoring
  labels:
    app: prometheus
spec:
  replicas: 1
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9090"
    spec:
      containers:
        - name: prometheus
          image: prom/prometheus
          imagePullPolicy: IfNotPresent
          args:
            - '--storage.tsdb.retention=6h'
            - '--storage.tsdb.path=/prometheus'
            - '--config.file=/etc/prometheus/prometheus.yml'
          ports:
            - name: web
              containerPort: 9090
          volumeMounts:
            - name: prometheus-config-volume
              mountPath: /etc/prometheus
            - name: prometheus-storage-volume
              mountPath: /prometheus
      restartPolicy: Always

      volumes:
        - name: prometheus-config-volume
          configMap:
            defaultMode: 420
            name: prometheus-config

        - name: prometheus-storage-volume
          persistentVolumeClaim:
            claimName: pvc-manual-data
---
apiVersion: v1
kind: Service
metadata:
  name: prometheus-service
  namespace: monitoring
  annotations:
    prometheus.io/scrape: 'true'
    prometheus.io/port:   '9090'
spec:
  selector:
    app: prometheus
  type: NodePort
  ports:
    - port: 8080
      targetPort: 9090
      nodePort: 30000
---
