{{$PROMETHEUS_SCRAPE_ETCD := DefaultParam .PROMETHEUS_SCRAPE_ETCD false}}
{{$PROMETHEUS_SCRAPE_MASTER_KUBELETS := DefaultParam .PROMETHEUS_SCRAPE_MASTER_KUBELETS false}}
{{$PROMETHEUS_SCRAPE_NODE_EXPORTER := DefaultParam .PROMETHEUS_SCRAPE_NODE_EXPORTER false}}

# If kube-apiserver is overloaded, we might get slower responses.
{{$PROMETHEUS_SLOW_APISERVER := DefaultParam .CL2_PROMETHEUS_SLOW_APISERVER false}}

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    k8s-app: master
  name: master
  namespace: monitoring
spec:
  endpoints:
  {{if $PROMETHEUS_SCRAPE_ETCD}}
  - interval: 5s
    port: etcd-2379
  - interval: 5s
    port: etcd-2382
  {{end}}
  {{if $PROMETHEUS_SCRAPE_NODE_EXPORTER}}
  # TODO(mborsz): Debug why node-exporter is that slow and change interval back to 5s.
  - interval: 30s
    port: node-exporter
    scrapeTimeout: 20s
  {{end}}
  - port: apiserver
    scheme: https
    bearerTokenFile: "/var/run/secrets/kubernetes.io/serviceaccount/token"
    tlsConfig:
      insecureSkipVerify: true
      caFile: "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
  {{if $PROMETHEUS_SLOW_APISERVER}}
    interval: 30s
    scrapeTimeout: 30s
  {{else}}
    interval: 5s
  {{end}}
    bearerTokenSecret:
      name: prometheus-token
      key: token
  {{if $PROMETHEUS_SCRAPE_MASTER_KUBELETS}}
  - interval: 5s
    port: kubelet
    scheme: https
    tlsConfig:
      insecureSkipVerify: true
    bearerTokenSecret:
      name: prometheus-token
      key: token
  - interval: 5s
    port: kubelet
    path: /metrics/cadvisor
    scheme: https
    tlsConfig:
      insecureSkipVerify: true
    bearerTokenSecret:
      name: prometheus-token
      key: token
  {{end}}
  - interval: 5s
    port: kube-scheduler
    scheme: https
    tlsConfig:
      insecureSkipVerify: true
    bearerTokenSecret:
      name: prometheus-token
      key: token
  - interval: 5s
    port: kube-controller-manager
    scheme: https
    tlsConfig:
      insecureSkipVerify: true
    bearerTokenSecret:
      name: prometheus-token
      key: token
  selector:
    matchLabels:
      k8s-app: master