Skip to content

python-common: normalize hostnames to lowercase at ingest - #66805

Closed
Shubhaj1810 wants to merge 1 commit into
ceph:mainfrom
Shubhaj1810:fix-hostname-case-insensitive
Closed

Shubhaj1810 wants to merge 1 commit into
ceph:mainfrom
Shubhaj1810:fix-hostname-case-insensitive

Conversation

@Shubhaj1810

@Shubhaj1810 Shubhaj1810 commented Jan 6, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Normalize hostnames at ingest in python-common
  • Ensure placement parsing and matching is case-insensitive

Testing

  • Added unit test covering mixed-case hostname scenarios

Fixes: https://tracker.ceph.com/issues/74299

Testing Logs
BEFORE FIX — Reproduce the Issue

1. Verify registered hostnames
ceph orch host ls

> ceph-node-00
> ceph-node-01
> ceph-node-02

2. Create a service spec with mixed-case hostname
cat > case-test.yaml << 'EOF'
service_type: nfs
service_id: case-test
placement:
  hosts:
    - Ceph-Node-00
EOF

3. Apply the spec (EXPECTED TO FAIL)
ceph orch apply -i case-test.yaml


 Expected BEFORE result:

Cannot place <NFSServiceSpec ...> on Ceph-Node-00: Unknown hosts

This is the reproduced issue.

AFTER FIX:

[ceph: root@ceph-node-00 /]# ceph orch host ls 
HOST          ADDR             LABELS  STATUS   
ceph-node-00  192.168.100.100  _admin           
ceph-node-01  192.168.100.101                   
ceph-node-02  192.168.100.102                   
3 hosts in cluster 
[ceph: root@ceph-node-00 /]# cat > case-test.yaml << 'EOF' service_type: nfs service_id: case-test placement: hosts: - Ceph-Node-00 EOF 
[ceph: root@ceph-node-00 /]# ceph orch apply -i case-test.yaml 
Scheduled nfs.case-test update... 
[ceph: root@ceph-node-00 /]#  

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 x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

@Shubhaj1810
Shubhaj1810 requested a review from a team as a code owner January 6, 2026 09:27
@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

13 similar comments
@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@ceph-jenkins

Copy link
Copy Markdown
Collaborator

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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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]):

@Kushal-deb Kushal-deb Jan 6, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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
    ]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 '',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would suggest to keep the check as is.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also, since the init lowercases anyway, it is not needed here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. Since HostSpec.init() already normalizes the hostname, I’ll drop the additional lowercasing here and keep the original behavior.

Comment on lines +1 to +34
#!/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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would suggest to add the test case inside /ceph/src/python-common/ceph/tests/test_hostspec.py.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, I’ll move the test into test_hostspec.py to keep related coverage together.

@Shubhaj1810
Shubhaj1810 force-pushed the fix-hostname-case-insensitive branch 2 times, most recently from aa5eb35 to ee3f649 Compare January 6, 2026 14:34

@phlogistonjohn phlogistonjohn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  1. 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.

  2. 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What other type can it be?
Note the old-style type annotation on this line.

Comment on lines +349 to +350
# Type assertion for mypy
hosts_typed = hosts # type: List[HostPlacementSpec] # type: ignore

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same.

@@ -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())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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]>
@Shubhaj1810

Copy link
Copy Markdown
Contributor Author

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
HostSpec and related placement parsing logic.

@Shubhaj1810 Shubhaj1810 closed this Jan 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants