add device tags
This commit is contained in:
parent
2fcd13a16b
commit
f5a2ecd6fd
2 changed files with 18 additions and 8 deletions
|
|
@ -12,6 +12,8 @@ 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
|
||||||
|
|
||||||
|
MATRIX_TAG = "tag:matrix"
|
||||||
|
|
||||||
env_path = os.getenv("TAILSCALESD_ENV_FILE")
|
env_path = os.getenv("TAILSCALESD_ENV_FILE")
|
||||||
debug = os.getenv("TAILSCALESD_DEBUG", False)
|
debug = os.getenv("TAILSCALESD_DEBUG", False)
|
||||||
log = logging.getLogger("tailscalesd")
|
log = logging.getLogger("tailscalesd")
|
||||||
|
|
@ -70,8 +72,8 @@ def group_by_type(input_list):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def tailscale_labels(tailnet, device) -> Dict[str, str]:
|
def tailscale_labels(tailnet, device, tag) -> Dict[str, str]:
|
||||||
return {
|
labels = {
|
||||||
"__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"],
|
||||||
"__meta_tailscale_device_authorized": str(device["authorized"]).lower(),
|
"__meta_tailscale_device_authorized": str(device["authorized"]).lower(),
|
||||||
|
|
@ -80,6 +82,9 @@ def tailscale_labels(tailnet, device) -> Dict[str, str]:
|
||||||
"__meta_tailscale_device_os": device["os"],
|
"__meta_tailscale_device_os": device["os"],
|
||||||
"__meta_tailscale_tailnet": tailnet,
|
"__meta_tailscale_tailnet": tailnet,
|
||||||
}
|
}
|
||||||
|
if tag:
|
||||||
|
labels["__meta_tailscale_device_tag"] = tag
|
||||||
|
return labels
|
||||||
|
|
||||||
|
|
||||||
async def matrix_node_sd(device) -> Dict:
|
async def matrix_node_sd(device) -> Dict:
|
||||||
|
|
@ -107,7 +112,7 @@ def matrix_workers_to_sd(tailnet, device, workers) -> List:
|
||||||
target_groups.append(
|
target_groups.append(
|
||||||
{
|
{
|
||||||
"targets": [f"{ipv4}:{port}"],
|
"targets": [f"{ipv4}:{port}"],
|
||||||
"labels": tailscale_labels(tailnet, device)
|
"labels": tailscale_labels(tailnet, device, None)
|
||||||
| {
|
| {
|
||||||
"__meta_matrix_worker_type": worker_type,
|
"__meta_matrix_worker_type": worker_type,
|
||||||
"__meta_matrix_worker_name": worker_name,
|
"__meta_matrix_worker_name": worker_name,
|
||||||
|
|
@ -120,7 +125,7 @@ def matrix_workers_to_sd(tailnet, device, workers) -> List:
|
||||||
async def matrix_sd(tailnet, devices) -> List:
|
async def matrix_sd(tailnet, devices) -> List:
|
||||||
sd = []
|
sd = []
|
||||||
for device in devices:
|
for device in devices:
|
||||||
if "tag:matrix" not in device["tags"]:
|
if MATRIX_TAG not in device["tags"]:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
workers = await matrix_node_sd(device)
|
workers = await matrix_node_sd(device)
|
||||||
|
|
@ -140,9 +145,14 @@ def plain_devices_sd(tailnet, devices) -> List:
|
||||||
sd = []
|
sd = []
|
||||||
for device in devices:
|
for device in devices:
|
||||||
targets = ipv4_only(device["addresses"])
|
targets = ipv4_only(device["addresses"])
|
||||||
labels = tailscale_labels(tailnet, device)
|
tags = device["tags"]
|
||||||
|
if tags:
|
||||||
|
for tag in tags:
|
||||||
|
labels = tailscale_labels(tailnet, device, tag)
|
||||||
|
sd.append({"labels": labels, "targets": targets})
|
||||||
|
else:
|
||||||
|
labels = tailscale_labels(tailnet, device, None)
|
||||||
sd.append({"labels": labels, "targets": targets})
|
sd.append({"labels": labels, "targets": targets})
|
||||||
|
|
||||||
return sd
|
return sd
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,11 +41,11 @@ def test_matrix_workers_to_sd_valid_input():
|
||||||
"addresses": ["10.0.0.1", "192.168.1.1"],
|
"addresses": ["10.0.0.1", "192.168.1.1"],
|
||||||
"hostname": "test-host",
|
"hostname": "test-host",
|
||||||
"clientVersion": "1",
|
"clientVersion": "1",
|
||||||
"tags": ["tag:matrix"],
|
|
||||||
"id": "id",
|
"id": "id",
|
||||||
"name": "name",
|
"name": "name",
|
||||||
"os": "linux",
|
"os": "linux",
|
||||||
"authorized": True,
|
"authorized": True,
|
||||||
|
"tags": ["tag:test"],
|
||||||
"tailnet": "testnet",
|
"tailnet": "testnet",
|
||||||
}
|
}
|
||||||
workers = {
|
workers = {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue