Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 endpoints-discovery: avoid duplicated urls #4706

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions reconcile/endpoints_discovery/integration.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from collections import defaultdict
from collections.abc import Callable, Iterable
from typing import TypedDict

Expand Down Expand Up @@ -43,7 +44,7 @@
from reconcile.utils.vcs import VCS

QONTRACT_INTEGRATION = "endpoints-discovery"
QONTRACT_INTEGRATION_VERSION = make_semver(1, 0, 0)
QONTRACT_INTEGRATION_VERSION = make_semver(1, 0, 1)


class EndpointsDiscoveryIntegrationParams(PydanticRunParams):
Expand Down Expand Up @@ -137,13 +138,21 @@ def get_routes(self, oc_map: OCMap, namespace: NamespaceV1) -> list[Route]:
)
return []

routes = defaultdict(list)
for item in oc.get_items(kind="Route", namespace=namespace.name):
tls = bool(item["spec"].get("tls"))
host = item["spec"]["host"]
# group all routes with the same hostname/tls
routes[(host, tls)].append(item["metadata"]["name"])

# merge all routes with the same hostname into one and combine the names
return [
Route(
name=item["metadata"]["name"],
host=item["spec"]["host"],
tls=bool(item["spec"].get("tls")),
name="|".join(sorted(names)),
host=host,
tls=tls,
)
for item in oc.get_items(kind="Route", namespace=namespace.name)
for (host, tls), names in routes.items()
]

def get_endpoint_changes(
Expand Down
6 changes: 5 additions & 1 deletion reconcile/test/endpoints_discovery/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Callable, Mapping
from copy import deepcopy
from typing import Any

import pytest
Expand Down Expand Up @@ -68,7 +69,10 @@ def fake_route() -> dict[str, Any]:
def oc(mocker: MockerFixture, fake_route: dict[str, Any]) -> OCNative:
oc = mocker.patch("reconcile.utils.oc.OCNative", autospec=True)
oc.project_exists.return_value = True
oc.get_items.return_value = [fake_route]
# return 2 routes with the same hostname. this should be filtered out by get_routes
fake_route2 = deepcopy(fake_route)
fake_route2["metadata"]["name"] = "zzz-fake-route"
oc.get_items.return_value = [fake_route, fake_route2]
return oc


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_endpoints_discovery_integration_get_routes(
# see fake_route fixture!
assert len(routes) == 1
assert isinstance(routes[0], Route)
assert routes[0].name == "fake-route"
assert routes[0].name == "fake-route|zzz-fake-route"


def test_endpoints_discovery_integration_get_endpoint_changes_no_routes_no_endpoints(
Expand Down Expand Up @@ -158,9 +158,9 @@ def test_endpoints_discovery_integration_get_apps(
path="/path/app-1.yml",
endpoints_to_add=[
Endpoint(
name="endpoints-discovery/cluster-1/app-1-ns-1/fake-route",
name="endpoints-discovery/cluster-1/app-1-ns-1/fake-route|zzz-fake-route",
data={
"name": "endpoints-discovery/cluster-1/app-1-ns-1/fake-route",
"name": "endpoints-discovery/cluster-1/app-1-ns-1/fake-route|zzz-fake-route",
"url": "https://fake-route.com:80",
},
)
Expand All @@ -173,9 +173,9 @@ def test_endpoints_discovery_integration_get_apps(
path="/path/app-2.yml",
endpoints_to_add=[
Endpoint(
name="endpoints-discovery/cluster-1/app-2-ns-1/fake-route",
name="endpoints-discovery/cluster-1/app-2-ns-1/fake-route|zzz-fake-route",
data={
"name": "endpoints-discovery/cluster-1/app-2-ns-1/fake-route",
"name": "endpoints-discovery/cluster-1/app-2-ns-1/fake-route|zzz-fake-route",
"url": "https://fake-route.com:80",
},
)
Expand Down