---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ include "falcosidekick.fullname" . }}
  namespace: {{ .Release.Namespace }}
  {{- if and .Values.config.aws.useirsa .Values.config.aws.rolearn }}
  annotations:
    eks.amazonaws.com/role-arn: {{ .Values.config.aws.rolearn }}
  {{- end }}
  labels:
    {{- include "falcosidekick.labels" . | nindent 4 }}
    app.kubernetes.io/component: core
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: {{ include "falcosidekick.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "falcosidekick.labels" . | nindent 4 }}
    app.kubernetes.io/component: core
rules:
- apiGroups:
    - ""
  resources:
    - endpoints
  verbs:
    - get
{{- if .Values.podSecurityPolicy.create }}
- apiGroups:
    - policy
  resources:
    - podsecuritypolicies
  resourceNames:
    - {{ template "falcosidekick.fullname" . }}
  verbs:
    - use
{{- end }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ include "falcosidekick.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "falcosidekick.labels" . | nindent 4 }}
    app.kubernetes.io/component: core
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: {{ include "falcosidekick.fullname" . }}
subjects:
- kind: ServiceAccount
  name: {{ include "falcosidekick.fullname" . }}
{{- if .Values.config.policyreport.enabled  }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ include "falcosidekick.fullname" . }}
  labels:
    {{- include "falcosidekick.labels" . | nindent 4 }}
    app.kubernetes.io/component: core
rules:
- apiGroups:
    - "wgpolicyk8s.io"
  resources:
    - policyreports
    - clusterpolicyreports
  verbs:
    - get
    - create
    - delete
    - update
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ include "falcosidekick.fullname" . }}
  labels:
    {{- include "falcosidekick.labels" . | nindent 4 }}
    app.kubernetes.io/component: core
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ include "falcosidekick.fullname" . }}
subjects:
- kind: ServiceAccount
  namespace: {{ .Release.Namespace }}
  name: {{ include "falcosidekick.fullname" . }}
{{- end }}
