# 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: Service
metadata:
  name: prometheus-system-discovery
  namespace: knative-monitoring
  labels:
    app: prometheus
    serving.knative.dev/release: devel
spec:
  clusterIP: None
  ports:
  - name: web
    port: 9090
    protocol: TCP
    targetPort: web
  selector:
    app: prometheus
  sessionAffinity: None
  type: ClusterIP
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus-system
  namespace: knative-monitoring
  labels:
    serving.knative.dev/release: devel
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: prometheus-system
  namespace: default
  labels:
    serving.knative.dev/release: devel
rules:
- apiGroups: [""]
  resources:
  - nodes
  - services
  - endpoints
  - pods
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources:
  - configmaps
  verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: prometheus-system
  namespace: knative-monitoring
  labels:
    serving.knative.dev/release: devel
rules:
- apiGroups: [""]
  resources:
  - nodes
  - services
  - endpoints
  - pods
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources:
  - configmaps
  verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: prometheus-system
  namespace: kube-system
  labels:
    serving.knative.dev/release: devel
rules:
- apiGroups: [""]
  resources:
  - services
  - endpoints
  - pods
  verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: prometheus-system
  namespace: istio-system
  labels:
    serving.knative.dev/release: devel
rules:
- apiGroups: [""]
  resources:
  - nodes/metrics
  - nodes
  - services
  - endpoints
  - pods
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources:
  - configmaps
  verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus-system
  namespace: knative-monitoring
  labels:
    serving.knative.dev/release: devel
rules:
- apiGroups: [""]
  resources:
  - nodes/metrics
  - nodes
  - services
  - endpoints
  - pods
  - nodes/proxy
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources:
  - configmaps
  verbs: ["get"]
- nonResourceURLs: ["/metrics"]
  verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: prometheus-system
  namespace: default
  labels:
    serving.knative.dev/release: devel
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: prometheus-system
subjects:
- kind: ServiceAccount
  name: prometheus-system
  namespace: knative-monitoring
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: prometheus-system
  namespace: knative-monitoring
  labels:
    serving.knative.dev/release: devel
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: prometheus-system
subjects:
- kind: ServiceAccount
  name: prometheus-system
  namespace: knative-monitoring
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: prometheus-system
  namespace: kube-system
  labels:
    serving.knative.dev/release: devel
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: prometheus-system
subjects:
- kind: ServiceAccount
  name: prometheus-system
  namespace: knative-monitoring
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: prometheus-system
  namespace: istio-system
  labels:
    serving.knative.dev/release: devel
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: prometheus-system
subjects:
- kind: ServiceAccount
  name: prometheus-system
  namespace: knative-monitoring
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: prometheus-system
  labels:
    serving.knative.dev/release: devel
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: prometheus-system
subjects:
- kind: ServiceAccount
  name: prometheus-system
  namespace: knative-monitoring
---
apiVersion: v1
kind: Service
metadata:
  name: prometheus-system-np
  namespace: knative-monitoring
  labels:
    serving.knative.dev/release: devel
spec:
  type: NodePort
  selector:
    app: prometheus
  ports:
    - port: 8080
      targetPort: 9090
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: prometheus-system
  namespace: knative-monitoring
  labels:
    serving.knative.dev/release: devel
spec:
  replicas: 2
  podManagementPolicy: Parallel
  serviceName: prometheus-system
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
        serving.knative.dev/release: devel
    spec:
      containers:
      - args:
        - --config.file=/etc/prometheus/prometheus.yml
        - --storage.tsdb.path=/prometheus
        - --storage.tsdb.retention=2d
        - --storage.tsdb.no-lockfile
        - --web.enable-lifecycle
        - --web.route-prefix=/
        image: prom/prometheus:v2.2.1
        imagePullPolicy: IfNotPresent
        name: prometheus
        ports:
        - containerPort: 9090
          name: web
          protocol: TCP
        livenessProbe:
          httpGet:
            path: /-/healthy
            port: web
            scheme: HTTP
          initialDelaySeconds: 30
          failureThreshold: 10
          timeoutSeconds: 3
        readinessProbe:
          httpGet:
            path: /-/ready
            port: web
            scheme: HTTP
          failureThreshold: 10
          timeoutSeconds: 3
        resources:
          limits:
            memory: 1000Mi
          requests:
            memory: 400Mi
        terminationMessagePath: /dev/termination-log
        volumeMounts:
        - name: prometheus-config-volume
          mountPath: /etc/prometheus
        - name: prometheus-storage-volume
          mountPath: /prometheus
      serviceAccountName: prometheus-system
      terminationGracePeriodSeconds: 600
      volumes:
      - name: prometheus-config-volume
        configMap:
          defaultMode: 420
          name: prometheus-scrape-config
      - name: prometheus-storage-volume
        emptyDir: {}
  updateStrategy:
    type: RollingUpdate
