# PostSync hook to patch Homarr deployment probes
# Chart v8.23.0 doesn't support probe customization via values
apiVersion: batch/v1
kind: Job
metadata:
  name: homarr-fix-probes
  namespace: dashboard
  annotations:
    argocd.argoproj.io/hook: PostSync
    argocd.argoproj.io/hook-delete-policy: BeforeHookCreation
spec:
  backoffLimit: 3
  template:
    spec:
      restartPolicy: Never
      serviceAccountName: homarr-probe-patcher
      containers:
      - name: patch
        image: python:3.12-alpine
        command:
        - /bin/sh
        - -c
        - |
          set -e
          echo "Installing kubectl..."
          wget -q -O /tmp/kubectl https://dl.k8s.io/release/v1.28.0/bin/linux/amd64/kubectl
          chmod +x /tmp/kubectl
          
          echo "Patching Homarr deployment probes..."
          /tmp/kubectl -n dashboard patch deployment homarr --type=json -p='[
            {
              "op": "replace",
              "path": "/spec/template/spec/containers/0/livenessProbe/initialDelaySeconds",
              "value": 60
            },
            {
              "op": "replace",
              "path": "/spec/template/spec/containers/0/livenessProbe/periodSeconds",
              "value": 30
            },
            {
              "op": "replace",
              "path": "/spec/template/spec/containers/0/livenessProbe/timeoutSeconds",
              "value": 5
            },
            {
              "op": "replace",
              "path": "/spec/template/spec/containers/0/readinessProbe/initialDelaySeconds",
              "value": 45
            },
            {
              "op": "replace",
              "path": "/spec/template/spec/containers/0/readinessProbe/periodSeconds",
              "value": 15
            },
            {
              "op": "replace",
              "path": "/spec/template/spec/containers/0/readinessProbe/timeoutSeconds",
              "value": 5
            }
          ]'
          
          echo "✅ Probes patched successfully"
          echo "  Liveness: 60s initial, 30s period, 5s timeout"
          echo "  Readiness: 45s initial, 15s period, 5s timeout"
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: homarr-probe-patcher
  namespace: dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: homarr-probe-patcher
  namespace: dashboard
rules:
- apiGroups: ["apps"]
  resources: ["deployments"]
  verbs: ["get", "patch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: homarr-probe-patcher
  namespace: dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: homarr-probe-patcher
subjects:
- kind: ServiceAccount
  name: homarr-probe-patcher
  namespace: dashboard
