# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

{{- if .Values.rbac.create }}
{{- if .Values.rbac.namespaceRestricted }}
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ include "snapshot.fullname" . }}-agent
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "snapshot.labels" . | nindent 4 }}
    app.kubernetes.io/component: checkpoint-agent
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ include "snapshot.fullname" . }}-agent
subjects:
  - kind: ServiceAccount
    name: {{ include "snapshot.serviceAccountName" . }}
    namespace: {{ .Release.Namespace }}
---
# Bind agent to ClusterRole for cluster-scoped ResourceSlices (DRA GPU UUID lookup)
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ include "snapshot.fullname" . }}-agent-resourceslices
  labels:
    {{- include "snapshot.labels" . | nindent 4 }}
    app.kubernetes.io/component: checkpoint-agent
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ include "snapshot.fullname" . }}-agent-resourceslices
subjects:
  - kind: ServiceAccount
    name: {{ include "snapshot.serviceAccountName" . }}
    namespace: {{ .Release.Namespace }}
{{- else }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ include "snapshot.fullname" . }}-agent
  labels:
    {{- include "snapshot.labels" . | nindent 4 }}
    app.kubernetes.io/component: checkpoint-agent
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ include "snapshot.fullname" . }}-agent
subjects:
  - kind: ServiceAccount
    name: {{ include "snapshot.serviceAccountName" . }}
    namespace: {{ .Release.Namespace }}
{{- end }}
{{- end }}
