appease the linter
This commit is contained in:
parent
087087bf87
commit
44e681dd13
1 changed files with 12 additions and 14 deletions
|
|
@ -6,9 +6,8 @@ from ipaddress import ip_address
|
|||
from typing import Dict, List
|
||||
|
||||
import httpx
|
||||
import json_logging # type: ignore
|
||||
import uvicorn
|
||||
|
||||
import json_logging
|
||||
from fastapi import FastAPI
|
||||
from pydantic import Field, SecretStr
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
|
@ -20,7 +19,8 @@ log.setLevel(logging.DEBUG if debug else logging.INFO)
|
|||
log.addHandler(logging.StreamHandler(sys.stdout))
|
||||
|
||||
|
||||
def filter_ipv6(addresses):
|
||||
def ipv4_only(addresses) -> List[str]:
|
||||
"""Given a list of ip addresses, returns only the ipv4 ones"""
|
||||
return list(filter(lambda a: ip_address(a).version == 4, addresses))
|
||||
|
||||
|
||||
|
|
@ -55,7 +55,7 @@ async def tailscale_devices() -> List:
|
|||
return r.json()["devices"]
|
||||
except Exception as e:
|
||||
log.error(
|
||||
f"Polling tailscale devices failed!",
|
||||
"Polling tailscale devices failed!",
|
||||
exc_info=e,
|
||||
)
|
||||
return []
|
||||
|
|
@ -70,7 +70,7 @@ def group_by_type(input_list):
|
|||
return result
|
||||
|
||||
|
||||
def tailscale_labels(device) -> Dict:
|
||||
def tailscale_labels(device) -> Dict[str, str]:
|
||||
return {
|
||||
"__meta_tailscale_device_client_version": device["clientVersion"],
|
||||
"__meta_tailscale_device_hostname": device["hostname"],
|
||||
|
|
@ -83,7 +83,7 @@ def tailscale_labels(device) -> Dict:
|
|||
|
||||
|
||||
async def matrix_node_sd(device) -> Dict:
|
||||
ipv4 = filter_ipv6(device["addresses"])[0]
|
||||
ipv4 = ipv4_only(device["addresses"])[0]
|
||||
async with httpx.AsyncClient() as client:
|
||||
r = await client.get(f"http://{ipv4}:8081/")
|
||||
data = r.json()
|
||||
|
|
@ -93,7 +93,7 @@ async def matrix_node_sd(device) -> Dict:
|
|||
def matrix_workers_to_sd(device, workers) -> List:
|
||||
if len(workers) == 0:
|
||||
return []
|
||||
ipv4 = filter_ipv6(device["addresses"])[0]
|
||||
ipv4 = ipv4_only(device["addresses"])[0]
|
||||
target_groups = []
|
||||
for worker_type, workers in workers.items():
|
||||
targets = []
|
||||
|
|
@ -131,7 +131,7 @@ async def matrix_sd(devices) -> List:
|
|||
f"Failed parsing matrix node sd for device={device['hostname']}",
|
||||
exc_info=e,
|
||||
)
|
||||
workers = []
|
||||
workers = {}
|
||||
targets = matrix_workers_to_sd(device, workers)
|
||||
if targets:
|
||||
sd.append(targets)
|
||||
|
|
@ -141,11 +141,9 @@ async def matrix_sd(devices) -> List:
|
|||
def plain_devices_sd(devices) -> List:
|
||||
sd = []
|
||||
for device in devices:
|
||||
service = {}
|
||||
service["targets"] = filter_ipv6(device["addresses"])
|
||||
service["labels"] = tailscale_labels(device)
|
||||
|
||||
sd.append(service)
|
||||
targets = ipv4_only(device["addresses"])
|
||||
labels = tailscale_labels(device)
|
||||
sd.append({"labels": labels, "targets": targets})
|
||||
|
||||
return sd
|
||||
|
||||
|
|
@ -161,7 +159,7 @@ async def poll_sd():
|
|||
await asyncio.sleep(settings.interval)
|
||||
except Exception as e:
|
||||
log.error(
|
||||
f"service discovery poller failed",
|
||||
"Service Discovery poller failed",
|
||||
exc_info=e,
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue