# Copyright 2018 The Knative 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
#
#     https://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: ServiceAccount
metadata:
  name: prometheus-test
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus-test
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-test
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: prometheus-test
subjects:
- kind: ServiceAccount
  name: prometheus-test
  namespace: default
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-test-scrape-config
  labels:
    name: prometheus-test-scrape-config
  namespace: default
data:
  prometheus.yml: |-
    scrape_configs:
    - job_name: telemetrysample-test
      scrape_interval: 3s
      scrape_timeout: 3s
      kubernetes_sd_configs:
      - role: endpoints
      relabel_configs:
      # Scrape only the the targets matching the following metadata
      - source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_pod_label_serving_knative_dev_configuration, __meta_kubernetes_endpoint_port_name]
        action: keep
        regex: default;telemetrysample-configuration;http
      # Rename metadata labels to be reader friendly
      - source_labels: [__meta_kubernetes_namespace]
        action: replace
        regex: (.*)
        target_label: namespace
        replacement: $1
      - source_labels: [__meta_kubernetes_pod_name]
        action: replace
        regex: (.*)
        target_label: pod
        replacement: $1
      - source_labels: [__meta_kubernetes_service_name]
        action: replace
        regex: (.*)
        target_label: service
        replacement: $1
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: prometheus-test-deployment
  namespace: default
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: prometheus-test
    spec:
      serviceAccountName: prometheus-test
      containers:
        - name: prometheus
          image: prom/prometheus:v2.2.1
          args:
            - "--config.file=/etc/prometheus/prometheus.yml"
            - "--storage.tsdb.path=/prometheus/"
          ports:
            - containerPort: 9090
          volumeMounts:
            - name: prometheus-config-volume
              mountPath: /etc/prometheus/
            - name: prometheus-storage-volume
              mountPath: /prometheus/
      volumes:
        - name: prometheus-config-volume
          configMap:
            defaultMode: 420
            name: prometheus-test-scrape-config
  
        - name: prometheus-storage-volume
          emptyDir: {}
