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

NM renderer: set default IPv6 addr-gen-mode for all interfaces to EUI64 #4291

Merged
merged 1 commit into from
Aug 4, 2023
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
21 changes: 21 additions & 0 deletions cloudinit/net/network_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
NM_RUN_DIR = "/etc/NetworkManager"
NM_LIB_DIR = "/usr/lib/NetworkManager"
NM_CFG_FILE = "/etc/NetworkManager/NetworkManager.conf"
NM_IPV6_ADDR_GEN_CONF = """# This is generated by cloud-init. Do not edit.
#
[.config]
enable=nm-version-min:1.40
[connection.30-cloud-init-ip6-addr-gen-mode]
# Select EUI64 to be used if the profile does not specify it.
ipv6.addr-gen-mode=0

"""
LOG = logging.getLogger(__name__)


Expand Down Expand Up @@ -368,13 +377,25 @@ def render_network_state(
name = conn_filename(con_id, target)
util.write_file(name, conn.dump(), 0o600)

# Select EUI64 to be used by default by NM for creating the address
# for use with RFC4862 IPv6 Stateless Address Autoconfiguration.
util.write_file(
cloud_init_nm_conf_filename(target), NM_IPV6_ADDR_GEN_CONF, 0o600
)


def conn_filename(con_id, target=None):
target_con_dir = subp.target_path(target, NM_RUN_DIR)
con_file = f"cloud-init-{con_id}.nmconnection"
return f"{target_con_dir}/system-connections/{con_file}"


def cloud_init_nm_conf_filename(target=None):
target_con_dir = subp.target_path(target, NM_RUN_DIR)
conf_file = "30-cloud-init-ip6-addr-gen-mode.conf"
return f"{target_con_dir}/conf.d/{conf_file}"


def available(target=None):
# TODO: Move `uses_systemd` to a more appropriate location
# It is imported here to avoid circular import
Expand Down
95 changes: 76 additions & 19 deletions tests/unittests/test_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -5868,9 +5868,25 @@ class TestNetworkManagerRendering(CiTestCase):
with_logs = True

scripts_dir = "/etc/NetworkManager/system-connections"
conf_dir = "/etc/NetworkManager/conf.d"

expected_name = "expected_network_manager"

expected_conf_d = {
"30-cloud-init-ip6-addr-gen-mode.conf": textwrap.dedent(
"""\
# This is generated by cloud-init. Do not edit.
#
[.config]
enable=nm-version-min:1.40
[connection.30-cloud-init-ip6-addr-gen-mode]
# Select EUI64 to be used if the profile does not specify it.
ipv6.addr-gen-mode=0

"""
),
}

def _get_renderer(self):
return network_manager.Renderer()

Expand All @@ -5889,11 +5905,19 @@ def _render_and_read(self, network_config=None, state=None, dir=None):
renderer.render_network_state(ns, target=dir)
return dir2dict(dir)

def _compare_files_to_expected(self, expected, found):
def _compare_files_to_expected(
self, expected_scripts, expected_conf, found
):
orig_maxdiff = self.maxDiff
expected_d = dict(
(os.path.join(self.scripts_dir, k), v) for k, v in expected.items()
conf_d = dict(
(os.path.join(self.conf_dir, k), v)
for k, v in expected_conf.items()
)
scripts_d = dict(
(os.path.join(self.scripts_dir, k), v)
for k, v in expected_scripts.items()
)
expected_d = {**conf_d, **scripts_d}

try:
self.maxDiff = None
Expand Down Expand Up @@ -5954,6 +5978,7 @@ def test_default_generation(
"""
),
},
self.expected_conf_d,
found,
)

Expand Down Expand Up @@ -6009,8 +6034,9 @@ def test_network_config_v1_samples(self):
gateway=10.0.2.2

"""
),
)
},
self.expected_conf_d,
found,
)

Expand Down Expand Up @@ -6046,33 +6072,44 @@ def test_config_with_explicit_loopback(self):
"""
),
},
self.expected_conf_d,
found,
)

def test_bond_config(self):
entry = NETWORK_CONFIGS["bond"]
found = self._render_and_read(network_config=yaml.load(entry["yaml"]))
self._compare_files_to_expected(entry[self.expected_name], found)
self._compare_files_to_expected(
entry[self.expected_name], self.expected_conf_d, found
)

def test_vlan_config(self):
entry = NETWORK_CONFIGS["vlan"]
found = self._render_and_read(network_config=yaml.load(entry["yaml"]))
self._compare_files_to_expected(entry[self.expected_name], found)
self._compare_files_to_expected(
entry[self.expected_name], self.expected_conf_d, found
)

def test_bridge_config(self):
entry = NETWORK_CONFIGS["bridge"]
found = self._render_and_read(network_config=yaml.load(entry["yaml"]))
self._compare_files_to_expected(entry[self.expected_name], found)
self._compare_files_to_expected(
entry[self.expected_name], self.expected_conf_d, found
)

def test_manual_config(self):
entry = NETWORK_CONFIGS["manual"]
found = self._render_and_read(network_config=yaml.load(entry["yaml"]))
self._compare_files_to_expected(entry[self.expected_name], found)
self._compare_files_to_expected(
entry[self.expected_name], self.expected_conf_d, found
)

def test_all_config(self):
entry = NETWORK_CONFIGS["all"]
found = self._render_and_read(network_config=yaml.load(entry["yaml"]))
self._compare_files_to_expected(entry[self.expected_name], found)
self._compare_files_to_expected(
entry[self.expected_name], self.expected_conf_d, found
)
self.assertNotIn(
"WARNING: Network config: ignoring eth0.101 device-level mtu",
self.logs.getvalue(),
Expand All @@ -6081,17 +6118,23 @@ def test_all_config(self):
def test_small_config_v1(self):
entry = NETWORK_CONFIGS["small_v1"]
found = self._render_and_read(network_config=yaml.load(entry["yaml"]))
self._compare_files_to_expected(entry[self.expected_name], found)
self._compare_files_to_expected(
entry[self.expected_name], self.expected_conf_d, found
)

def test_small_config_v2(self):
entry = NETWORK_CONFIGS["small_v2"]
found = self._render_and_read(network_config=yaml.load(entry["yaml"]))
self._compare_files_to_expected(entry[self.expected_name], found)
self._compare_files_to_expected(
entry[self.expected_name], self.expected_conf_d, found
)

def test_v4_and_v6_static_config(self):
entry = NETWORK_CONFIGS["v4_and_v6_static"]
found = self._render_and_read(network_config=yaml.load(entry["yaml"]))
self._compare_files_to_expected(entry[self.expected_name], found)
self._compare_files_to_expected(
entry[self.expected_name], self.expected_conf_d, found
)
expected_msg = (
"WARNING: Network config: ignoring iface0 device-level mtu:8999"
" because ipv4 subnet-level mtu:9000 provided."
Expand All @@ -6101,41 +6144,55 @@ def test_v4_and_v6_static_config(self):
def test_dhcpv6_only_config(self):
entry = NETWORK_CONFIGS["dhcpv6_only"]
found = self._render_and_read(network_config=yaml.load(entry["yaml"]))
self._compare_files_to_expected(entry[self.expected_name], found)
self._compare_files_to_expected(
entry[self.expected_name], self.expected_conf_d, found
)

def test_simple_render_ipv6_slaac(self):
entry = NETWORK_CONFIGS["ipv6_slaac"]
found = self._render_and_read(network_config=yaml.load(entry["yaml"]))
self._compare_files_to_expected(entry[self.expected_name], found)
self._compare_files_to_expected(
entry[self.expected_name], self.expected_conf_d, found
)

def test_dhcpv6_stateless_config(self):
entry = NETWORK_CONFIGS["dhcpv6_stateless"]
found = self._render_and_read(network_config=yaml.load(entry["yaml"]))
self._compare_files_to_expected(entry[self.expected_name], found)
self._compare_files_to_expected(
entry[self.expected_name], self.expected_conf_d, found
)

def test_wakeonlan_disabled_config_v2(self):
entry = NETWORK_CONFIGS["wakeonlan_disabled"]
found = self._render_and_read(
network_config=yaml.load(entry["yaml_v2"])
)
self._compare_files_to_expected(entry[self.expected_name], found)
self._compare_files_to_expected(
entry[self.expected_name], self.expected_conf_d, found
)

def test_wakeonlan_enabled_config_v2(self):
entry = NETWORK_CONFIGS["wakeonlan_enabled"]
found = self._render_and_read(
network_config=yaml.load(entry["yaml_v2"])
)
self._compare_files_to_expected(entry[self.expected_name], found)
self._compare_files_to_expected(
entry[self.expected_name], self.expected_conf_d, found
)

def test_render_v4_and_v6(self):
entry = NETWORK_CONFIGS["v4_and_v6"]
found = self._render_and_read(network_config=yaml.load(entry["yaml"]))
self._compare_files_to_expected(entry[self.expected_name], found)
self._compare_files_to_expected(
entry[self.expected_name], self.expected_conf_d, found
)

def test_render_v6_and_v4(self):
entry = NETWORK_CONFIGS["v6_and_v4"]
found = self._render_and_read(network_config=yaml.load(entry["yaml"]))
self._compare_files_to_expected(entry[self.expected_name], found)
self._compare_files_to_expected(
entry[self.expected_name], self.expected_conf_d, found
)


@mock.patch(
Expand Down
Loading