From 4b2cce8faa74db73a5f937d15d39c3c8707b6505 Mon Sep 17 00:00:00 2001 From: Max Gautier Date: Wed, 6 Dec 2023 23:59:36 +0100 Subject: [PATCH] Less vars in nodelocaldns templates Put intermediate templates vars in vars/ rather than in facts --- .../ansible/tasks/nodelocaldns.yml | 46 ------------------- .../templates/nodelocaldns-config.yml.j2 | 16 +++---- roles/kubernetes-apps/ansible/vars/main.yml | 13 ++++++ 3 files changed, 21 insertions(+), 54 deletions(-) diff --git a/roles/kubernetes-apps/ansible/tasks/nodelocaldns.yml b/roles/kubernetes-apps/ansible/tasks/nodelocaldns.yml index b438afb88c4..b6de7cc4a5d 100644 --- a/roles/kubernetes-apps/ansible/tasks/nodelocaldns.yml +++ b/roles/kubernetes-apps/ansible/tasks/nodelocaldns.yml @@ -1,21 +1,4 @@ --- -- name: Kubernetes Apps | set up necessary nodelocaldns parameters - set_fact: - # noqa: jinja[spacing] - primaryClusterIP: >- - {%- if dns_mode in ['coredns', 'coredns_dual'] -%} - {{ skydns_server }} - {%- elif dns_mode == 'manual' -%} - {{ manual_dns_server }} - {%- endif -%} - secondaryclusterIP: "{{ skydns_server_secondary }}" - when: - - enable_nodelocaldns - - inventory_hostname == groups['kube_control_plane'] | first - tags: - - nodelocaldns - - coredns - - name: Kubernetes Apps | Lay Down nodelocaldns Template template: src: "{{ item.file }}.j2" @@ -26,20 +9,6 @@ - { name: nodelocaldns, file: nodelocaldns-sa.yml, type: sa } - { name: nodelocaldns, file: nodelocaldns-daemonset.yml, type: daemonset } register: nodelocaldns_manifests - vars: - # noqa: jinja[spacing] - forwardTarget: >- - {%- if secondaryclusterIP is defined and dns_mode == 'coredns_dual' -%} - {{ primaryClusterIP }} {{ secondaryclusterIP }} - {%- else -%} - {{ primaryClusterIP }} - {%- endif -%} - upstreamForwardTarget: >- - {%- if upstream_dns_servers is defined and upstream_dns_servers | length > 0 -%} - {{ upstream_dns_servers | join(' ') }} - {%- else -%} - /etc/resolv.conf - {%- endif -%} when: - enable_nodelocaldns - inventory_hostname == groups['kube_control_plane'] | first @@ -55,21 +24,6 @@ with_items: - { name: nodelocaldns, file: nodelocaldns-second-daemonset.yml, type: daemonset } register: nodelocaldns_second_manifests - vars: - # noqa: jinja[spacing] - forwardTarget: >- - {%- if secondaryclusterIP is defined and dns_mode == 'coredns_dual' -%} - {{ primaryClusterIP }} {{ secondaryclusterIP }} - {%- else -%} - {{ primaryClusterIP }} - {%- endif -%} - # noqa: jinja[spacing] - upstreamForwardTarget: >- - {%- if upstream_dns_servers is defined and upstream_dns_servers | length > 0 -%} - {{ upstream_dns_servers | join(' ') }} - {%- else -%} - /etc/resolv.conf - {%- endif -%} when: - enable_nodelocaldns - enable_nodelocaldns_secondary diff --git a/roles/kubernetes-apps/ansible/templates/nodelocaldns-config.yml.j2 b/roles/kubernetes-apps/ansible/templates/nodelocaldns-config.yml.j2 index b15ea89e996..a80fe7245c4 100644 --- a/roles/kubernetes-apps/ansible/templates/nodelocaldns-config.yml.j2 +++ b/roles/kubernetes-apps/ansible/templates/nodelocaldns-config.yml.j2 @@ -41,7 +41,7 @@ data: reload loop bind {{ nodelocaldns_ip }} - forward . {{ forwardTarget }} { + forward . {{ nodelocaldns_forward_target }} { force_tcp } prometheus {% if nodelocaldns_bind_metrics_host_ip %}{$MY_HOST_IP}{% endif %}:{{ nodelocaldns_prometheus_port }} @@ -58,7 +58,7 @@ data: reload loop bind {{ nodelocaldns_ip }} - forward . {{ forwardTarget }} { + forward . {{ nodelocaldns_forward_target }} { force_tcp } prometheus {% if nodelocaldns_bind_metrics_host_ip %}{$MY_HOST_IP}{% endif %}:{{ nodelocaldns_prometheus_port }} @@ -69,7 +69,7 @@ data: reload loop bind {{ nodelocaldns_ip }} - forward . {{ forwardTarget }} { + forward . {{ nodelocaldns_forward_target }} { force_tcp } prometheus {% if nodelocaldns_bind_metrics_host_ip %}{$MY_HOST_IP}{% endif %}:{{ nodelocaldns_prometheus_port }} @@ -80,7 +80,7 @@ data: reload loop bind {{ nodelocaldns_ip }} - forward . {{ upstreamForwardTarget }}{% if dns_upstream_forward_extra_opts is defined %} { + forward . {{ nodelocaldns_upstream_forward_target }}{% if dns_upstream_forward_extra_opts is defined %} { {% for optname, optvalue in dns_upstream_forward_extra_opts.items() %} {{ optname }} {{ optvalue }} {% endfor %} @@ -123,7 +123,7 @@ data: reload loop bind {{ nodelocaldns_ip }} - forward . {{ forwardTarget }} { + forward . {{ nodelocaldns_forward_target }} { force_tcp } prometheus {% if nodelocaldns_bind_metrics_host_ip %}{$MY_HOST_IP}{% endif %}:{{ nodelocaldns_secondary_prometheus_port }} @@ -140,7 +140,7 @@ data: reload loop bind {{ nodelocaldns_ip }} - forward . {{ forwardTarget }} { + forward . {{ nodelocaldns_forward_target }} { force_tcp } prometheus {% if nodelocaldns_bind_metrics_host_ip %}{$MY_HOST_IP}{% endif %}:{{ nodelocaldns_secondary_prometheus_port }} @@ -151,7 +151,7 @@ data: reload loop bind {{ nodelocaldns_ip }} - forward . {{ forwardTarget }} { + forward . {{ nodelocaldns_forward_target }} { force_tcp } prometheus {% if nodelocaldns_bind_metrics_host_ip %}{$MY_HOST_IP}{% endif %}:{{ nodelocaldns_secondary_prometheus_port }} @@ -162,7 +162,7 @@ data: reload loop bind {{ nodelocaldns_ip }} - forward . {{ upstreamForwardTarget }}{% if dns_upstream_forward_extra_opts is defined %} { + forward . {{ nodelocaldns_upstream_forward_target }}{% if dns_upstream_forward_extra_opts is defined %} { {% for optname, optvalue in dns_upstream_forward_extra_opts.items() %} {{ optname }} {{ optvalue }} {% endfor %} diff --git a/roles/kubernetes-apps/ansible/vars/main.yml b/roles/kubernetes-apps/ansible/vars/main.yml index 712fe365f32..861bb146cd4 100644 --- a/roles/kubernetes-apps/ansible/vars/main.yml +++ b/roles/kubernetes-apps/ansible/vars/main.yml @@ -1 +1,14 @@ +--- +# CoreDNS vars coredns_ordinal_suffixes: "{{ [''] + (dns_mode == 'coredns_dual' | ternary(['-secondary'], [])) }}" + +# Nodelocal DNS vars + +primary_dns_by_mode: + coredns: "{{ skydns_server }}" + coredns_dual: "{{ skydns_server }}" + manual: "{{ manual_dns_server }}" + none: "" + +nodelocaldns_forward_target: "{{ ([primary_dns_by_mode[dns_mode]] + (dns_mode == 'coredns_dual') | ternary([skydns_server_secondary], [])) | join(' ') }}" +nodelocaldns_upstream_forward_target: "{{ upstream_dns_servers | d([]) | join(' ') | d('/etc/resolv.conf', true) }}"