Skip to content

Commit

Permalink
🐛 endpoints-discovery: avoid duplicated urls (#4706)
Browse files Browse the repository at this point in the history
Several routes may have the same hostname, e.g., if path is used. Remove duplicates and unclutter the app.yml files

Ticket: https://issues.redhat.com/browse/APPSRE-10847
  • Loading branch information
chassing authored Oct 3, 2024
1 parent d736d76 commit 3a0cbc3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
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

0 comments on commit 3a0cbc3

Please sign in to comment.