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

[neutron-dhcp-agent] creating resolv.conf in namespaces #68

Merged
merged 2 commits into from
Oct 30, 2024

Conversation

mutax
Copy link

@mutax mutax commented Aug 1, 2024

Using the 'regular' /etc/resolv.conf inside the network namespaces prevents proper DNS resolution, as these nameservers usually are unreachable from within the namespaces.

This change enables creation of a resolv.conf in each namespace.

The new setting netns_resolvconf enables this feature, by default its disabled.

Default nameservers are 127.0.0.1 and ::1 if IPv6 is enabled, for search domains the dns_domain is used if set.

These internal defaults can be overwritten via settings file.

To skip setting nameservers, search domains or options in the generated file and not use the dynamic defaults, one can add the settings key in the configuration file but leave out any value, e.g.:

netns_resolvconf = yes
netns_resolvconf_search =
netns_resolvconf_options =
netns_resolvconf_nameservers =

This would create an empty resolv.conf.

@mutax mutax force-pushed the i583863/yoga-m3-netns-resolvconf branch from f7ee498 to b3a8c78 Compare August 1, 2024 10:20
Copy link
Collaborator

@sebageek sebageek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you thought about a test for when we set up a DHCP agent on the netns and make sure the file is written?

neutron/agent/linux/ip_lib.py Outdated Show resolved Hide resolved
neutron/agent/linux/ip_lib.py Outdated Show resolved Hide resolved
neutron/agent/linux/ip_lib.py Outdated Show resolved Hide resolved
neutron/agent/linux/ip_lib.py Outdated Show resolved Hide resolved
neutron/agent/linux/ip_lib.py Outdated Show resolved Hide resolved
neutron/agent/linux/ip_lib.py Outdated Show resolved Hide resolved
neutron/agent/linux/ip_lib.py Outdated Show resolved Hide resolved
neutron/agent/linux/ip_lib.py Outdated Show resolved Hide resolved
@mutax
Copy link
Author

mutax commented Aug 1, 2024

Have you thought about a test for when we set up a DHCP agent on the netns and make sure the file is written?

that is indeed the next item on the agenda :)

update: tests are there now

@mutax mutax force-pushed the i583863/yoga-m3-netns-resolvconf branch 16 times, most recently from 1c87735 to d06770c Compare August 7, 2024 10:55
@mutax mutax force-pushed the i583863/yoga-m3-netns-resolvconf branch from d06770c to 70fbc5f Compare August 7, 2024 12:23
neutron/agent/linux/dhcp.py Outdated Show resolved Hide resolved
neutron/agent/linux/dhcp.py Outdated Show resolved Hide resolved
neutron/agent/linux/dhcp.py Outdated Show resolved Hide resolved
neutron/tests/unit/agent/dhcp/test_agent.py Show resolved Hide resolved
@mutax mutax force-pushed the i583863/yoga-m3-netns-resolvconf branch 3 times, most recently from 6c0954f to 09ca166 Compare August 8, 2024 11:47
@mutax mutax force-pushed the i583863/yoga-m3-netns-resolvconf branch 3 times, most recently from 148a699 to 9d08630 Compare August 30, 2024 16:30
Copy link
Collaborator

@sebageek sebageek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good, added a couple of comments. When the PR is out of WIP I'll review the tests as well.

neutron/conf/agent/dhcp.py Outdated Show resolved Hide resolved
neutron/conf/agent/dhcp.py Outdated Show resolved Hide resolved
neutron/conf/agent/dhcp.py Outdated Show resolved Hide resolved
neutron/conf/agent/dhcp.py Show resolved Hide resolved
neutron/agent/linux/dhcp.py Outdated Show resolved Hide resolved
neutron/agent/linux/dhcp.py Outdated Show resolved Hide resolved
neutron/agent/linux/dhcp.py Show resolved Hide resolved
@mutax mutax force-pushed the i583863/yoga-m3-netns-resolvconf branch from 9d08630 to 8754d7e Compare September 2, 2024 14:31
@mutax mutax force-pushed the i583863/yoga-m3-netns-resolvconf branch 2 times, most recently from 93542ab to 47bf728 Compare September 2, 2024 16:04
@mutax mutax changed the title [neutron-agent] wip: creating resolv.conf in namespaces [neutron-dhcp-agent] creating resolv.conf in namespaces Sep 2, 2024
@mutax mutax marked this pull request as ready for review September 2, 2024 16:05
@mutax mutax force-pushed the i583863/yoga-m3-netns-resolvconf branch from 47bf728 to a74153b Compare September 2, 2024 16:45
@mutax mutax force-pushed the i583863/yoga-m3-netns-resolvconf branch from a74153b to d6ed0c9 Compare September 17, 2024 14:12
Copy link

@sven-rosenzweig sven-rosenzweig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the tests

@mutax mutax force-pushed the i583863/yoga-m3-netns-resolvconf branch from d6ed0c9 to 4df8ec3 Compare October 9, 2024 09:53
neutron/conf/agent/dhcp.py Outdated Show resolved Hide resolved
neutron/tests/unit/agent/dhcp/test_agent.py Outdated Show resolved Hide resolved
neutron/agent/linux/dhcp.py Outdated Show resolved Hide resolved
neutron/tests/unit/agent/dhcp/test_agent.py Outdated Show resolved Hide resolved
neutron/tests/unit/agent/dhcp/test_agent.py Outdated Show resolved Hide resolved
Comment on lines 2660 to 2665
for line in f.readlines():
if ' ' in line:
keyword, value = line.strip().split(' ', 1)
if keyword in ('search', 'options',
'nameserver'):
found_settings.add((keyword, value))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[optional] If this were a resolv.conf parsing function like described above you could also just return the options as a set and do something like assertEqual(set(), settings - set(self._parse_resolv_conf(resolvconf)))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would not catch the case where the resulting resolv.conf has more settings than intended:

In [1]: set([1,2,3]) - set([1,2,3,4])
Out[1]: set()

what is the advantage for that approach? Given that the assertion will give a verbose error message, e.g.:

AssertionError: Items in the first set but not the second:
('nameserver', '::1')

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The advantage would be that you could write one "resolv.conf parsing function" and use it everywhere and that I like set operations. We can keep it as is though :) For the case you're mentioning you can just compare the sets if they're equal or if you want the extra elements, do an xor:

In [2]: set([1,2,3]) ^ set([1,2,3,4])
Out[2]: {4}

Anyway, as marked as optional you can also keep it as is.

Copy link
Author

@mutax mutax Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented the resolv.conf parsing function, that totally makes sense, even to me ;)
I just don't get why I should do the set operation myself when the unittest framework is giving me nice output already when comparing the sets

neutron/tests/unit/agent/dhcp/test_agent.py Outdated Show resolved Hide resolved
neutron/agent/linux/dhcp.py Outdated Show resolved Hide resolved
Comment on lines 2660 to 2665
for line in f.readlines():
if ' ' in line:
keyword, value = line.strip().split(' ', 1)
if keyword in ('search', 'options',
'nameserver'):
found_settings.add((keyword, value))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The advantage would be that you could write one "resolv.conf parsing function" and use it everywhere and that I like set operations. We can keep it as is though :) For the case you're mentioning you can just compare the sets if they're equal or if you want the extra elements, do an xor:

In [2]: set([1,2,3]) ^ set([1,2,3,4])
Out[2]: {4}

Anyway, as marked as optional you can also keep it as is.

neutron/tests/unit/agent/dhcp/test_agent.py Outdated Show resolved Hide resolved
Comment on lines 2706 to 2711
with open(resolvconf, 'rt') as f:
for line in f.readlines():
if ' ' in line:
keyword, value = line.strip().split(' ', 1)
if keyword in ('search', 'options', 'nameserver'):
found_settings.add((keyword, value))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this also be covered by _read_resolvconf()?

Copy link
Author

@mutax mutax Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm. need to adapt _read_resolvconf for that, but is doable,

The test is only looking for the given three keywords and ignoring everything else in the file, so it would only fail if the setting for one of these keywords is not correct, making the test specific to that corner case.

If for example the code adds an additional keyword (because an additional linebreak is added by accident in a comment when the code is modified), now all tests that check the resolv.conf will fail, because a new keyword-value pair is found by the helper function that parses the file. This could make it unneccessarily hard for the developer to verify the root cause with 4 tests failing.

The parser has been updated and got its own test, so that should work in the same way as before, with a parameter to the parser function.

@mutax mutax force-pushed the i583863/yoga-m3-netns-resolvconf branch 2 times, most recently from 9d386f0 to 6170f65 Compare October 10, 2024 14:16
Using the 'regular' /etc/resolv.conf inside the network namespaces
prevents proper DNS resolution, as these nameservers usually are
unreachable from within the namespaces.

This change enables creation of a resolv.conf in each namespace.

The new setting netns_resolvconf enables this feature, by default it is
disabled.

Default nameservers are 127.0.0.1 and ::1 if IPv6 is enabled, for search
domains the networks dns_domain is used if set.

These defaults can be overwritten via settings file.

To skip setting nameservers, search domains or options in the generated
file and not use the dynamic defaults, one can add the settings key in
the configuration file but leave out any value, e.g.:

  netns_resolvconf = yes
  netns_resolvconf_search =
  netns_resolvconf_options =
  netns_resolvconf_nameservers =

This would create an empty resolv.conf.
@mutax mutax force-pushed the i583863/yoga-m3-netns-resolvconf branch from 6170f65 to ad82d7b Compare October 10, 2024 15:06
@mutax mutax merged commit 99d3d04 into stable/yoga-m3 Oct 30, 2024
2 checks passed
@mutax mutax deleted the i583863/yoga-m3-netns-resolvconf branch October 30, 2024 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants