# Deployment Configuration template
# Renders form data into a Nickel deployment specification

{
  # Deployment Metadata
  metadata = {
    name | doc "Deployment name" : String = "{{ deployment_name }}",
    {%- if namespace is defined %}
    namespace | doc "Kubernetes namespace" : String = "{{ namespace }}",
    {%- else %}
    namespace : String = "default",
    {%- endif %}
  },

  # Deployment Specification
  spec = {
    replicas | doc "Number of replicas" : Number = {{ replicas | default(1) }},

    # Pod Template Specification
    template = {
      # Pod Metadata
      metadata = {
        labels = {
          app | doc "App label" : String = "{{ app_label }}",
          {%- if version_label is defined %}
          version | doc "Version label" : String = "{{ version_label }}",
          {%- endif %}
        },
      },

      # Pod Specification
      spec = {
        containers = [
          {
            name | doc "Container name" : String = "{{ container_name }}",
            image | doc "Container image" : String = "{{ container_image }}",
            {%- if image_pull_policy is defined %}
            imagePullPolicy | doc "Image pull policy" : String = "{{ image_pull_policy }}",
            {%- endif %}

            # Port Configuration
            {%- if container_port is defined %}
            ports = [
              {
                containerPort | doc "Container port" : Number = {{ container_port }},
                {%- if port_name is defined %}
                name | doc "Port name" : String = "{{ port_name }}",
                {%- endif %}
              },
            ],
            {%- endif %}

            # Environment Variables
            {%- if environment_vars is defined %}
            env = [
              {%- for var in environment_vars %}
              {
                name : String = "{{ var.name }}",
                value : String = "{{ var.value }}",
              },
              {%- endfor %}
            ],
            {%- endif %}

            # Resource Limits
            resources = {
              {%- if cpu_request is defined %}
              requests = {
                cpu | doc "CPU request" : String = "{{ cpu_request }}",
                {%- if memory_request is defined %}
                memory | doc "Memory request" : String = "{{ memory_request }}",
                {%- endif %}
              },
              {%- endif %}

              {%- if cpu_limit is defined %}
              limits = {
                cpu | doc "CPU limit" : String = "{{ cpu_limit }}",
                {%- if memory_limit is defined %}
                memory | doc "Memory limit" : String = "{{ memory_limit }}",
                {%- endif %}
              },
              {%- endif %}
            },

            # Health Checks
            {%- if enable_liveness_probe == true or enable_liveness_probe == "true" %}
            livenessProbe = {
              httpGet = {
                path | doc "Health check path" : String = "{{ liveness_path | default('/health') }}",
                port | doc "Health check port" : Number = {{ liveness_port | default(8080) }},
              },
              initialDelaySeconds | doc "Initial delay" : Number = {{ liveness_delay | default(30) }},
              periodSeconds | doc "Check period" : Number = {{ liveness_period | default(10) }},
            },
            {%- endif %}

            {%- if enable_readiness_probe == true or enable_readiness_probe == "true" %}
            readinessProbe = {
              httpGet = {
                path | doc "Readiness check path" : String = "{{ readiness_path | default('/ready') }}",
                port | doc "Readiness check port" : Number = {{ readiness_port | default(8080) }},
              },
              initialDelaySeconds | doc "Initial delay" : Number = {{ readiness_delay | default(10) }},
              periodSeconds | doc "Check period" : Number = {{ readiness_period | default(5) }},
            },
            {%- endif %}
          },
        ],

        # Pod Restart Policy
        {%- if restart_policy is defined %}
        restartPolicy | doc "Restart policy" : String = "{{ restart_policy }}",
        {%- else %}
        restartPolicy : String = "Always",
        {%- endif %}
      },
    },
  },
}
