dnsdisc

DNS Discovery module.

exception spicerack.dnsdisc.DiscoveryCheckError[source]

Bases: SpicerackCheckError

Custom exception class for errors while performing checks.

exception spicerack.dnsdisc.DiscoveryError[source]

Bases: SpicerackError

Custom exception class for errors of the Discovery class.

class spicerack.dnsdisc.Discovery(*, conftool: spicerack.confctl.ConftoolEntity, authdns_servers: dict[str, str], records: list[str], dry_run: bool = True) None[source]

Bases: object

Class to manage Confctl discovery objects.

Initialize the instance.

Parameters:
  • conftool (spicerack.confctl.ConftoolEntity) -- the conftool instance for the discovery type objects.

  • authdns_servers (dict[str, str]) -- a dictionary where keys are the hostnames and values are the IPs of the authoritative nameservers to be used.

  • records (list[str]) -- list of strings, each one must be a Discovery DNS record name.

  • dry_run (bool, default: True) -- whether this is a DRY-RUN.

Raises:

spicerack.dnsdisc.DiscoveryError -- if unable to initialize the resolvers.

check_if_depoolable(datacenter: str) None[source]

Determine if a datacenter can be depooled for all records.

Parameters:

datacenter (str) -- the datacenter to depool.

Raises:

spicerack.dnsdisc.DiscoveryError -- if any service cannot be depooled.

Return type:

None

check_record(name: str, expected_name: str) None[source]

Check that a Discovery record resolves on all authoritative resolvers to the correct IP.

The IP to use for the comparison is obtained resolving the expected_name record. For example with name='servicename-rw.discovery.wmnet' and expected_name='servicename.svc.eqiad.wmnet', this method will resolve the 'expected_name' to get its IP address and then verify that on all authoritative resolvers the record for 'name' resolves to the same IP. It is retried to allow the change to be propagated through all authoritative resolvers.

Parameters:
  • name (str) -- the record to check the resolution for.

  • expected_name (str) -- the name of a record to be resolved and used as the expected address.

Raises:

spicerack.dnsdisc.DiscoveryError -- if the record doesn't match the IP of the expected_name.

Return type:

None

check_ttl(ttl: int) None[source]

Check the TTL for all records.

Parameters:

ttl (int) -- the expected TTL value.

Raises:

spicerack.dnsdisc.DiscoveryError -- if the expected TTL is not found.

Return type:

None

depool(datacenter: str) None[source]

Set the records as depooled in the given datacenter.

Parameters:

datacenter (str) -- the DC from which to depool the discovery records.

Return type:

None

pool(datacenter: str) None[source]

Set the records as pooled in the given datacenter.

Parameters:

datacenter (str) -- the DC in which to pool the discovery records.

Return type:

None

resolve(name: str | None = None) collections.abc.Iterator[dns.resolver.Answer][source]

Generator that yields the resolved records.

Todo

move a more generalized version of this into a DNS resolver module.

Parameters:

name (typing.Optional[str], default: None) -- record name to use for the resolution instead of self.records.

Yields:

dns.resolver.Answer -- the DNS response.

Raises:

spicerack.dnsdic.DiscoveryError -- if unable to resolve the address.

Return type:

collections.abc.Iterator[dns.resolver.Answer]

resolve_address(name: str) str[source]

Resolve the IP of a given record.

Todo

move a more generalized version of this into a DNS resolver module.

Parameters:

name (str) -- the DNS record to resolve.

Raises:

spicerack.dnsdisc.DiscoveryError -- if unable to resolve the address.

Return type:

str

resolve_with_client_ip(record: str, client_ip: ipaddress.IPv4Address | ipaddress.IPv6Address) dict[str, ipaddress.IPv4Address | ipaddress.IPv6Address][source]

Resolves a discovery record with a specific client IP and returns the resolved address grouped by nameserver.

Parameters:
Raises:

spicerack.discovery.DiscoveryError -- if unable to resolve the address.

Return type:

dict[str, typing.Union[ipaddress.IPv4Address, ipaddress.IPv6Address]]

update_ttl(ttl: int) None[source]

Update the TTL for all registered records.

Parameters:

ttl (int) -- the new TTL value to set.

Raises:

spicerack.dnsdisc.DiscoveryError -- if the check of the modified TTL fail and not in DRY-RUN mode.

Return type:

None

property active_datacenters: defaultdict

Information about pooled state of services.

Returns:

A map of services, with values given by a list of datacenters where the service is pooled, i.e.:

{
    'svc_foo': ['dc1', 'dc2'],
    'svc_bar': ['dc1'],
}