# Example IceHive controller Deployment (reference wiring).
#
# Before applying:
#   1. Deploy MySQL (with an empty database) and RabbitMQ reachable from the cluster,
#      or point the mysql / worker_bootstrap hostnames below at existing instances.
#   2. Replace the image tag (if needed), CHANGEME_* passwords, and hostnames.
#   3. Apply: kubectl apply -f kubernetes-icehive.yaml
#
# Collectors and persisters use the same container image; set ICEHIVE_SERVICE and
# ICEHIVE_CONTROLLER_URL=http://icehive-controller.icehive.svc.cluster.local:8080
# on additional Deployments (see docs/content/deployment/kubernetes.md).

---
apiVersion: v1
kind: Namespace
metadata:
  name: icehive
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: icehive-controller-config
  namespace: icehive
data:
  config.yaml: |
    listen: "0.0.0.0:8080"

    worker_bootstrap:
      amqp:
        url: "amqp://guest:guest@rabbitmq.icehive.svc.cluster.local:5672/"

    mysql:
      host: "mysql-controller.icehive.svc.cluster.local"
      port: 3306
      user: "icehive"
      password: "CHANGEME_CONTROLLER_DB_PASSWORD"
      database: "icehive"

    persister_mysql:
      host: "mysql-sink.icehive.svc.cluster.local"
      port: 3306
      user: "icehive_sink"
      password: "CHANGEME_SINK_DB_PASSWORD"
      database: "icehive_sink"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: icehive-controller
  namespace: icehive
  labels:
    app.kubernetes.io/name: icehive-controller
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: icehive-controller
  template:
    metadata:
      labels:
        app.kubernetes.io/name: icehive-controller
    spec:
      containers:
        - name: controller
          image: ghcr.io/jamesread/icehive:latest
          imagePullPolicy: Always
          env:
            - name: ICEHIVE_SERVICE
              value: controller
            - name: LOG_LEVEL
              value: info
          args:
            - -configdir
            - /etc/icehive
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          readinessProbe:
            httpGet:
              path: /healthz
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8080
            initialDelaySeconds: 15
            periodSeconds: 20
          volumeMounts:
            - name: config
              mountPath: /etc/icehive/config.yaml
              subPath: config.yaml
              readOnly: true
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
            limits:
              cpu: "1"
              memory: 512Mi
      volumes:
        - name: config
          configMap:
            name: icehive-controller-config
---
apiVersion: v1
kind: Service
metadata:
  name: icehive-controller
  namespace: icehive
  labels:
    app.kubernetes.io/name: icehive-controller
spec:
  type: ClusterIP
  selector:
    app.kubernetes.io/name: icehive-controller
  ports:
    - name: http
      port: 8080
      targetPort: http
      protocol: TCP
# Example Prometheus alert rules for IceHive pipeline health.
# Apply separately when Prometheus Operator is available (adjust namespace/labels as needed).
#
# apiVersion: monitoring.coreos.com/v1
# kind: PrometheusRule
# metadata:
#   name: icehive-alerts
#   namespace: icehive
# spec:
#   groups:
#     - name: icehive.pipeline
#       rules:
#         - alert: IcehiveGitRepoPersistStalled
#           expr: |
#             increase(icehive_collector_entities_published_total{entity_type="GitRepo"}[1h]) > 0
#             and increase(icehive_persister_entities_total{entity_type="GitRepo",outcome="success"}[1h]) == 0
#           for: 15m
#           labels:
#             severity: warning
#           annotations:
#             summary: GitHub repos published but not persisted
#             description: collector-github published GitRepo entities in the last hour but persister-mysql recorded no successful upserts.
#         - alert: IcehiveCollectionSourceStale
#           expr: icehive_controller_collection_source_stale == 1
#           for: 30m
#           labels:
#             severity: warning
#           annotations:
#             summary: IceHive collection source is stale
#             description: A scheduled collection source has not succeeded within twice its cron interval.
#         - alert: IcehiveEntityFreshnessStale
#           expr: icehive_controller_entity_freshness_age_seconds{entity_type="GitRepo"} > 86400
#           for: 30m
#           labels:
#             severity: warning
#           annotations:
#             summary: GitRepo entity data is stale in the sink database
#             description: No GitRepo row has been updated in the persister MySQL sink for over 24 hours.
