# The following example shows how you can set up and use prometheus
# and a configured scrape config to get metrics from brupop on the
# nodes that have been updated and their status.

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus-service-account
  namespace: brupop-bottlerocket-aws
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus-role
  namespace: brupop-bottlerocket-aws
rules:
  - apiGroups: [""]
    resources:
      - nodes
      - services
      - endpoints
      - pods
    verbs:
      - get
      - list
      - watch
  - apiGroups: [""]
    resources:
      - configmaps
    verbs:
      - get
  - nonResourceURLs: ["/metrics"]
    verbs:
      - get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: prometheus-role-binding
  namespace: brupop-bottlerocket-aws
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: prometheus-role
subjects:
  - kind: ServiceAccount
    name: prometheus-service-account
    namespace: brupop-bottlerocket-aws
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: brupop-bottlerocket-aws
data:
  prometheus.yml: |
    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.
      # scrape_timeout is set to the global default (10s).

    # A scrape configuration
    scrape_configs:
      - job_name: "prometheus"
        static_configs:
        - targets: ['localhost:9090']
      - job_name: "kubernetes-service-endpoints"
        kubernetes_sd_configs:
          - role: endpoints
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: prometheus-pvc
  namespace: brupop-bottlerocket-aws
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus-deployment
  namespace: brupop-bottlerocket-aws
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
      namespace: brupop-bottlerocket-aws
    spec:
      containers:
      - name: prometheus-container
        image: prom/prometheus
        volumeMounts:
        - name: prometheus-volume
          mountPath: /prometheus
        - name: config-volume
          mountPath: /etc/prometheus/prometheus.yml
          subPath: prometheus.yml
        ports:
        - containerPort: 9090
      initContainers:
      - name: prometheus-data-permission-fix
        image: busybox
        command: ["/bin/chmod", "-R", "777", "/data"]
        volumeMounts:
        - name: prometheus-volume
          mountPath: /data
      volumes:
        - name: config-volume
          configMap:
            name: prometheus-config
        - name: prometheus-volume
          persistentVolumeClaim:
            claimName: prometheus-pvc
      serviceAccountName: prometheus-service-account