diff --git a/files/build_templates/dns.j2 b/files/build_templates/dns.j2 new file mode 100644 index 0000000000..96fb81f9a1 --- /dev/null +++ b/files/build_templates/dns.j2 @@ -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": {} +} diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index d8399d25cb..09a5fe870a 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -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 diff --git a/src/sonic-config-engine/minigraph.py b/src/sonic-config-engine/minigraph.py index 0b2f8b0e76..8f9ec5bbef 100644 --- a/src/sonic-config-engine/minigraph.py +++ b/src/sonic-config-engine/minigraph.py @@ -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 diff --git a/src/sonic-config-engine/tests/dns.j2 b/src/sonic-config-engine/tests/dns.j2 new file mode 100644 index 0000000000..3ee2ab038a --- /dev/null +++ b/src/sonic-config-engine/tests/dns.j2 @@ -0,0 +1,9 @@ +{ + "DNS_NAMESERVER": { +{% if DEVICE_METADATA.localhost.cloudtype == "Public" %} + "6.6.6.6": {} +{% else %} + "8.8.8.8": {} +{% endif %} + } +} diff --git a/src/sonic-config-engine/tests/test_cfggen.py b/src/sonic-config-engine/tests/test_cfggen.py index 66635a9f78..ef44979558 100644 --- a/src/sonic-config-engine/tests/test_cfggen.py +++ b/src/sonic-config-engine/tests/test_cfggen.py @@ -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"]