Skip to content

Commit

Permalink
Less vars in nodelocaldns templates
Browse files Browse the repository at this point in the history
Put intermediate templates vars in vars/ rather than in facts
  • Loading branch information
VannTen committed Dec 7, 2023
1 parent bc1026d commit 4b2cce8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 54 deletions.
46 changes: 0 additions & 46 deletions roles/kubernetes-apps/ansible/tasks/nodelocaldns.yml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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 }}
Expand All @@ -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 }}
Expand All @@ -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 %}
Expand Down Expand Up @@ -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 }}
Expand All @@ -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 }}
Expand All @@ -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 }}
Expand All @@ -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 %}
Expand Down
13 changes: 13 additions & 0 deletions roles/kubernetes-apps/ansible/vars/main.yml
Original file line number Diff line number Diff line change
@@ -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) }}"

0 comments on commit 4b2cce8

Please sign in to comment.