apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: diagnosis-query
  name: diagnosis-query
  namespace: observability
spec:
  replicas: 1
  selector:
    matchLabels:
      app: diagnosis-query
  template:
    metadata:
      labels:
        app: diagnosis-query
    spec:
      containers:
      - args:
        - -vendor
        - azure
        - -region
        - eastus2
        - -duckDBSwitchAllEnabled=true
        - -duckDBSwitchTime
        - "1748736001"
        command:
        - /diagnosis-query
        env:
        - name: TOPSQL_OLD_CLUSTERS
          value: ""
        - name: POD_MEMORY_LIMITS_MB
          valueFrom:
            resourceFieldRef:
              containerName: diagnosis-query
              resource: limits.memory
              divisor: 1Mi
        - name: DUCKDB_MEMORY_PERCENTAGE
          value: "80"
        - name: AZURE_TENANT_ID
          value: 6c865c34-8ff8-4792-bbca-8c9d15efb29d
        - name: AZURE_CLIENT_ID
          value: 1d1a31c8-a968-4924-8917-a01a32a5107d
        - name: AZURE_FEDERATED_TOKEN_FILE
          value: /var/run/secrets/azure/tokens/azure-identity-token
        - name: AZURE_AUTHORITY_HOST
          value: https://login.microsoftonline.com/
        - name: AZURE_STORAGE_ACCOUNT
          value: "o11yprodeastus2"
        - name: AZURE_LOG_LEVEL
          value: "verbose"
        image: o11yprod.azurecr.io/diagnosis-query:v111
        imagePullPolicy: Always
        name: diagnosis-query
        ports:
        - containerPort: 8081
          protocol: TCP
        resources:
          limits:
            cpu: "2"
            memory: 4Gi
          requests:
            cpu: "1"
            memory: 2Gi
        volumeMounts:
        - mountPath: /cache
          name: diagnosis-query-cache
        - mountPath: /var/run/secrets/azure/tokens
          name: azure-identity-token
          readOnly: true
      serviceAccount: regional-server-controller-manager
      serviceAccountName: regional-server-controller-manager
      volumes:
      - name: azure-identity-token
        projected:
          defaultMode: 420
          sources:
          - serviceAccountToken:
              audience: api://AzureADTokenExchange
              expirationSeconds: 3600
              path: azure-identity-token
  volumeClaimTemplates:
  - apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      labels:
        app: diagnosis-query
      name: diagnosis-query-cache
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 50Gi
      storageClassName: prometheus.ssd
      volumeMode: Filesystem

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: diagnosis-query
  name: diagnosis-query
  namespace: observability
spec:
  selector:
    app: diagnosis-query
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 8081

---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    release: self-monitoring
  name: diagnosis-query-monitor
  namespace: self-monitoring
spec:
  endpoints:
    - path: /metrics
      targetPort: 8081
  namespaceSelector:
    matchNames:
      - observability
  selector:
    matchLabels:
      app: diagnosis-query
