# Public Services — these are what runners connect to.
# They route through HAProxy (buildkitd-lb) for least-connections load balancing.
#
# buildctl --addr tcp://buildkitd-arm64.buildkit:1234 ...  (ARM64 builds)
# buildctl --addr tcp://buildkitd-amd64.buildkit:1234 ...  (x86_64 builds)
# buildctl --addr tcp://buildkitd.buildkit:1234 ...        (any arch)

apiVersion: v1
kind: Service
metadata:
  name: buildkitd-arm64
  namespace: buildkit
  labels:
    app: buildkitd
    arch: arm64
spec:
  type: ClusterIP
  ports:
    - name: buildkit
      port: 1234
      targetPort: 2234  # HAProxy arm64 frontend
      protocol: TCP
  selector:
    app: buildkitd-lb

---
apiVersion: v1
kind: Service
metadata:
  name: buildkitd-amd64
  namespace: buildkit
  labels:
    app: buildkitd
    arch: amd64
spec:
  type: ClusterIP
  ports:
    - name: buildkit
      port: 1234
      targetPort: 3234  # HAProxy amd64 frontend
      protocol: TCP
  selector:
    app: buildkitd-lb

---
# Combined Service — leastconn across all arches.
# Use when the caller doesn't care about the output architecture
# (e.g., building a multi-stage tool image, running tests).
apiVersion: v1
kind: Service
metadata:
  name: buildkitd
  namespace: buildkit
  labels:
    app: buildkitd
spec:
  type: ClusterIP
  ports:
    - name: buildkit
      port: 1234
      targetPort: 1234  # HAProxy combined frontend
      protocol: TCP
  selector:
    app: buildkitd-lb

---
# HAProxy metrics Service — exposes Prometheus endpoint for monitoring.
apiVersion: v1
kind: Service
metadata:
  name: buildkitd-lb-metrics
  namespace: buildkit
  labels:
    app.kubernetes.io/name: buildkitd-lb
    app.kubernetes.io/component: load-balancer
spec:
  type: ClusterIP
  ports:
    - name: metrics
      port: 9404
      targetPort: prometheus
      protocol: TCP
  selector:
    app: buildkitd-lb

---
# Headless Services — used by HAProxy for backend pod discovery via DNS.
# These are not accessed directly by runners.

apiVersion: v1
kind: Service
metadata:
  name: buildkitd-arm64-pods
  namespace: buildkit
  labels:
    app.kubernetes.io/name: buildkitd
    app.kubernetes.io/component: build-service
    arch: arm64
spec:
  clusterIP: None
  ports:
    - name: buildkit
      port: 1234
    - name: metrics
      port: 9090
  selector:
    app: buildkitd
    arch: arm64

---
apiVersion: v1
kind: Service
metadata:
  name: buildkitd-amd64-pods
  namespace: buildkit
  labels:
    app.kubernetes.io/name: buildkitd
    app.kubernetes.io/component: build-service
    arch: amd64
spec:
  clusterIP: None
  ports:
    - name: buildkit
      port: 1234
    - name: metrics
      port: 9090
  selector:
    app: buildkitd
    arch: amd64

---
apiVersion: v1
kind: Service
metadata:
  name: buildkitd-pods
  namespace: buildkit
  labels:
    app.kubernetes.io/name: buildkitd
    app.kubernetes.io/component: build-service
spec:
  clusterIP: None
  ports:
    - name: buildkit
      port: 1234
    - name: metrics
      port: 9090
  selector:
    app: buildkitd
