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
|
from typing import Dict, List
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
import json_logging # type: ignore
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
|
||||||
import json_logging
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from pydantic import Field, SecretStr
|
from pydantic import Field, SecretStr
|
||||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
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))
|
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))
|
return list(filter(lambda a: ip_address(a).version == 4, addresses))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -55,7 +55,7 @@ async def tailscale_devices() -> List:
|
||||||
return r.json()["devices"]
|
return r.json()["devices"]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(
|
log.error(
|
||||||
f"Polling tailscale devices failed!",
|
"Polling tailscale devices failed!",
|
||||||
exc_info=e,
|
exc_info=e,
|
||||||
)
|
)
|
||||||
return []
|
return []
|
||||||
|
|
@ -70,7 +70,7 @@ def group_by_type(input_list):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def tailscale_labels(device) -> Dict:
|
def tailscale_labels(device) -> Dict[str, str]:
|
||||||
return {
|
return {
|
||||||
"__meta_tailscale_device_client_version": device["clientVersion"],
|
"__meta_tailscale_device_client_version": device["clientVersion"],
|
||||||
"__meta_tailscale_device_hostname": device["hostname"],
|
"__meta_tailscale_device_hostname": device["hostname"],
|
||||||
|
|
@ -83,7 +83,7 @@ def tailscale_labels(device) -> Dict:
|
||||||
|
|
||||||
|
|
||||||
async def matrix_node_sd(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:
|
async with httpx.AsyncClient() as client:
|
||||||
r = await client.get(f"http://{ipv4}:8081/")
|
r = await client.get(f"http://{ipv4}:8081/")
|
||||||
data = r.json()
|
data = r.json()
|
||||||
|
|
@ -93,7 +93,7 @@ async def matrix_node_sd(device) -> Dict:
|
||||||
def matrix_workers_to_sd(device, workers) -> List:
|
def matrix_workers_to_sd(device, workers) -> List:
|
||||||
if len(workers) == 0:
|
if len(workers) == 0:
|
||||||
return []
|
return []
|
||||||
ipv4 = filter_ipv6(device["addresses"])[0]
|
ipv4 = ipv4_only(device["addresses"])[0]
|
||||||
target_groups = []
|
target_groups = []
|
||||||
for worker_type, workers in workers.items():
|
for worker_type, workers in workers.items():
|
||||||
targets = []
|
targets = []
|
||||||
|
|
@ -131,7 +131,7 @@ async def matrix_sd(devices) -> List:
|
||||||
f"Failed parsing matrix node sd for device={device['hostname']}",
|
f"Failed parsing matrix node sd for device={device['hostname']}",
|
||||||
exc_info=e,
|
exc_info=e,
|
||||||
)
|
)
|
||||||
workers = []
|
workers = {}
|
||||||
targets = matrix_workers_to_sd(device, workers)
|
targets = matrix_workers_to_sd(device, workers)
|
||||||
if targets:
|
if targets:
|
||||||
sd.append(targets)
|
sd.append(targets)
|
||||||
|
|
@ -141,11 +141,9 @@ async def matrix_sd(devices) -> List:
|
||||||
def plain_devices_sd(devices) -> List:
|
def plain_devices_sd(devices) -> List:
|
||||||
sd = []
|
sd = []
|
||||||
for device in devices:
|
for device in devices:
|
||||||
service = {}
|
targets = ipv4_only(device["addresses"])
|
||||||
service["targets"] = filter_ipv6(device["addresses"])
|
labels = tailscale_labels(device)
|
||||||
service["labels"] = tailscale_labels(device)
|
sd.append({"labels": labels, "targets": targets})
|
||||||
|
|
||||||
sd.append(service)
|
|
||||||
|
|
||||||
return sd
|
return sd
|
||||||
|
|
||||||
|
|
@ -161,7 +159,7 @@ async def poll_sd():
|
||||||
await asyncio.sleep(settings.interval)
|
await asyncio.sleep(settings.interval)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(
|
log.error(
|
||||||
f"service discovery poller failed",
|
"Service Discovery poller failed",
|
||||||
exc_info=e,
|
exc_info=e,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue