# ServiceAccount and RBAC for ARC Runners in Kubernetes Mode
# Allows runner pods to create job containers as separate pods

apiVersion: v1
kind: ServiceAccount
metadata:
  name: arc-runner
  namespace: arc-runners
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: arc-runner
  namespace: arc-runners
rules:
  # Allow runners to create and manage pods for job containers
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["create", "delete", "get", "list", "watch"]
  - apiGroups: [""]
    resources: ["pods/log"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["pods/exec"]
    verbs: ["create", "get"]
  - apiGroups: [""]
    resources: ["pods/status"]
    verbs: ["get"]
  # Allow runners to create and delete secrets (for workflow secrets and cleanup)
  # Note: "list" is required by runner-container-hooks preflight permission check
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["create", "delete", "get", "list"]
  # Allow runners to create configmaps (for step outputs/caching)
  - apiGroups: [""]
    resources: ["configmaps"]
    verbs: ["create", "get", "list"]
  # Allow runners to create and manage jobs (for Kubernetes mode job containers)
  - apiGroups: ["batch"]
    resources: ["jobs"]
    verbs: ["create", "delete", "get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: arc-runner
  namespace: arc-runners
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: arc-runner
subjects:
  - kind: ServiceAccount
    name: arc-runner
    namespace: arc-runners
---
# ClusterRole for reading node objects (cluster-scoped resource).
# Required by runner-container-hooks to poll startup taints before creating
# workflow pods (prevents Karpenter-scheduler deadlock on fresh nodes).
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: arc-runner-node-reader
rules:
  - apiGroups: [""]
    resources: ["nodes"]
    verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: arc-runner-node-reader
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: arc-runner-node-reader
subjects:
  - kind: ServiceAccount
    name: arc-runner
    namespace: arc-runners
---
# ServiceAccount for workflow job pods (untrusted user code)
# Intentionally has NO Role or RoleBinding — job pods get zero Kubernetes API permissions.
apiVersion: v1
kind: ServiceAccount
metadata:
  name: arc-workflow
  namespace: arc-runners
