{{/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}

{{- define "cyborg.agent.daemonset" }}
{{- $daemonset := index . 0 }}
{{- $configMapName := index . 1 }}
{{- $serviceAccountName := index . 2 }}
{{- $envAll := index . 3 }}
{{- with $envAll }}

{{- $mounts_cyborg := .Values.pod.mounts.cyborg_agent.cyborg_agent }}
{{- $mounts_cyborg_agent_init := .Values.pod.mounts.cyborg_agent.init_container }}

---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: cyborg-agent
  annotations:
    {{ tuple $envAll | include "helm-toolkit.snippets.release_uuid" }}
  labels:
{{ tuple $envAll .Chart.Name $daemonset | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 4 }}
spec:
  selector:
    matchLabels:
{{ tuple $envAll .Chart.Name $daemonset | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 6 }}
{{ tuple $envAll $daemonset | include "helm-toolkit.snippets.kubernetes_upgrades_daemonset" | indent 2 }}
  template:
    metadata:
      labels:
{{ tuple $envAll .Chart.Name $daemonset | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 8 }}
      annotations:
{{ tuple $envAll | include "helm-toolkit.snippets.release_uuid" | indent 8 }}
        configmap-bin-hash: {{ tuple "configmap-bin.yaml" . | include "helm-toolkit.utils.hash" }}
{{ tuple "cyborg_agent" . | include "helm-toolkit.snippets.custom_pod_annotations" | indent 8 }}
    spec:
      serviceAccountName: {{ $serviceAccountName }}
      nodeSelector:
        {{ .Values.labels.agent.node_selector_key }}: {{ .Values.labels.agent.node_selector_value }}
      hostNetwork: true
      hostPID: true
      hostIPC: true
      dnsPolicy: ClusterFirstWithHostNet
      initContainers:
{{ tuple $envAll "cyborg" $mounts_cyborg_agent_init | include "helm-toolkit.snippets.kubernetes_entrypoint_init_container" | indent 8 }}
      containers:
        - name: cyborg-agent
{{ tuple $envAll "cyborg_agent" | include "helm-toolkit.snippets.image" | indent 10 }}
{{ tuple $envAll $envAll.Values.pod.resources.agent | include "helm-toolkit.snippets.kubernetes_resources" | indent 10 }}
          securityContext:
            runAsUser: 0
            privileged: true
          env:
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: status.hostIP
          command:
            - /tmp/cyborg-agent.sh
          volumeMounts:
            - name: pod-tmp
              mountPath: /tmp
            - name: oslo-lock-path
              mountPath: {{ .Values.conf.cyborg.oslo_concurrency.lock_path }}
            - name: cyborg-bin
              mountPath: /tmp/cyborg-agent.sh
              subPath: cyborg-agent.sh
              readOnly: true
            - name: cyborg-etc
              mountPath: /etc/cyborg/cyborg.conf
              subPath: cyborg.conf
              readOnly: true
      volumes:
        - name: pod-tmp
          emptyDir: {}
        - name: oslo-lock-path
          emptyDir: {}
        - name: pod-shared
          emptyDir: {}
        - name: cyborg-bin
          configMap:
            name: cyborg-bin
            defaultMode: 0555
        - name: cyborg-etc
          secret:
            secretName: cyborg-etc
            defaultMode: 0444
{{- end }}
{{- end }}

{{- if .Values.manifests.daemonset_agent }}
{{- $envAll := . }}
{{- $daemonset := "agent" }}
{{- $configMapName := "cyborg-etc" }}
{{- $serviceAccountName := "cyborg-agent" }}

{{- $dependencyOpts := dict "envAll" $envAll "dependencyMixinParam" $envAll.Values.network.backend "dependencyKey" "agent" -}}
{{- $_ := include "helm-toolkit.utils.dependency_resolver" $dependencyOpts | toString | fromYaml }}

{{ tuple $envAll "pod_dependency" $serviceAccountName | include "helm-toolkit.snippets.kubernetes_pod_rbac_serviceaccount" }}
{{- $daemonset_yaml := list $daemonset $configMapName $serviceAccountName . | include "cyborg.agent.daemonset" | toString | fromYaml }}
{{- $configmap_yaml := "cyborg.etc" }}
{{- list $daemonset $daemonset_yaml $configmap_yaml $configMapName . | include "helm-toolkit.utils.daemonset_overrides" }}
{{- end }}
