{% extends 'common/base_resource.tf.j2' %}
{% import 'common/macros.j2' as macros %}
{% block resources %}
{% for pool in resources %}
{{ resource_spacer() }}
resource "azurerm_kubernetes_cluster_node_pool" "{{ pool.name_sanitized }}" {
  name                  = "{{ pool.name }}"
  kubernetes_cluster_id = "{{ pool.kubernetes_cluster_id }}"
  vm_size               = "{{ pool.vm_size }}"
  
  {% if pool.node_count %}
  node_count = {{ pool.node_count }}
  {% endif %}
  
  {% if pool.enable_auto_scaling %}
  enable_auto_scaling = true
  {% if pool.min_count %}
  min_count = {{ pool.min_count }}
  {% endif %}
  {% if pool.max_count %}
  max_count = {{ pool.max_count }}
  {% endif %}
  {% endif %}
  
  {% if pool.enable_host_encryption is defined %}
  enable_host_encryption = {{ pool.enable_host_encryption | lower }}
  {% endif %}
  
  {% if pool.enable_node_public_ip is defined %}
  enable_node_public_ip = {{ pool.enable_node_public_ip | lower }}
  {% endif %}
  
  {% if pool.eviction_policy %}
  eviction_policy = "{{ pool.eviction_policy }}"
  {% endif %}
  
  {% if pool.fips_enabled is defined %}
  fips_enabled = {{ pool.fips_enabled | lower }}
  {% endif %}
  
  {% if pool.gpu_instance %}
  gpu_instance = "{{ pool.gpu_instance }}"
  {% endif %}
  
  {% if pool.host_group_id %}
  host_group_id = "{{ pool.host_group_id }}"
  {% endif %}
  
  {% if pool.kubelet_disk_type %}
  kubelet_disk_type = "{{ pool.kubelet_disk_type }}"
  {% endif %}
  
  {% if pool.max_pods %}
  max_pods = {{ pool.max_pods }}
  {% endif %}
  
  {% if pool.mode %}
  mode = "{{ pool.mode }}"
  {% endif %}
  
  {% if pool.node_labels %}
  node_labels = {{ pool.node_labels | tojson }}
  {% endif %}
  
  {% if pool.node_public_ip_prefix_id %}
  node_public_ip_prefix_id = "{{ pool.node_public_ip_prefix_id }}"
  {% endif %}
  
  {% if pool.node_taints %}
  node_taints = {{ pool.node_taints | tojson }}
  {% endif %}
  
  {% if pool.orchestrator_version %}
  orchestrator_version = "{{ pool.orchestrator_version }}"
  {% endif %}
  
  {% if pool.os_disk_size_gb %}
  os_disk_size_gb = {{ pool.os_disk_size_gb }}
  {% endif %}
  
  {% if pool.os_disk_type %}
  os_disk_type = "{{ pool.os_disk_type }}"
  {% endif %}
  
  {% if pool.os_sku %}
  os_sku = "{{ pool.os_sku }}"
  {% endif %}
  
  {% if pool.os_type %}
  os_type = "{{ pool.os_type }}"
  {% endif %}
  
  {% if pool.pod_subnet_id %}
  pod_subnet_id = "{{ pool.pod_subnet_id }}"
  {% endif %}
  
  {% if pool.priority %}
  priority = "{{ pool.priority }}"
  {% endif %}
  
  {% if pool.proximity_placement_group_id %}
  proximity_placement_group_id = "{{ pool.proximity_placement_group_id }}"
  {% endif %}
  
  {% if pool.scale_down_mode %}
  scale_down_mode = "{{ pool.scale_down_mode }}"
  {% endif %}
  
  {% if pool.snapshot_id %}
  snapshot_id = "{{ pool.snapshot_id }}"
  {% endif %}
  
  {% if pool.spot_max_price %}
  spot_max_price = {{ pool.spot_max_price }}
  {% endif %}
  
  {% if pool.tags %}
  tags = {{ pool.tags | tojson }}
  {% endif %}
  
  {% if pool.ultra_ssd_enabled is defined %}
  ultra_ssd_enabled = {{ pool.ultra_ssd_enabled | lower }}
  {% endif %}
  
  {% if pool.vnet_subnet_id %}
  vnet_subnet_id = "{{ pool.vnet_subnet_id }}"
  {% endif %}
  
  {% if pool.workload_runtime %}
  workload_runtime = "{{ pool.workload_runtime }}"
  {% endif %}
  
  {% if pool.availability_zones %}
  zones = {{ pool.availability_zones | tojson }}
  {% endif %}
  
  {% if pool.capacity_reservation_group_id %}
  capacity_reservation_group_id = "{{ pool.capacity_reservation_group_id }}"
  {% endif %}
  
  {% if pool.custom_ca_trust_enabled is defined %}
  custom_ca_trust_enabled = {{ pool.custom_ca_trust_enabled | lower }}
  {% endif %}
  
  {% if pool.kubelet_config %}
  kubelet_config {
    {% if pool.kubelet_config.allowed_unsafe_sysctls %}
    allowed_unsafe_sysctls = {{ pool.kubelet_config.allowed_unsafe_sysctls | tojson }}
    {% endif %}
    {% if pool.kubelet_config.container_log_max_line %}
    container_log_max_line = {{ pool.kubelet_config.container_log_max_line }}
    {% endif %}
    {% if pool.kubelet_config.container_log_max_size_mb %}
    container_log_max_size_mb = {{ pool.kubelet_config.container_log_max_size_mb }}
    {% endif %}
    {% if pool.kubelet_config.cpu_cfs_quota_enabled is not none %}
    cpu_cfs_quota_enabled = {{ pool.kubelet_config.cpu_cfs_quota_enabled | lower }}
    {% endif %}
    {% if pool.kubelet_config.cpu_cfs_quota_period %}
    cpu_cfs_quota_period = "{{ pool.kubelet_config.cpu_cfs_quota_period }}"
    {% endif %}
    {% if pool.kubelet_config.cpu_manager_policy %}
    cpu_manager_policy = "{{ pool.kubelet_config.cpu_manager_policy }}"
    {% endif %}
    {% if pool.kubelet_config.image_gc_high_threshold %}
    image_gc_high_threshold = {{ pool.kubelet_config.image_gc_high_threshold }}
    {% endif %}
    {% if pool.kubelet_config.image_gc_low_threshold %}
    image_gc_low_threshold = {{ pool.kubelet_config.image_gc_low_threshold }}
    {% endif %}
    {% if pool.kubelet_config.pod_max_pid %}
    pod_max_pid = {{ pool.kubelet_config.pod_max_pid }}
    {% endif %}
    {% if pool.kubelet_config.topology_manager_policy %}
    topology_manager_policy = "{{ pool.kubelet_config.topology_manager_policy }}"
    {% endif %}
  }
  {% endif %}
  
  {% if pool.linux_os_config %}
  linux_os_config {
    {% if pool.linux_os_config.swap_file_size_mb %}
    swap_file_size_mb = {{ pool.linux_os_config.swap_file_size_mb }}
    {% endif %}
    {% if pool.linux_os_config.transparent_huge_page_defrag %}
    transparent_huge_page_defrag = "{{ pool.linux_os_config.transparent_huge_page_defrag }}"
    {% endif %}
    {% if pool.linux_os_config.transparent_huge_page_enabled %}
    transparent_huge_page_enabled = "{{ pool.linux_os_config.transparent_huge_page_enabled }}"
    {% endif %}
    
    {% if pool.linux_os_config.sysctl_config %}
    sysctl_config {
      {% if pool.linux_os_config.sysctl_config.fs_aio_max_nr %}
      fs_aio_max_nr = {{ pool.linux_os_config.sysctl_config.fs_aio_max_nr }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.fs_file_max %}
      fs_file_max = {{ pool.linux_os_config.sysctl_config.fs_file_max }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.fs_inotify_max_user_watches %}
      fs_inotify_max_user_watches = {{ pool.linux_os_config.sysctl_config.fs_inotify_max_user_watches }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.fs_nr_open %}
      fs_nr_open = {{ pool.linux_os_config.sysctl_config.fs_nr_open }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.kernel_threads_max %}
      kernel_threads_max = {{ pool.linux_os_config.sysctl_config.kernel_threads_max }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_core_netdev_max_backlog %}
      net_core_netdev_max_backlog = {{ pool.linux_os_config.sysctl_config.net_core_netdev_max_backlog }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_core_optmem_max %}
      net_core_optmem_max = {{ pool.linux_os_config.sysctl_config.net_core_optmem_max }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_core_rmem_default %}
      net_core_rmem_default = {{ pool.linux_os_config.sysctl_config.net_core_rmem_default }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_core_rmem_max %}
      net_core_rmem_max = {{ pool.linux_os_config.sysctl_config.net_core_rmem_max }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_core_somaxconn %}
      net_core_somaxconn = {{ pool.linux_os_config.sysctl_config.net_core_somaxconn }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_core_wmem_default %}
      net_core_wmem_default = {{ pool.linux_os_config.sysctl_config.net_core_wmem_default }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_core_wmem_max %}
      net_core_wmem_max = {{ pool.linux_os_config.sysctl_config.net_core_wmem_max }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_ipv4_ip_local_port_range_max %}
      net_ipv4_ip_local_port_range_max = {{ pool.linux_os_config.sysctl_config.net_ipv4_ip_local_port_range_max }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_ipv4_ip_local_port_range_min %}
      net_ipv4_ip_local_port_range_min = {{ pool.linux_os_config.sysctl_config.net_ipv4_ip_local_port_range_min }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_ipv4_neigh_default_gc_thresh1 %}
      net_ipv4_neigh_default_gc_thresh1 = {{ pool.linux_os_config.sysctl_config.net_ipv4_neigh_default_gc_thresh1 }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_ipv4_neigh_default_gc_thresh2 %}
      net_ipv4_neigh_default_gc_thresh2 = {{ pool.linux_os_config.sysctl_config.net_ipv4_neigh_default_gc_thresh2 }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_ipv4_neigh_default_gc_thresh3 %}
      net_ipv4_neigh_default_gc_thresh3 = {{ pool.linux_os_config.sysctl_config.net_ipv4_neigh_default_gc_thresh3 }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_ipv4_tcp_fin_timeout %}
      net_ipv4_tcp_fin_timeout = {{ pool.linux_os_config.sysctl_config.net_ipv4_tcp_fin_timeout }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_ipv4_tcp_keepalive_intvl %}
      net_ipv4_tcp_keepalive_intvl = {{ pool.linux_os_config.sysctl_config.net_ipv4_tcp_keepalive_intvl }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_ipv4_tcp_keepalive_probes %}
      net_ipv4_tcp_keepalive_probes = {{ pool.linux_os_config.sysctl_config.net_ipv4_tcp_keepalive_probes }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_ipv4_tcp_keepalive_time %}
      net_ipv4_tcp_keepalive_time = {{ pool.linux_os_config.sysctl_config.net_ipv4_tcp_keepalive_time }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_ipv4_tcp_max_syn_backlog %}
      net_ipv4_tcp_max_syn_backlog = {{ pool.linux_os_config.sysctl_config.net_ipv4_tcp_max_syn_backlog }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_ipv4_tcp_max_tw_buckets %}
      net_ipv4_tcp_max_tw_buckets = {{ pool.linux_os_config.sysctl_config.net_ipv4_tcp_max_tw_buckets }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_ipv4_tcp_tw_reuse is not none %}
      net_ipv4_tcp_tw_reuse = {{ pool.linux_os_config.sysctl_config.net_ipv4_tcp_tw_reuse | lower }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_netfilter_nf_conntrack_buckets %}
      net_netfilter_nf_conntrack_buckets = {{ pool.linux_os_config.sysctl_config.net_netfilter_nf_conntrack_buckets }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.net_netfilter_nf_conntrack_max %}
      net_netfilter_nf_conntrack_max = {{ pool.linux_os_config.sysctl_config.net_netfilter_nf_conntrack_max }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.vm_max_map_count %}
      vm_max_map_count = {{ pool.linux_os_config.sysctl_config.vm_max_map_count }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.vm_swappiness %}
      vm_swappiness = {{ pool.linux_os_config.sysctl_config.vm_swappiness }}
      {% endif %}
      {% if pool.linux_os_config.sysctl_config.vm_vfs_cache_pressure %}
      vm_vfs_cache_pressure = {{ pool.linux_os_config.sysctl_config.vm_vfs_cache_pressure }}
      {% endif %}
    }
    {% endif %}
  }
  {% endif %}
  
  {% if pool.node_network_profile %}
  node_network_profile {
    {% if pool.node_network_profile.node_public_ip_tags %}
    node_public_ip_tags = {{ pool.node_network_profile.node_public_ip_tags | tojson }}
    {% endif %}
  }
  {% endif %}
  
  {% if pool.upgrade_settings %}
  upgrade_settings {
    {% if pool.upgrade_settings.max_surge %}
    max_surge = "{{ pool.upgrade_settings.max_surge }}"
    {% endif %}
  }
  {% endif %}
  
  {% if pool.windows_profile %}
  windows_profile {
    {% if pool.windows_profile.outbound_nat_enabled is defined %}
    outbound_nat_enabled = {{ pool.windows_profile.outbound_nat_enabled | lower }}
    {% endif %}
  }
  {% endif %}

  lifecycle {
    prevent_destroy = true
    ignore_changes = [
      node_count
    ]
  }
}

{% endfor %}
{% endblock %}
