#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.
#

apiVersion: v1
kind: ConfigMap
metadata:
    name: prometheus-config
data:
    # Include prometheus configuration file, setup to monitor all the
    # Kubernetes pods with the "scrape=true" annotation.
    prometheus.yml: |
        global:
            scrape_interval: 15s
        scrape_configs:
          - job_name: 'prometheus'
            static_configs:
              - targets: ['localhost:9090']
          - job_name: 'kubernetes-pods'
            kubernetes_sd_configs:
              - role: pod

            relabel_configs:
              - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
                action: keep
                regex: true
              - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
                action: replace
                target_label: __metrics_path__
                regex: (.+)
              - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
                action: replace
                regex: ([^:]+)(?::\d+)?;(\d+)
                replacement: $1:$2
                target_label: __address__
              - action: labelmap
                regex: __meta_kubernetes_pod_label_(.+)
              - source_labels: [__meta_kubernetes_namespace]
                action: replace
                target_label: kubernetes_namespace
              - source_labels: [__meta_kubernetes_pod_label_component]
                action: replace
                target_label: job
              - source_labels: [__meta_kubernetes_pod_name]
                action: replace
                target_label: kubernetes_pod_name

---

kind: PersistentVolume
apiVersion: v1
metadata:
    name: task-pv-volume
    labels:
        type: local
spec:
    storageClassName: gp2
    capacity:
        storage: 100Gi
    accessModes:
    - ReadWriteOnce
    hostPath:
        path: "/mnt/data"

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: prometheus-data-volume
    annotations:
        # Allow relaxed durability for stats storage
        volume.beta.kubernetes.io/mount-options: "discard"
spec:
    resources:
        requests:
            storage: 50Gi
    accessModes:
      - ReadWriteOnce
    storageClassName: gp2

---

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
    name: standard
provisioner: kubernetes.io/aws-ebs
parameters:
    type: gp2
    zones: us-west-2a, us-west-2b, us-west-2c

---

apiVersion: apps/v1beta1
kind: Deployment
metadata:
    name: prometheus
spec:
    replicas: 1
    template:
        metadata:
            labels:
                app: pulsar
                component: prometheus
        spec:
            containers:
              - name: prometheus
                image: prom/prometheus:v1.6.3
                volumeMounts:
                  - name: config-volume
                    mountPath: /etc/prometheus
                  - name: data-volume
                    mountPath: /prometheus
                ports:
                  - containerPort: 9090
            volumes:
              - name: config-volume
                configMap:
                    name: prometheus-config
              - name: data-volume
                persistentVolumeClaim:
                  claimName: prometheus-data-volume

---

apiVersion: v1
kind: Service
metadata:
    name: prometheus
    labels:
        app: pulsar
        component: prometheus
spec:
    type: NodePort
    ports:
      - name: prometheus
        nodePort: 30003
        port: 9090
        protocol: TCP
    selector:
        app: pulsar
        component: prometheus


---
## GRAFANA

apiVersion: apps/v1beta1
kind: Deployment
metadata:
    name: grafana
spec:
    replicas: 1
    template:
        metadata:
            labels:
                app: pulsar
                component: grafana
        spec:
            containers:
              - name: grafana
                image: apachepulsar/pulsar-grafana:latest
                ports:
                  - containerPort: 3000
                env:
                  - name: PROMETHEUS_URL
                    value: http://prometheus:9090/

---

apiVersion: v1
kind: Service
metadata:
    name: grafana
    labels:
        app: pulsar
        component: grafana
spec:
    type: NodePort
    ports:
      - name: grafana
        nodePort: 30004
        port: 3000
        protocol: TCP 
    selector:
        app: pulsar
        component: grafana

---

apiVersion: v1
kind: Service
metadata:
  name: grafana-proxy
  labels:
    app: pulsar
    component: grafana
spec:
  type: LoadBalancer
  ports:
    - name: http
      nodePort: 30006
      port: 3000
      protocol: TCP
  selector:
    app: pulsar
    component: grafana

---
## Include detailed Pulsar dashboard

apiVersion: apps/v1beta1
kind: Deployment
metadata:
    name: pulsar-dashboard
spec:
    replicas: 1
    template:
        metadata:
            labels:
                app: pulsar
                component: dashboard
        spec:
            containers:
              - name: grafana
                image: apachepulsar/pulsar-dashboard:latest
                ports:
                  - containerPort: 80
                env:
                  - name: SERVICE_URL
                    value: http://broker:8080/

---

apiVersion: v1
kind: Service
metadata:
  name: pulsar-dashboard-proxy
  labels:
    app: pulsar
    component: dashboard
spec:
  type: LoadBalancer
  ports:
    - name: http
      nodePort: 30005
      port: 80
      protocol: TCP
  selector:
    app: pulsar
    component: dashboard
