apiVersion: v1
kind: ConfigMap
metadata:
  name: parca
data:
  # TODO(bwplotka): Add config reloader, otherwise pod has to manually deleted.
  parca.yaml: |-
    "object_storage":
      "bucket":
        "config":
          "directory": "/var/lib/parca"
        "type": "FILESYSTEM"
    scrape_configs:
      - job_name: prometheus
        scrape_interval: 30s
        scrape_timeout: 40s
        scheme: http
        
        kubernetes_sd_configs:
          - role: endpoints
              
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
          insecure_skip_verify: true
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
            
        relabel_configs:
          - action: keep
            source_labels: [__meta_kubernetes_service_label_app]
            regex: prometheus|prometheus-meta
          - source_labels: [__meta_kubernetes_service_label_prometheus]
            target_label: prometheus
          # Each prometheus is configured with external path option, so it serves on certain hardcoded path. Adjust it.
          - source_labels: [__meta_kubernetes_namespace]
            regex: prombench-(.*)
            target_label: pr_number
          - source_labels: [__profile_path__]
            target_label: __init_profile_path
          - source_labels: [__meta_kubernetes_service_label_app, __init_profile_path]
            regex: prometheus-meta;(.*)
            replacement: /prometheus-meta$1
            target_label: __profile_path__
          - source_labels: [prometheus, pr_number, __init_profile_path]
            regex: test-.*;(.*);(.*)
            replacement: /$1/prometheus-release$2
            target_label: __profile_path__
          - source_labels: [prometheus, pr_number, __init_profile_path]
            regex: test-pr-.*;(.*);(.*)
            replacement: /$1/prometheus-pr$2
            target_label: __profile_path__
          - regex: __init_profile_path
            action: labeldrop
          - source_labels: [__address__]
            target_label: instance
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: parca
  labels:
    app: parca
spec:
  replicas: 1
  selector:
    matchLabels:
      app: parca
  template:
    metadata:
      labels:
        app: parca
    spec:
      serviceAccountName: parca
      securityContext:
        fsGroup: 65534
        runAsUser: 65534
      containers:
      - image: ghcr.io/parca-dev/parca:v0.18.0
        args:
          - /parca
          - "--http-address=:7070"
          - "--config-path=/etc/parca/parca.yaml"
          - "--path-prefix=/profiles"
          - "--log-level=info"
          - "--cors-allowed-origins=*"
          - "--debuginfod-upstream-servers=https://debuginfod.systemtap.org"
          - "--debuginfod-http-request-timeout=5m"
        name: parca
        terminationMessagePolicy: FallbackToLogsOnError
        volumeMounts:
        - mountPath: /etc/parca
          name: config
        - mountPath: /var/lib/parca
          name: storage
        ports:
        - name: parca-web
          containerPort: 7070
      volumes:
      - name: config
        configMap:
          name: parca
      # TODO(bwplotka): Make it persistent at some point.
      - name: storage
        emptyDir: {}
      terminationGracePeriodSeconds: 120
      nodeSelector:
        node-name: main-node
---
apiVersion: v1
kind: Service
metadata:
  name: parca
  labels:
    app: parca
spec:
  type: NodePort
  ports:
  - name: parca-web
    port: 80
    targetPort: parca-web
  selector:
    app: parca
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-parca
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - http:
      paths:
      - backend:
          service:
            name: parca
            port:
              name: parca-web
        path: /profiles
        pathType: Prefix