Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-5070

DNS SRV records needs FQDN for no reason

    • Type: Icon: Task Task
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Python Drivers

      Context

      I lost 4 hours today trying to connect to MongoDB cluster inside Kubernetes because PyMongo driver is broken (read: has undocumented "feature") and needs FQDN without even telling that.

      I tried connecting to my MongoDB via `mongodb+srv://myapp-mongodb-headless/?tls=false&replicaSet=rs0` and it didn't worked despite having `dnspython` already installed. All I got, was this weird error:

        File "/venv/lib/python3.11/site-packages/pymongo/mongo_client.py", line 736, in __init__
          res = uri_parser.parse_uri(
                ^^^^^^^^^^^^^^^^^^^^^
        File "/venv/lib/python3.11/site-packages/pymongo/uri_parser.py", line 541, in parse_uri
          dns_resolver = _SrvResolver(fqdn, connect_timeout, srv_service_name, srv_max_hosts)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/venv/lib/python3.11/site-packages/pymongo/srv_resolver.py", line 73, in __init__
          raise ConfigurationError(_INVALID_HOST_MSG % (fqdn,))
      pymongo.errors.ConfigurationError: Invalid URI host: myapp-mongodb-headless is not a valid hostname for 'mongodb+srv://'. Did you mean to use 'mongodb://'?

      Going into source code of pymongo actually showed root cause, which is this line which checks if supplied address is FQDN for no apparent reason.

      Keep in mind, that the error message said about "invalid HOSTNAME", not "FQDN"!!! And hostname is 100% correct here! It's not just FQDN as code expects (in contrast of error message; altough, variable is called `fqdn` so... it's even more ambiguous).

      And that breaks Kubernetes deployment, as Kubernetes supports DNS search, and it resolves DNS correctly from `myapp-mongodb-headless` into `myapp-mongodb-headless.mynamespace.svc.cluster.local`. And being able to use just hostname instead of FQDN is nice, because I don't need to hardcode my namespace anywhere nor write template to gather that namespace name.

       

      Definition of done

      PyMongo should allow to use just hostname in mongodb+srv:// scheme.

      Or if there is reason why it's not possible, it should be documented.

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            mosonkonrad@gmail.com Konrad Mosoń
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: