Use DNS j2 for default DNS configuration (#15901)

Why I did it
Support default DNS configuration

How I did it
Use j2 template to generate default DNS configuration.

How to verify it
Run sonic-config-engine unit test.
This commit is contained in:
ganglv 2023-08-01 06:43:00 +08:00 committed by GitHub
parent 04a6031b2d
commit 5c4ab7a7f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,11 @@
{# Please follow below example to add your DNS server
{
"DNS_NAMESERVER": {
"6.6.6.6": {},
"2001:4860:4860::64": {}
}
}
#}
{
"DNS_NAMESERVER": {}
}

View File

@ -373,6 +373,9 @@ sudo cp $IMAGE_CONFIGS/ntp/ntp-systemd-wrapper $FILESYSTEM_ROOT/usr/lib/ntp/
sudo cp $IMAGE_CONFIGS/ntp/ntp.service $FILESYSTEM_ROOT_USR_LIB_SYSTEMD_SYSTEM
echo "ntp.service" | sudo tee -a $GENERATED_SERVICE_FILE
# Copy DNS templates
sudo cp $BUILD_TEMPLATES/dns.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/
# Copy warmboot-finalizer files
sudo LANG=C cp $IMAGE_CONFIGS/warmboot-finalizer/finalize-warmboot.sh $FILESYSTEM_ROOT/usr/local/bin/finalize-warmboot.sh
sudo LANG=C cp $IMAGE_CONFIGS/warmboot-finalizer/warmboot-finalizer.service $FILESYSTEM_ROOT_USR_LIB_SYSTEMD_SYSTEM

View File

@ -5,6 +5,7 @@ import math
import os
import sys
import json
import jinja2
import subprocess
from collections import defaultdict
@ -2010,6 +2011,27 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
results['DHCP_SERVER'] = dict((item, {}) for item in dhcp_servers)
results['DHCP_RELAY'] = dhcp_relay_table
results['NTP_SERVER'] = dict((item, {}) for item in ntp_servers)
# Set default DNS nameserver from dns.j2
results['DNS_NAMESERVER'] = {}
if os.environ.get("CFGGEN_UNIT_TESTING", "0") == "2":
dns_conf = os.path.join(os.path.dirname(__file__), "tests/", "dns.j2")
else:
dns_conf = "/usr/share/sonic/templates/dns.j2"
if os.path.isfile(dns_conf):
text = ""
with open(dns_conf) as template_file:
# Semgrep does not allow to use jinja2 directly, but we do need jinja2 for SONiC
environment = jinja2.Environment(trim_blocks=True) # nosemgrep
dns_template = environment.from_string(template_file.read())
text = dns_template.render(results)
try:
dns_res = json.loads(text)
except ValueError as e:
sys.exit("Error: fail to load dns configuration, %s" % str(e))
else:
dns_nameservers = dns_res.get('DNS_NAMESERVER', {})
for k in dns_nameservers.keys():
results['DNS_NAMESERVER'][str(k)] = {}
results['TACPLUS_SERVER'] = dict((item, {'priority': '1', 'tcp_port': '49'}) for item in tacacs_servers)
if len(acl_table_types) > 0:
results['ACL_TABLE_TYPE'] = acl_table_types

View File

@ -0,0 +1,9 @@
{
"DNS_NAMESERVER": {
{% if DEVICE_METADATA.localhost.cloudtype == "Public" %}
"6.6.6.6": {}
{% else %}
"8.8.8.8": {}
{% endif %}
}
}

View File

@ -696,6 +696,11 @@ class TestCfgGen(TestCase):
output = self.run_script(argument)
self.assertEqual(utils.to_dict(output.strip()), utils.to_dict("{'10.0.10.1': {}, '10.0.10.2': {}}"))
def test_dns_nameserver(self):
argument = ['-m', self.sample_graph_metadata, '-p', self.port_config, '-v', "DNS_NAMESERVER"]
output = self.run_script(argument)
self.assertEqual(utils.to_dict(output.strip()), utils.to_dict("{'6.6.6.6': {}}"))
def test_minigraph_vnet(self, **kwargs):
graph_file = kwargs.get('graph_file', self.sample_graph_simple)
argument = ['-m', graph_file, '-p', self.port_config, '-v', "VNET"]