# Copyright 2018 The Kubernetes Authors.
#
# 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.

# Prometheus metrics monitoring pod and configuration. To use this:
#
# $ kubectl create -f prometheus.yaml
#
# The Prometheus UI can be accessed via kubectl port-forward:
#
# $ kubectl get pods -l app=prometheus -o name \
#   | sed 's/^.*\///' \
#   | xargs -I{} kubectl port-forward {} 9090:9090
#
# Go to http://localhost:9090 on your browser to see the prometheus UI.
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus
data:
  prometheus.yaml: |-
    global:
      scrape_interval: 5s
      scrape_timeout: 5s
    scrape_configs:
    - job_name: 'prometheus'
      static_configs:
        - targets: ['localhost:9090']
    - job_name: 'kube-dns'
      scheme: http
      kubernetes_sd_configs:
      - api_servers:
        - 'https://kubernetes.default.svc'
        in_cluster: true
        role: container
      # Note: this configuration selects the dnsmasq-metrics and
      # kubedns containers by name to include the dns daemons that are
      # created by the performance test. In production, it should
      # filter out first by the k8s-app and kube-system namespace as
      # well.
      relabel_configs:
      - source_labels: [__meta_kubernetes_pod_container_name]
        action: keep
        regex: (dnsmasq-metrics|kubedns)
      - source_labels: [__meta_kubernetes_pod_container_port_list]
        action: replace
        regex: (?:.*),metrics=(\d+)(?:.*)
        replacement: ${1}
        target_label: __metrics_port__
      - source_labels: [__address__, __metrics_port__]
        action: replace
        regex: (.+):(?:\d+);(\d+)
        replacement: ${1}:${2}
        target_label: __address__
      - action: labelmap
        regex: __meta_kubernetes_pod_label_(.+)
      - source_labels: [__meta_kubernetes_pod_container_name]
        action: replace
        target_label: kubernetes_container_name
      - source_labels: [__meta_kubernetes_pod_namespace]
        action: replace
        target_label: kubernetes_namespace
      - source_labels: [__meta_kubernetes_pod_name]
        action: replace
        target_label: kubernetes_pod_name
---
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: prometheus
  name: prometheus
spec:
  containers:
  - args:
    - -config.file=/etc/prometheus/prometheus.yaml
    - -storage.local.path=/prometheus
    - -storage.local.retention=72h
    command:
    - /bin/prometheus
    image: quay.io/prometheus/prometheus:v1.0.1
    imagePullPolicy: IfNotPresent
    name: prometheus
    ports:
    - containerPort: 9090
      protocol: TCP
    resources:
      requests:
        cpu: 100m
        memory: 100Mi
    terminationMessagePath: /dev/termination-log
    volumeMounts:
    - name: data
      mountPath: /prometheus
    - name: config-volume
      mountPath: /etc/prometheus
  dnsPolicy: ClusterFirst
  restartPolicy: Always
  terminationGracePeriodSeconds: 30
  volumes:
  - emptyDir: {}
    name: data
  - configMap:
      defaultMode: 420
      name: prometheus
    name: config-volume
