{{- if and (not .Values.secret.existingSecret) (not .Values.env.KEY) }}
{{- fail "env.KEY is required when not using an existingSecret. Please provide an SSH public key for agent authentication." }}
{{- end }}
{{- if and .Values.secret.existingSecret .Values.env.KEY }}
{{- fail "Cannot use both existingSecret and env.KEY. Please choose one method for providing authentication credentials." }}
{{- end }}
{{- if not .Values.secret.existingSecret }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "beszel-agent.fullname" . }}
  labels:
    {{- include "beszel-agent.labels" . | nindent 4 }}
type: Opaque
data:
  {{ .Values.secret.sshKey }}: {{ .Values.env.KEY | b64enc | quote }}
  {{- if .Values.env.TOKEN }}
  {{ .Values.secret.tokenKey }}: {{ .Values.env.TOKEN | b64enc | quote }}
  {{- end }}
{{- end }}
