Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix host DNS settings being edited too soon, and not working with NetworkManager #8575

Merged
merged 1 commit into from
Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cluster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@
- { role: kubernetes-apps/external_provisioner, tags: external-provisioner }
- { role: kubernetes-apps, tags: apps }

- hosts: k8s_cluster
- name: Apply resolv.conf changes now that cluster DNS is up
hosts: k8s_cluster
gather_facts: False
any_errors_fatal: "{{ any_errors_fatal | default(true) }}"
environment: "{{ proxy_disable_env }}"
Expand Down
1 change: 1 addition & 0 deletions roles/kubernetes/preinstall/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ignore_assert_errors: false

epel_enabled: false
# Kubespray sets this to true after clusterDNS is running to apply changes to the host resolv.conf
dns_late: false

common_required_pkgs:
Expand Down
3 changes: 1 addition & 2 deletions roles/kubernetes/preinstall/handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
command: /usr/bin/coreos-cloudinit --from-file {{ resolveconf_cloud_init_conf }}
when: ansible_os_family in ["Flatcar", "Flatcar Container Linux by Kinvolk"]

- name: Preinstall | update resolvconf for Fedora CoreOS
- name: Preinstall | update resolvconf for networkmanager
command: /bin/true
notify:
- Preinstall | reload NetworkManager
- Preinstall | reload kubelet
when: is_fedora_coreos

- name: Preinstall | reload NetworkManager
service:
Expand Down
29 changes: 24 additions & 5 deletions roles/kubernetes/preinstall/tasks/0040-set_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@

when: resolvconf_stat.stat.exists is defined and resolvconf_stat.stat.exists

- name: NetworkManager | Check if host has NetworkManager
# noqa 303 Should we use service_facts for this?
command: systemctl is-active --quiet NetworkManager.service
register: networkmanager_enabled
failed_when: false
changed_when: false
check_mode: false

- name: check systemd-resolved
# noqa 303 Should we use service_facts for this?
command: systemctl is-active systemd-resolved
Expand Down Expand Up @@ -98,8 +106,7 @@

- name: check if early DNS configuration stage
set_fact:
dns_early: >-
{%- if kubelet_configured.stat.exists -%}false{%- else -%}true{%- endif -%}
dns_early: "{{ not kubelet_configured.stat.exists }}"

- name: target resolv.conf files
set_fact:
Expand Down Expand Up @@ -177,12 +184,24 @@
{{ upstream_dns_servers|default([]) }}
{%- endif -%}

- name: generate nameservers to resolvconf
# This task should only run after cluster/nodelocal DNS is up, otherwise all DNS lookups will timeout
- name: generate nameservers for resolvconf, including cluster DNS
set_fact:
nameserverentries:
nameserver {{ ( ( [nodelocaldns_ip] if enable_nodelocaldns else []) + coredns_server|d([]) + nameservers|d([]) + cloud_resolver|d([]) + configured_nameservers|d([])) | unique | join(',nameserver ') }}
nameserverentries: |-
{{ ( ( [nodelocaldns_ip] if enable_nodelocaldns else []) + coredns_server|d([]) + nameservers|d([]) + cloud_resolver|d([]) + configured_nameservers|d([])) | unique | join(',') }}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change allows us to specify this complex list of nameservers one time, then re-use the value in both the raw /etc/resolv.conf task and the NetworkManager task.

supersede_nameserver:
supersede domain-name-servers {{ ( coredns_server|d([]) + nameservers|d([]) + cloud_resolver|d([])) | unique | join(', ') }};
when: not dns_early or dns_late

# This task should run instead of the above task when cluster/nodelocal DNS hasn't
# been deployed yet (like scale.yml/cluster.yml) or when it's down (reset.yml)
- name: generate nameservers for resolvconf, not including cluster DNS
set_fact:
nameserverentries: |-
{{ ( nameservers|d([]) + cloud_resolver|d([]) + configured_nameservers|d([])) | unique | join(',') }}
supersede_nameserver:
supersede domain-name-servers {{ ( nameservers|d([]) + cloud_resolver|d([])) | unique | join(', ') }};
when: dns_early and not dns_late

- name: gather os specific variables
include_vars: "{{ item }}"
Expand Down
5 changes: 4 additions & 1 deletion roles/kubernetes/preinstall/tasks/0060-resolvconf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
blockinfile:
path: "{{ resolvconffile }}"
block: |-
{% for item in [domainentry] + [searchentries] + nameserverentries.split(',') -%}
{% for item in [domainentry] + [searchentries] -%}
{{ item }}
{% endfor %}
{% for item in nameserverentries.split(',') %}
nameserver {{ item }}
{% endfor %}
options ndots:{{ ndots }}
options timeout:2
options attempts:2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
---
- name: NetworkManager | Check if host has NetworkManager
# noqa 303 Should we use service_facts for this?
command: systemctl is-active --quiet NetworkManager.service
register: nm_check
failed_when: false
changed_when: false
check_mode: false

- name: NetworkManager | Ensure NetworkManager conf.d dir
file:
path: "/etc/NetworkManager/conf.d"
state: directory
recurse: yes
when: nm_check.rc == 0

- name: NetworkManager | Prevent NetworkManager from managing Calico interfaces (cali*/tunl*/vxlan.calico)
copy:
Expand All @@ -22,7 +13,6 @@
dest: /etc/NetworkManager/conf.d/calico.conf
mode: 0644
when:
- nm_check.rc == 0
- kube_network_plugin == "calico"
notify: Preinstall | reload NetworkManager

Expand All @@ -35,5 +25,4 @@
unmanaged-devices+=interface-name:kube-ipvs0;interface-name:nodelocaldns
dest: /etc/NetworkManager/conf.d/k8s.conf
mode: 0644
when: nm_check.rc == 0
notify: Preinstall | reload NetworkManager
8 changes: 4 additions & 4 deletions roles/kubernetes/preinstall/tasks/0063-networkmanager-dns.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
path: /etc/NetworkManager/conf.d/dns.conf
section: global-dns-domain-*
option: servers
value: "{{ ( coredns_server + nameservers|d([]) + cloud_resolver|d([])) | unique | join(',') }}"
value: "{{ nameserverentries }}"
mode: '0600'
backup: yes
notify: Preinstall | update resolvconf for Fedora CoreOS
notify: Preinstall | update resolvconf for networkmanager

- name: NetworkManager | Add DNS search to NM configuration
ini_file:
Expand All @@ -17,7 +17,7 @@
value: "{{ ([ 'default.svc.' + dns_domain, 'svc.' + dns_domain ] + searchdomains|default([])) | join(',') }}"
mode: '0600'
backup: yes
notify: Preinstall | update resolvconf for Fedora CoreOS
notify: Preinstall | update resolvconf for networkmanager

- name: NetworkManager | Add DNS options to NM configuration
ini_file:
Expand All @@ -27,4 +27,4 @@
value: "ndots:{{ ndots }};timeout:2;attempts:2;"
mode: '0600'
backup: yes
notify: Preinstall | update resolvconf for Fedora CoreOS
notify: Preinstall | update resolvconf for networkmanager
6 changes: 4 additions & 2 deletions roles/kubernetes/preinstall/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
- dns_mode != 'none'
- resolvconf_mode == 'host_resolvconf'
- systemd_resolved_enabled.rc != 0
- not is_fedora_coreos
- networkmanager_enabled.rc != 0
tags:
- bootstrap-os
- resolvconf
Expand All @@ -40,14 +40,16 @@
- resolvconf

- import_tasks: 0062-networkmanager-unmanaged-devices.yml
when:
- networkmanager_enabled.rc == 0
tags:
- bootstrap-os

- import_tasks: 0063-networkmanager-dns.yml
when:
- dns_mode != 'none'
- resolvconf_mode == 'host_resolvconf'
- is_fedora_coreos
- networkmanager_enabled.rc == 0
tags:
- bootstrap-os
- resolvconf
Expand Down
5 changes: 3 additions & 2 deletions scale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,11 @@
- { role: kubernetes/node-label, tags: node-label }
- { role: network_plugin, tags: network }

- hosts: k8s_cluster
- name: Apply resolv.conf changes now that cluster DNS is up
hosts: k8s_cluster
gather_facts: False
any_errors_fatal: "{{ any_errors_fatal | default(true) }}"
environment: "{{ proxy_disable_env }}"
roles:
- { role: kubespray-defaults }
- { role: kubernetes/preinstall, when: "dns_mode != 'none' and resolvconf_mode == 'host_resolvconf'", tags: resolvconf }
- { role: kubernetes/preinstall, when: "dns_mode != 'none' and resolvconf_mode == 'host_resolvconf'", tags: resolvconf, dns_late: true }
5 changes: 3 additions & 2 deletions upgrade-cluster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@
- { role: kubespray-defaults }
- { role: kubernetes-apps, tags: apps }

- hosts: k8s_cluster
- name: Apply resolv.conf changes now that cluster DNS is up
hosts: k8s_cluster
gather_facts: False
any_errors_fatal: "{{ any_errors_fatal | default(true) }}"
environment: "{{ proxy_disable_env }}"
roles:
- { role: kubespray-defaults }
- { role: kubernetes/preinstall, when: "dns_mode != 'none' and resolvconf_mode == 'host_resolvconf'", tags: resolvconf }
- { role: kubernetes/preinstall, when: "dns_mode != 'none' and resolvconf_mode == 'host_resolvconf'", tags: resolvconf, dns_late: true }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the extra dns_late: true here to match cluster.yml and the others since this setting allows you to skip much of the preinstall role that doesn't involve updating resolv.conf.