apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: kagent
  namespace: kagent
  labels:
    app: kagent
    managed-by: vapora
spec:
  serviceName: kagent
  replicas: 3
  selector:
    matchLabels:
      app: kagent
  template:
    metadata:
      labels:
        app: kagent
        agent: "true"
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9090"
        prometheus.io/path: "/metrics"
    spec:
      serviceAccountName: kagent
      securityContext:
        runAsNonRoot: true
        runAsUser: 1000
        fsGroup: 1000

      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchExpressions:
                    - key: app
                      operator: In
                      values:
                        - kagent
                topologyKey: kubernetes.io/hostname

      containers:
        - name: kagent
          image: google-kagent:latest
          imagePullPolicy: IfNotPresent

          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
            - name: grpc
              containerPort: 50051
              protocol: TCP
            - name: metrics
              containerPort: 9090
              protocol: TCP

          env:
            - name: KAGENT_CONFIG
              value: /etc/kagent/kagent.config.yaml
            - name: KAGENT_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: KAGENT_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: A2A_SERVER_URL
              value: "http://vapora-a2a:8003"
            - name: LOG_LEVEL
              value: info

          volumeMounts:
            - name: config
              mountPath: /etc/kagent
              readOnly: true
            - name: cache
              mountPath: /var/cache/kagent
            - name: tmp
              mountPath: /tmp

          resources:
            requests:
              cpu: "500m"
              memory: "512Mi"
            limits:
              cpu: "2000m"
              memory: "2Gi"

          livenessProbe:
            httpGet:
              path: /health
              port: http
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3

          readinessProbe:
            httpGet:
              path: /ready
              port: http
            initialDelaySeconds: 10
            periodSeconds: 5
            timeoutSeconds: 3
            failureThreshold: 2

          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop:
                - ALL

      volumes:
        - name: config
          configMap:
            name: kagent-config
            defaultMode: 0444
        - name: cache
          emptyDir:
            sizeLimit: 1Gi
        - name: tmp
          emptyDir:
            sizeLimit: 500Mi

  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 10Gi
