apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{k8s_deploy.name}}
  namespace: {{k8s_deploy.namespace}}
  labels:
    {%- for label in k8s_deploy.labels %}
    {{label.key}}: {{label.value}}
    {%- endfor %}
spec:
  replicas: {{k8s_deploy.spec.replicas}}
  selector:
    matchLabels:
      {%- for label in k8s_deploy.sel_labels %}
        {{label.key}}: {{label.value}}
      {%- endfor %}
  template:
    metadata:
      labels:
        {%- for label in k8s_deploy.tpl_labels %}
        {{label.key}}: {{label.value}}
        {%- endfor %}
    spec:
      {%- if k8s_deploy.spec.hostUsers %}
      hostUsers: {{k8s_deploy.spec.hostUsers}}
      {%- endif %}
      containers:
      {%- for container in k8s_deploy.spec.containers %}
        - name: {{container.name}}
          image: {{container.image}}
          {%- if container.cmd %}
          command: {{container.cmd}}
          {%- endif %}
          imagePullPolicy: {{container.imagePull}}
          {%- if container.ports %}
          ports:
            {%- for port in container.ports %}
            - name: {{port.name}}
              {%- if port.container %}
              containerPort: {{port.container}}
              {%- endif %}
              {%- if port.proto %}
              protocol: {{port.proto}}
              {%- endif %}
              {%- if port.target %}
              targetPort: {{port.target}}
              {%- endif %}
            {%- endfor %}
          {%- endif %}
          {%- if container.env %}
          env:
            {%- for env in container.env %}
            - name: {{env.key}}
              value: {{env.value}}
            {%- endfor %}
          {%- endif %}
          {%- if container.volumeMounts %}
          volumeMounts:
            {%- for vol in container.volumeMounts %}
            - name: {{vol.name}}
              mountPath: {{vol.mountPath}}
              readOnly: {{vol.readOnly}}
              {%- if vol.subPath %}
              subPath: {{vol.subPath}}
              {%- endif %}
            {%- endfor %}
          {%- endif %}
          {%- if container.resources_requests or container.resources_limits %}
          resources:
          {%- if container.resources_requests %}
            requests:
              memory: "{{container.resources_requests.memory}}"
              cpu: "{{container.resources_requests.cpu}}"
          {%- endif %}
          {%- if container.resources_limits %}
            limits:
              memory: "{{container.resources_limits.memory}}"
              cpu: "{{container.resources_limits.cpu}}"
          {%- endif %}
          {%- endif %}
      {%- endfor %}
      {%- if k8s_deploy.spec.imagePullSecret %}
      imagePullSecrets: {{k8s_deploy.spec.imagePullSecret}}
      {%- endif %}
      {%- if k8s_deploy.spec.nodeSelector %}
        {%- for sel in k8s_deploy.spec.nodeSelector %}
        {{sel.key}}: {{sel.value}}
        {%- endfor %}
      {%- endif %}
      {%- if k8s_deploy.spec.nodeName %}
      nodeName: {{k8s_deploy.spec.nodeName}}
      {%- endif %}
      {%- if k8s_deploy.spec.affinity %}
      affinity:
        {%- if k8s_deploy.spec.affinity.affinity %}
        podAffinity:
          {{k8s_deploy.spec.affinity.affinity.typ}}:
          {%- if k8s_deploy.spec.affinity.affinity.matchExpressions %}
          - labelSelector:
              matchExpressions:
                {%- for expr in k8s_deploy.affinity.affinity.matchExpressions %}
                - key: {{expr.key}}
                  operator: {{expr.operator}}
                  values:
                  {%- for val in expr.values %}
                  - {{val}}
                  {%- endfor %}
                {%- endfor %}
            {%- if k8s_deploy.affinity.affinity.topologyKey %}
            topologyKey: {{k8s_deploy.affinity.affinity.topologyKey}}
            {%- endif %}
            {%- if k8s_deploy.affinity.affinity.matchLabelKeys %}
            matchLabelKeys:
            {%- for val in k8s_deploy.affinity.affinity.matchLabelKeys %}
            - {{val}}
            {%- endfor %}
            {%- endif %}
          {%- endif %}
        {%- endif %}
        {%- if k8s_deploy.spec.affinity.antiAffinity %}
        podAntiAffinity:
          {{k8s_deploy.spec.affinity.affinity.typ}}:
          - weight: {{k8s_deploy.spec.affinity.antiAffinity.weight}}
            podAffinityTerm:
              - labelSelector:
                  matchExpressions:
                  {%- for expr in k8s_deploy.affinity.antiAffinity.matchExpressions %}
                  - key: {{expr.key}}
                    operator: {{expr.operator}}
                    values:
                    {%- for val in expr.values %}
                    - {{val}}
                    {%- endfor %}
                  {%- endfor %}
                {%- if k8s_deploy.affinity.affinity.topologyKey %}
                topologyKey: {{k8s_deploy.affinity.affinity.topologyKey}}
                {%- endif %}
                {%- if k8s_deploy.affinity.affinity.matchLabelKeys %}
                matchLabelKeys:
                {%- for val in k8s_deploy.affinity.affinity.matchLabelKeys %}
                - {{val}}
                {%- endfor %}
              {%- endif %}
        {%- endif %}
      {%- endif %}
      {%- if k8s_deploy.spec.volumes %}
      volumes:
      {%- for vol in k8s_deploy.spec.volumes %}
        - name: {{vol.name}}
          {%- if vol.typ == "volumeClaim" and vol.persitentVolumeClaim %}
          persistentVolumeClaim:
            claimName: {{vol.persitentVolumeClaim.name}}
          {%- elif  vol.typ == "configMap" and vol.items %}
          configMap:
            name: {{vol.name}}
            items:
            {%- for item in vol.items %}
              - key: {{item.key}}
                path: {{item.path}}
            {%- endfor %}
          {%- elif  vol.typ == "secret" and vol.items %}
          secret:
            secretName: {{vol.name}}
            items:
            {%- for item in vol.items %}
              - key: {{item.key}}
                path: {{item.path}}
            {%- endfor %}
          {%- endif %}
      {%- endfor %}
      {%- endif %}
