apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus-server
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      # Used to match pre-existing pods that may be affected during updates.
      run: prometheus-server
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  # Pod template.
  template:
    metadata:
      labels:
        # Note: run=prometheus-server should match a service config with a
        # public IP and port so that it is publically accessible.
        run: prometheus-server
      annotations:
        # Tell prometheus service discovery to scrape the pod containers.
        prometheus.io/scrape: 'true'
    spec:
      # References a service account with RBAC permissions for accessing node
      # metrics. This is required for k8s version 1.6+.
      serviceAccountName: prometheus

      # Clusters running a prometheus instance must label nodes exclusively for
      # use by prometheus. See README for steps to create a GKE cluster for
      # prometheus.
      nodeSelector:
        prometheus-node: 'true'

      # When prometheus receives SIGTERM, it begins a new checkpoint. This can
      # take longer than the default grace period of 30s.
      terminationGracePeriodSeconds: 240

      # Place the pod into the Guaranteed QoS by setting equal resource
      # requests and limits for *all* containers in the pod.
      # For more background, see:
      # https://github.com/kubernetes/community/blob/master/contributors/design-proposals/resource-qos.md
      containers:
      # Check https://hub.docker.com/r/prom/prometheus/tags/ for the current
      # stable version.
      - image: prom/prometheus:v2.31.1
        # Note: the container name appears to be ignored and the actual pod name
        # is derived from the Deployment.metadata.name. However, removing this
        # value results in a configuration error.
        name: prometheus
        # Note: Set retention time to 120 days. (default retention is 30d).
        args: ["--config.file=/etc/prometheus/prometheus.yml",
               "--storage.tsdb.path=/prometheus",
               "--storage.tsdb.retention.time=120d",
               "--web.enable-lifecycle",
               "--web.external-url=https://prometheus.{{GCLOUD_PROJECT}}.measurementlab.net"]
        ports:
          - containerPort: 9090
        resources:
          requests:
            memory: "{{PROMETHEUS_RAM}}"
            cpu: "{{PROMETHEUS_CPU}}"
          limits:
            memory: "{{PROMETHEUS_RAM}}"
            cpu: "{{PROMETHEUS_CPU}}"
        volumeMounts:
        # /prometheus stores all metric data. Declared as VOLUME in base image.
        - mountPath: /prometheus
          name: prometheus-storage
          subPath: prometheus-data2
        # /federation-targets should contain federation target config files.
        - mountPath: /federation-targets
          name: prometheus-storage
          subPath: federation-targets
        # /blackbox-targets should contain blackbox target config files.
        - mountPath: /blackbox-targets
          name: prometheus-storage
          subPath: blackbox-targets
        # /blackbox-targets-ipv6 should contain blackbox IPv6 target files.
        - mountPath: /blackbox-targets-ipv6
          name: prometheus-storage
          subPath: blackbox-targets-ipv6
        # /aeflex-targets should contain AppEngine target config files.
        - mountPath: /aeflex-targets
          name: prometheus-storage
          subPath: aeflex-targets
        - mountPath: /script-targets-federation
          name: prometheus-storage
          subPath: script-targets-federation
        # /bmc-targets should contain BMC target config files.
        - mountPath: /bmc-targets
          name: prometheus-storage
          subPath: bmc-targets
        - mountPath: /switch-monitoring-targets
          name: prometheus-storage
          subPath: switch-monitoring-targets
        # /etc/prometheus/prometheus.yml contains the M-Lab Prometheus config.
        - mountPath: /etc/prometheus
          name: prometheus-config
      - image: measurementlab/gcp-service-discovery:v1.5.1
        name: service-discovery
        args: [ "--aef-target=/targets/aeflex-targets/aeflex.json",
                "--gke-target=/targets/federation-targets/prometheus-clusters.json",
                "--http-target=/targets/blackbox-targets/ssh.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/blackbox-targets/ssh.json",
                "--http-target=/targets/blackbox-targets-ipv6/ssh_ipv6.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/blackbox-targets-ipv6/ssh_ipv6.json",
                # NOTE: the output directory is different to use different target service.
                "--http-target=/targets/script-targets-federation/ndt5_client.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/script-targets/ndt5_client.json",
                "--http-target=/targets/script-targets-federation/wehe_client.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/script-targets/wehe_client.json",
                "--http-target=/targets/blackbox-targets/ndt_raw.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/blackbox-targets/ndt_raw.json",
                "--http-target=/targets/blackbox-targets-ipv6/ndt_raw_ipv6.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/blackbox-targets-ipv6/ndt_raw_ipv6.json",
                "--http-target=/targets/blackbox-targets/ndt_ssl.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/blackbox-targets/ndt_ssl.json",
                "--http-target=/targets/blackbox-targets-ipv6/ndt_ssl_ipv6.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/blackbox-targets-ipv6/ndt_ssl_ipv6.json",
                "--http-target=/targets/blackbox-targets/ndt7.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/blackbox-targets/ndt7.json",
                "--http-target=/targets/blackbox-targets-ipv6/ndt7_ipv6.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/blackbox-targets-ipv6/ndt7_ipv6.json",
                "--http-target=/targets/blackbox-targets/neubot.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/blackbox-targets/neubot.json",
                "--http-target=/targets/blackbox-targets-ipv6/neubot_ipv6.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/blackbox-targets-ipv6/neubot_ipv6.json",
                "--http-target=/targets/blackbox-targets/neubot_tls.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/blackbox-targets/neubot_tls.json",
                "--http-target=/targets/blackbox-targets-ipv6/neubot_tls_ipv6.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/blackbox-targets-ipv6/neubot_tls_ipv6.json",
                "--http-target=/targets/blackbox-targets/switches_ping.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/blackbox-targets/switches_ping.json",
                "--http-target=/targets/bmc-targets/bmc_e2e.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/bmc-targets/bmc_e2e.json",
                "--http-target=/targets/switch-monitoring-targets/switch-monitoring.json",
                "--http-source=https://storage.googleapis.com/operator-{{GCLOUD_PROJECT}}/prometheus/switch-monitoring-targets/switch-monitoring.json",
                "--project={{GCLOUD_PROJECT}}"]
        ports:
          - containerPort: 9373
        resources:
          requests:
            memory: "150Mi"
            cpu: "150m"
          limits:
            memory: "150Mi"
            cpu: "150m"
        volumeMounts:
        # Mount the the prometheus-storage for write access to the target
        # directories.
        - mountPath: /targets
          name: prometheus-storage

      # Check https://hub.docker.com/r/jimmidyson/configmap-reload/tags/ for the current
      # stable version.
      - image: jimmidyson/configmap-reload:v0.3.0
        name: configmap-reload
        args: ["-webhook-url", "http://prometheus-service.default.svc.cluster.local:9090/-/reload",
               "-volume-dir", "/prometheus-config"]
        env:
        - name: PROM_RELOAD_URL
          valueFrom:
            configMapKeyRef:
              name: configmap-reload-urls
              key: prometheus_reload_url
        resources:
          requests:
            memory: "400Mi"
            cpu: "200m"
          limits:
            memory: "400Mi"
            cpu: "200m"
        volumeMounts:
        # Mount the prometheus config volume so we can watch it for changes.
        - mountPath: /prometheus-config
          name: prometheus-config

      # Disks created manually, can be named here explicitly using
      # gcePersistentDisk instead of the persistentVolumeClaim.
      volumes:
      - name: prometheus-storage
        persistentVolumeClaim:
          claimName: auto-prometheus-disk0
      - name: prometheus-config
        configMap:
          name: prometheus-federation-config
