Skip to content

Commit

Permalink
simpler code
Browse files Browse the repository at this point in the history
  • Loading branch information
ekampf committed May 13, 2024
1 parent 24c1db9 commit 76187e7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
27 changes: 13 additions & 14 deletions app/handlers/handlers_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,19 @@ def service_to_twingate_resource(service_body, namespace) -> dict:
if value := meta.annotations.get(f"twingate.com/resource-{key}"):
result["spec"][key] = convert_f(value)

if service_ports := spec.get("ports", []):
protocols: dict = {
"allowIcmp": False,
"tcp": {"policy": "RESTRICTED", "ports": []},
"udp": {"policy": "RESTRICTED", "ports": []},
}
for port_obj in service_ports:
port = port_obj["port"]
if port_obj["protocol"] == "TCP":
protocols["tcp"]["ports"].append({"start": port, "end": port})
elif port_obj["protocol"] == "UDP":
protocols["udp"]["ports"].append({"start": port, "end": port})

result["spec"]["protocols"] = protocols
protocols: dict = {
"allowIcmp": False,
"tcp": {"policy": "RESTRICTED", "ports": []},
"udp": {"policy": "RESTRICTED", "ports": []},
}
for port_obj in spec.get("ports", []):
port = port_obj["port"]
if port_obj["protocol"] == "TCP":
protocols["tcp"]["ports"].append({"start": port, "end": port})
elif port_obj["protocol"] == "UDP":
protocols["udp"]["ports"].append({"start": port, "end": port})

result["spec"]["protocols"] = protocols

return result

Expand Down
4 changes: 1 addition & 3 deletions app/handlers/tests/test_handlers_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def example_service_body():
targetPort: 9376
name: ssh
"""
return kopf._cogs.structs.bodies.Body( # noqa: SLF001
yaml.safe_load(yaml_str)
)
return kopf.Body(yaml.safe_load(yaml_str))


@pytest.fixture()
Expand Down

0 comments on commit 76187e7

Please sign in to comment.