openapi: 3.0.3
info:
  title: Shoehorn Kubernetes API
  description: K8s agent registration, workload push, heartbeat, and network flows.
  version: 1.0.0

servers:
  - url: https://shoehorn.example.com/api/v1
    description: Production

paths:
  /k8s/agents:
    get:
      summary: List registered K8s agents
      tags: [K8s Agents]
      responses:
        '200':
          description: List of registered agents
          content:
            application/json:
              schema:
                type: object
                properties:
                  agents:
                    type: array
                    items: { $ref: '#/components/schemas/Agent' }

  /k8s/agents/register:
    post:
      summary: Register a new K8s agent
      description: Registers a cluster and returns an agent token for push operations.
      tags: [K8s Agents]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [cluster_id, display_name]
              properties:
                cluster_id: { type: string }
                display_name: { type: string }
                environment: { type: string }
                region: { type: string }
      responses:
        '201':
          description: Agent registered
          content:
            application/json:
              schema:
                type: object
                properties:
                  cluster_id: { type: string }
                  token: { type: string, description: Bearer token for push API }

  /k8s/agents/{cluster_id}:
    get:
      summary: Get agent details
      tags: [K8s Agents]
      parameters:
        - name: cluster_id
          in: path
          required: true
          schema: { type: string }
      responses:
        '200':
          description: Agent details

    delete:
      summary: Delete agent and all associated data
      tags: [K8s Agents]
      parameters:
        - name: cluster_id
          in: path
          required: true
          schema: { type: string }
      responses:
        '204':
          description: Agent deleted

  /k8s/agents/{cluster_id}/rotate:
    post:
      summary: Rotate agent token
      description: Generates a new token and invalidates the previous one.
      tags: [K8s Agents]
      parameters:
        - name: cluster_id
          in: path
          required: true
          schema: { type: string }
      responses:
        '200':
          description: New token generated
          content:
            application/json:
              schema:
                type: object
                properties:
                  token: { type: string }

  /k8s/agents/{cluster_id}/revoke:
    post:
      summary: Revoke agent token
      description: Revokes the agent token without deleting the agent record.
      tags: [K8s Agents]
      parameters:
        - name: cluster_id
          in: path
          required: true
          schema: { type: string }
      responses:
        '200':
          description: Token revoked

  /k8s/agents/push:
    post:
      summary: Push workload data from agent
      description: |
        Called by the K8s agent to push discovered workloads. Authenticated
        via agent bearer token (not session auth).
      tags: [K8s Agent Push]
      security:
        - AgentToken: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [cluster_id, workloads]
              properties:
                cluster_id: { type: string }
                workloads:
                  type: array
                  items: { $ref: '#/components/schemas/Workload' }
      responses:
        '200':
          description: Workloads received

  /k8s/agents/heartbeat:
    post:
      summary: Agent heartbeat
      description: Called periodically by the agent to indicate it is alive.
      tags: [K8s Agent Push]
      security:
        - AgentToken: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [cluster_id]
              properties:
                cluster_id: { type: string }
                agent_version: { type: string }
                node_count: { type: integer }
                pod_count: { type: integer }
      responses:
        '200':
          description: Heartbeat acknowledged

  /k8s/agents/{clusterId}/network-flows:
    post:
      summary: Push network flow data
      tags: [K8s Agent Push]
      security:
        - AgentToken: []
      parameters:
        - name: clusterId
          in: path
          required: true
          schema: { type: string }
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                flows:
                  type: array
                  items:
                    type: object
                    properties:
                      source: { type: string }
                      destination: { type: string }
                      port: { type: integer }
                      protocol: { type: string }
                      bytes_sent: { type: integer }
      responses:
        '200':
          description: Network flows received

components:
  schemas:
    Agent:
      type: object
      properties:
        cluster_id: { type: string }
        display_name: { type: string }
        environment: { type: string }
        region: { type: string }
        status: { type: string, enum: [active, stale, offline] }
        last_heartbeat: { type: string, format: date-time }
        agent_version: { type: string }
        node_count: { type: integer }
        workload_count: { type: integer }
        created_at: { type: string, format: date-time }

    Workload:
      type: object
      properties:
        name: { type: string }
        namespace: { type: string }
        kind: { type: string, enum: [Deployment, StatefulSet, DaemonSet, CronJob, Job] }
        labels: { type: object, additionalProperties: { type: string } }
        annotations: { type: object, additionalProperties: { type: string } }
        replicas: { type: integer }
        ready_replicas: { type: integer }
        images:
          type: array
          items: { type: string }
        conditions:
          type: array
          items:
            type: object
            properties:
              type: { type: string }
              status: { type: string }
              reason: { type: string }

  securitySchemes:
    AgentToken:
      type: http
      scheme: bearer
      description: Agent bearer token returned during registration
