[dhcp_server] Remove dependency in port-name-alias-map.txt.j2 (#17858)

* [dhcp_server] Remove dependency in port-name-alias-map.txt.j2
This commit is contained in:
Yaqiang Zhu 2024-01-23 07:21:16 +08:00 committed by mssonicbld
parent 9eef01d7a7
commit 0b84b8fc30
8 changed files with 50 additions and 30 deletions

View File

@ -86,7 +86,7 @@ RUN apt-get clean -y && \
COPY ["docker_init.sh", "start.sh", "/usr/bin/"] COPY ["docker_init.sh", "start.sh", "/usr/bin/"]
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"] COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
COPY ["files/supervisor-proc-exit-listener", "/usr/bin"] COPY ["files/supervisor-proc-exit-listener", "/usr/bin"]
COPY ["port-name-alias-map.txt.j2", "rsyslog/rsyslog.conf.j2", "kea-dhcp4.conf.j2", "/usr/share/sonic/templates/"] COPY ["rsyslog/rsyslog.conf.j2", "kea-dhcp4.conf.j2", "/usr/share/sonic/templates/"]
COPY ["critical_processes", "/etc/supervisor/"] COPY ["critical_processes", "/etc/supervisor/"]
COPY ["lease_update.sh", "/etc/kea/"] COPY ["lease_update.sh", "/etc/kea/"]
COPY ["kea-dhcp4-init.conf", "/etc/kea/kea-dhcp4.conf"] COPY ["kea-dhcp4-init.conf", "/etc/kea/kea-dhcp4.conf"]

View File

@ -12,7 +12,6 @@ hostname=$(hostname)
sonic-cfggen -d -t /usr/share/sonic/templates/rsyslog.conf.j2 \ sonic-cfggen -d -t /usr/share/sonic/templates/rsyslog.conf.j2 \
-a "{\"udp_server_ip\": \"$udp_server_ip\", \"hostname\": \"$hostname\"}" \ -a "{\"udp_server_ip\": \"$udp_server_ip\", \"hostname\": \"$hostname\"}" \
> /etc/rsyslog.conf > /etc/rsyslog.conf
sonic-cfggen -d -t /usr/share/sonic/templates/port-name-alias-map.txt.j2,/tmp/port-name-alias-map.txt
# Make the script that waits for all interfaces to come up executable # Make the script that waits for all interfaces to come up executable
chmod +x /etc/kea/lease_update.sh /usr/bin/start.sh chmod +x /etc/kea/lease_update.sh /usr/bin/start.sh

View File

@ -1,8 +0,0 @@
{# Generate port name-alias map for isc-dhcp-relay to parse. Each line contains one #}
{# name-alias pair of the form "<name> <alias>" #}
{% for port, config in PORT.items() %}
{{- port }} {% if "alias" in config %}{{ config["alias"] }}{% else %}{{ port }}{% endif %} {{- "\n" -}}
{% endfor -%}
{% for pc, config in PORTCHANNEL.items() %}
{{- pc }} {{ pc }} {{- "\n" -}}
{% endfor -%}

View File

@ -7,7 +7,6 @@ import syslog
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
from dhcp_utilities.common.utils import merge_intervals, validate_str_type from dhcp_utilities.common.utils import merge_intervals, validate_str_type
PORT_MAP_PATH = "/tmp/port-name-alias-map.txt"
UNICODE_TYPE = str UNICODE_TYPE = str
DHCP_SERVER_IPV4 = "DHCP_SERVER_IPV4" DHCP_SERVER_IPV4 = "DHCP_SERVER_IPV4"
DHCP_SERVER_IPV4_CUSTOMIZED_OPTIONS = "DHCP_SERVER_IPV4_CUSTOMIZED_OPTIONS" DHCP_SERVER_IPV4_CUSTOMIZED_OPTIONS = "DHCP_SERVER_IPV4_CUSTOMIZED_OPTIONS"
@ -32,14 +31,14 @@ class DhcpServCfgGenerator(object):
lease_update_script_path = "" lease_update_script_path = ""
lease_path = "" lease_path = ""
def __init__(self, dhcp_db_connector, lease_path=DEFAULT_LEASE_PATH, port_map_path=PORT_MAP_PATH, def __init__(self, dhcp_db_connector, lease_path=DEFAULT_LEASE_PATH,
lease_update_script_path=LEASE_UPDATE_SCRIPT_PATH, dhcp_option_path=DHCP_OPTION_FILE, lease_update_script_path=LEASE_UPDATE_SCRIPT_PATH, dhcp_option_path=DHCP_OPTION_FILE,
kea_conf_template_path=KEA_DHCP4_CONF_TEMPLATE_PATH): kea_conf_template_path=KEA_DHCP4_CONF_TEMPLATE_PATH):
self.db_connector = dhcp_db_connector self.db_connector = dhcp_db_connector
self.lease_path = lease_path self.lease_path = lease_path
self.lease_update_script_path = lease_update_script_path self.lease_update_script_path = lease_update_script_path
# Read port alias map file, this file is render after container start, so it would not change any more # Read port alias map file, this file is render after container start, so it would not change any more
self._parse_port_map_alias(port_map_path) self._parse_port_map_alias()
# Get kea config template # Get kea config template
self._get_render_template(kea_conf_template_path) self._get_render_template(kea_conf_template_path)
self._read_dhcp_option(dhcp_option_path) self._read_dhcp_option(dhcp_option_path)
@ -122,14 +121,13 @@ class DhcpServCfgGenerator(object):
env = Environment(loader=FileSystemLoader(os.path.dirname(kea_conf_template_path))) # nosemgrep env = Environment(loader=FileSystemLoader(os.path.dirname(kea_conf_template_path))) # nosemgrep
self.kea_template = env.get_template(os.path.basename(kea_conf_template_path)) self.kea_template = env.get_template(os.path.basename(kea_conf_template_path))
def _parse_port_map_alias(self, port_map_path): def _parse_port_map_alias(self):
with open(port_map_path, "r") as file: port_table = self.db_connector.get_config_db_table("PORT")
lines = file.readlines() pc_table = self.db_connector.get_config_db_table("PORTCHANNEL")
for line in lines: for port_name, item in port_table.items():
splits = line.strip().split(" ") self.port_alias_map[port_name] = item.get("alias", port_name)
if len(splits) != 2: for pc_name in pc_table.keys():
continue self.port_alias_map[pc_name] = pc_name
self.port_alias_map[splits[0]] = splits[1]
def _construct_obj_for_template(self, dhcp_server_ipv4, port_ips, hostname, customized_options): def _construct_obj_for_template(self, dhcp_server_ipv4, port_ips, hostname, customized_options):
subnets = [] subnets = []

View File

@ -161,5 +161,38 @@
"192.168.0.10" "192.168.0.10"
] ]
} }
},
"PORT": {
"Ethernet0": {
"admin_status": "up",
"alias": "etp1",
"description": "Servers0:eth0",
"index": "1",
"lanes": "25",
"mtu": "9100",
"pfc_asym": "off",
"speed": "1000",
"tpid": "0x8100"
},
"Ethernet1": {
"admin_status": "up",
"alias": "etp2",
"description": "Servers1:eth0",
"index": "2",
"lanes": "26",
"mtu": "9100",
"pfc_asym": "off",
"speed": "1000",
"tpid": "0x8100"
}
},
"PORTCHANNEL": {
"PortChannel101": {
"admin_status": "up",
"lacp_key": "auto",
"min_links": "1",
"mtu": "9100",
"tpid": "0x8100"
}
} }
} }

View File

@ -1,3 +0,0 @@
Ethernet24 etp7
Ethernet28 etp8
Ethernet32

View File

@ -267,10 +267,11 @@ tested_options_data = [
def test_parse_port_alias(mock_swsscommon_dbconnector_init, mock_get_render_template): def test_parse_port_alias(mock_swsscommon_dbconnector_init, mock_get_render_template):
dhcp_db_connector = DhcpDbConnector() with patch.object(DhcpDbConnector, "get_config_db_table", side_effect=mock_get_config_db_table):
dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector, dhcp_db_connector = DhcpDbConnector()
port_map_path="tests/test_data/port-name-alias-map.txt") dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector)
assert dhcp_cfg_generator.port_alias_map == {"Ethernet24": "etp7", "Ethernet28": "etp8"} assert dhcp_cfg_generator.port_alias_map == {"Ethernet0": "etp1", "Ethernet1": "etp2",
"PortChannel101": "PortChannel101"}
@pytest.mark.parametrize("is_success", [True, False]) @pytest.mark.parametrize("is_success", [True, False])

View File

@ -29,10 +29,10 @@ def test_dump_dhcp4_config(mock_swsscommon_dbconnector_init, enabled_checker):
new_callable=PropertyMock), \ new_callable=PropertyMock), \
patch.object(DhcpServdDbMonitor, "disable_checkers") as mock_unsubscribe, \ patch.object(DhcpServdDbMonitor, "disable_checkers") as mock_unsubscribe, \
patch.object(DhcpServdDbMonitor, "enable_checkers") as mock_subscribe, \ patch.object(DhcpServdDbMonitor, "enable_checkers") as mock_subscribe, \
patch.object(DhcpServd, "enabled_checker", return_value=enabled_checker, new_callable=PropertyMock): patch.object(DhcpServd, "enabled_checker", return_value=enabled_checker, new_callable=PropertyMock), \
patch.object(DhcpServCfgGenerator, "_parse_port_map_alias"):
dhcp_db_connector = DhcpDbConnector() dhcp_db_connector = DhcpDbConnector()
dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector, dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector,
port_map_path="tests/test_data/port-name-alias-map.txt",
kea_conf_template_path="tests/test_data/kea-dhcp4.conf.j2") kea_conf_template_path="tests/test_data/kea-dhcp4.conf.j2")
dhcpservd = DhcpServd(dhcp_cfg_generator, dhcp_db_connector, None, dhcpservd = DhcpServd(dhcp_cfg_generator, dhcp_db_connector, None,
kea_dhcp4_config_path="/tmp/kea-dhcp4.conf") kea_dhcp4_config_path="/tmp/kea-dhcp4.conf")