Repository navigation
python-common: normalize hostnames to lowercase at ingest - #66805
Shubhaj1810 wants to merge 1 commit into
Conversation
|
Can one of the admins verify this patch? |
13 similar comments
|
Can one of the admins verify this patch? |
|
Can one of the admins verify this patch? |
|
Can one of the admins verify this patch? |
|
Can one of the admins verify this patch? |
|
Can one of the admins verify this patch? |
|
Can one of the admins verify this patch? |
|
Can one of the admins verify this patch? |
|
Can one of the admins verify this patch? |
|
Can one of the admins verify this patch? |
|
Can one of the admins verify this patch? |
|
Can one of the admins verify this patch? |
|
Can one of the admins verify this patch? |
|
Can one of the admins verify this patch? |
| @@ -112,6 +112,10 @@ def __str__(self) -> str: | |||
| def from_json(cls, data: Union[dict, str]) -> 'HostPlacementSpec': | |||
| if isinstance(data, str): | |||
| return cls.parse(data) | |||
| # Lowercase hostname for case-insensitive matching | |||
| if isinstance(data, dict) and 'hostname' in data: | |||
There was a problem hiding this comment.
I think this might end up throwing an error if hostname: 123 or hostname is null because None.lower() -> AttributeError, similarly int has no .lower()
You may consider something like:
if isinstance(data, dict) and 'hostname' in data and isinstance(data['hostname'], str)
There was a problem hiding this comment.
agreed, I’ll guard the .lower() call to ensure hostname is a string before normalizing.
| @@ -342,7 +350,15 @@ def set_hosts(self, hosts: Union[List[str], List[HostPlacementSpec]]) -> None: | |||
| # To backpopulate the .hosts attribute when using labels or count | |||
| # in the orchestrator backend. | |||
| if all([isinstance(host, HostPlacementSpec) for host in hosts]): | |||
There was a problem hiding this comment.
This inner isinstance() check seems to be redundant to me. A cleaner version could be:
if all(isinstance(host, HostPlacementSpec) for host in hosts):
self.hosts = [
HostPlacementSpec(hostname=h.hostname.lower(), network=h.network, name=h.name)
for h in hosts
]
There was a problem hiding this comment.
I’ll simplify this as suggested.
| @@ -90,7 +91,7 @@ def to_json(self) -> Dict[str, Any]: | |||
| def from_json(cls, host_spec: dict) -> 'HostSpec': | |||
| host_spec = cls.normalize_json(host_spec) | |||
| _cls = cls( | |||
| host_spec['hostname'], | |||
| host_spec['hostname'].lower() if 'hostname' in host_spec else '', | |||
There was a problem hiding this comment.
I think we have always expected the host to exist, if it doesn't, that should fail clearly.
Your code is returning ' ', which might cause failure later with a confusing error.
There was a problem hiding this comment.
I would suggest to keep the check as is.
There was a problem hiding this comment.
Also, since the init lowercases anyway, it is not needed here.
There was a problem hiding this comment.
Makes sense. Since HostSpec.init() already normalizes the hostname, I’ll drop the additional lowercasing here and keep the original behavior.
| #!/usr/bin/env python3 | ||
| """Test hostname case-insensitive fix""" | ||
|
|
||
| import unittest | ||
| import sys | ||
| import os | ||
|
|
||
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../..')) | ||
|
|
||
| from ceph.deployment.hostspec import HostSpec | ||
| from ceph.deployment.service_spec import HostPlacementSpec, NFSServiceSpec | ||
|
|
||
|
|
||
| class TestHostnameCaseFix(unittest.TestCase): | ||
|
|
||
| def test_bug_fix_ceph_node_case_insensitive(self): | ||
| """Verify 'Ceph-Node-00' is normalized to 'ceph-node-00'""" | ||
| # Direct HostSpec test | ||
| host = HostSpec(hostname="Ceph-Node-00") | ||
| self.assertEqual(host.hostname, "ceph-node-00") | ||
|
|
||
| # Service spec test (the actual bug scenario) | ||
| spec = NFSServiceSpec.from_json({ | ||
| 'service_type': 'nfs', | ||
| 'service_id': 'test', | ||
| 'placement': {'hosts': ['Ceph-Node-00']} | ||
| }) | ||
|
|
||
| hosts = [h.hostname for h in spec.placement.hosts] | ||
| self.assertEqual(hosts[0], 'ceph-node-00') | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
There was a problem hiding this comment.
I would suggest to add the test case inside /ceph/src/python-common/ceph/tests/test_hostspec.py.
There was a problem hiding this comment.
Sure, I’ll move the test into test_hostspec.py to keep related coverage together.
aa5eb35 to
ee3f649
Compare
phlogistonjohn
left a comment
There was a problem hiding this comment.
-
it might be good to make it clear what the scope of the changes are. I saw 'python-common' and immediately stopped to look to see how extensive the changes were. Mentioning the HostSpec and related types in the subject or commit message might help.
-
There's some confused looking type annotations in the modified code. I usually encourage folks to update the type annotations in code that is being modified.
| @@ -52,10 +52,10 @@ def __init__(self, | |||
| self.service_type = 'host' | |||
|
|
|||
| #: the bare hostname on the host. Not the FQDN. | |||
| self.hostname = hostname # type: str | |||
| self.hostname = hostname.lower() if isinstance(hostname, str) else hostname # type: str | |||
There was a problem hiding this comment.
What other type can it be?
Note the old-style type annotation on this line.
| # Type assertion for mypy | ||
| hosts_typed = hosts # type: List[HostPlacementSpec] # type: ignore |
There was a problem hiding this comment.
Hmm? I'm not sure what's going on here with the repeated type comments.
|
|
||
| #: DNS name or IP address to reach it | ||
| self.addr = addr or hostname # type: str | ||
| self.addr = addr or (hostname.lower() if isinstance(hostname, str) else hostname) # type: str |
| @@ -145,7 +148,8 @@ def parse(cls, host, require_network=True): | |||
|
|
|||
| match_host = re.search(host_re, host) | |||
| if match_host: | |||
| host_spec = host_spec._replace(hostname=match_host.group(1)) | |||
| # Lowercase for case-insensitive matching | |||
| host_spec = host_spec._replace(hostname=match_host.group(1).lower()) | |||
There was a problem hiding this comment.
If you keep needing to do the same thing(s) over and over it might make more sense to add a new method. host_spec.normalized() for example. This also "future proofs" things if you ever need to to handle more than just case.
…hing Normalize HostSpec and related placement parsing to ensure hostnames are treated case-insensitively when supplied via service specs. This prevents service placement failures caused by mixed-case hostnames. Fixes: https://tracker.ceph.com/issues/74299 Signed-off-by: Shubha Jain <[email protected]>
ee3f649 to
cf837dc
Compare
|
Closing this PR in favor of a cleanly rebased replacement. The updated PR is here: #66820 All review feedback has been addressed and the diff is now limited to |
Summary
Normalize hostnames to lowercase at ingest to ensure case-insensitive
handling across python-common placement logic.
Problem
Service placement could fail with "Unknown hosts" when mixed-case
hostnames were used in service specs while the host was registered
in lowercase.
Solution
Testing
Fixes: https://tracker.ceph.com/issues/74299
Testing Logs
BEFORE FIX — Reproduce the Issue
AFTER FIX:
Contribution Guidelines
To sign and title your commits, please refer to Submitting Patches to Ceph.
If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.
When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an
xbetween the brackets:[x]. Spaces and capitalization matter when checking off items this way.Checklist
Show available Jenkins commands
jenkins test classic perfJenkins Job | Jenkins Job Definitionjenkins test crimson perfJenkins Job | Jenkins Job Definitionjenkins test signedJenkins Job | Jenkins Job Definitionjenkins test make checkJenkins Job | Jenkins Job Definitionjenkins test make check arm64Jenkins Job | Jenkins Job Definitionjenkins test submodulesJenkins Job | Jenkins Job Definitionjenkins test dashboardJenkins Job | Jenkins Job Definitionjenkins test dashboard cephadmJenkins Job | Jenkins Job Definitionjenkins test apiJenkins Job | Jenkins Job Definitionjenkins test docsReadTheDocs | Github Workflow Definitionjenkins test ceph-volume allJenkins Jobs | Jenkins Jobs Definitionjenkins test windowsJenkins Job | Jenkins Job Definitionjenkins test rook e2eJenkins Job | Jenkins Job DefinitionYou must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.