Skip to content

Commit

Permalink
Use DNS j2 for default DNS configuration (sonic-net#15901)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ganglyu authored Jul 31, 2023
1 parent 04a6031 commit 5c4ab7a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
11 changes: 11 additions & 0 deletions files/build_templates/dns.j2
Original file line number Diff line number Diff line change
@@ -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": {}
}
3 changes: 3 additions & 0 deletions files/build_templates/sonic_debian_extension.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions src/sonic-config-engine/minigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import sys
import json
import jinja2
import subprocess
from collections import defaultdict

Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/sonic-config-engine/tests/dns.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"DNS_NAMESERVER": {
{% if DEVICE_METADATA.localhost.cloudtype == "Public" %}
"6.6.6.6": {}
{% else %}
"8.8.8.8": {}
{% endif %}
}
}
5 changes: 5 additions & 0 deletions src/sonic-config-engine/tests/test_cfggen.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,11 @@ def test_metadata_ntp(self):
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"]
Expand Down

0 comments on commit 5c4ab7a

Please sign in to comment.