Skip to content

Commit ac4d5e7

Browse files
authored
Cleanup paths on output to remove unnecessary ./ prefixes (#269)
1 parent b0ae075 commit ac4d5e7

File tree

11 files changed

+99
-88
lines changed

11 files changed

+99
-88
lines changed

flux_local/git_repo.py

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,42 @@ async def get_fluxtomizations(
329329
]
330330

331331

332+
# default_path=root_path_selector.relative_path
333+
# sources=path-selector.sources or ()
334+
def adjust_ks_path(
335+
doc: Kustomization, default_path: Path, sources: list[Source]
336+
) -> Path | None:
337+
"""Make adjustments to the Kustomizations path."""
338+
# Source path is relative to the search path. Update to have the
339+
# full prefix relative to the root.
340+
if not doc.path:
341+
_LOGGER.debug("Assigning implicit path %s", default_path)
342+
return default_path
343+
344+
if doc.source_kind == OCI_REPO_KIND:
345+
for source in sources:
346+
if source.name == doc.source_name:
347+
_LOGGER.debug(
348+
"Updated Source for OCIRepository %s: %s", doc.name, doc.path
349+
)
350+
return source.root / doc.path
351+
352+
_LOGGER.info(
353+
"Unknown cluster source for OCIRepository %s: %s", doc.name, doc.path
354+
)
355+
return None
356+
357+
return Path(doc.path)
358+
359+
332360
async def kustomization_traversal(
333361
root_path_selector: PathSelector, path_selector: PathSelector, build: bool
334362
) -> list[Kustomization]:
335363
"""Search for kustomizations in the specified path."""
336364

337365
kustomizations: list[Kustomization] = []
338-
visited: set[Path] = set() # Relative paths within the cluster
339-
nodes: set[str] = set()
366+
visited_paths: set[Path] = set() # Relative paths within the cluster
367+
visited_ks: set[str] = set()
340368

341369
path_queue: queue.Queue[Path] = queue.Queue()
342370
path_queue.put(path_selector.relative_path)
@@ -346,65 +374,42 @@ async def kustomization_traversal(
346374
try:
347375
docs = await get_fluxtomizations(root_path_selector.root, path, build=build)
348376
except FluxException as err:
349-
detail = ERROR_DETAIL_BAD_KS if visited else ERROR_DETAIL_BAD_PATH
377+
detail = ERROR_DETAIL_BAD_KS if visited_paths else ERROR_DETAIL_BAD_PATH
350378
raise FluxException(
351379
f"Error building Fluxtomization in '{root_path_selector.root}' "
352380
f"path '{path}': {err} - {detail}"
353381
)
354382

355-
visited |= set({path})
383+
visited_paths |= set({path})
384+
385+
orig_len = len(docs)
386+
docs = [doc for doc in docs if doc.namespaced_name not in visited_ks]
387+
visited_ks |= set({doc.namespaced_name for doc in docs})
388+
new_len = len(docs)
389+
_LOGGER.debug("Found %s Kustomizations (%s unique)", orig_len, new_len)
356390

357-
_LOGGER.debug("Found %s Kustomizations", len(docs))
358391
result_docs = []
359392
for doc in docs:
360-
if doc.namespaced_name in nodes:
361-
_LOGGER.debug(
362-
"Ignoring duplicate Kustomization %s", doc.namespaced_name
363-
)
364-
continue
365-
nodes.add(doc.namespaced_name)
366-
# Source path is relative to the search path. Update to have the
367-
# full prefix relative to the root.
368-
if not doc.path:
369-
_LOGGER.debug(
370-
"Assigning implicit path %s", root_path_selector.relative_path
371-
)
372-
doc.path = str(root_path_selector.relative_path)
373-
374-
found_path: Path | None = None
375393
_LOGGER.debug(
376-
"Kustomization '%s' has sourceRef.kind '%s' of '%s'",
394+
"Kustomization '%s' sourceRef.kind '%s' of '%s'",
377395
doc.name,
378396
doc.source_kind,
379397
doc.source_name,
380398
)
381-
if not doc.source_kind or doc.source_kind == GIT_REPO_KIND:
382-
found_path = Path(doc.path)
383-
elif doc.source_kind == OCI_REPO_KIND:
384-
for source in path_selector.sources or ():
385-
if source.name == doc.source_name:
386-
found_path = source.root / doc.path
387-
doc.path = str(found_path)
388-
_LOGGER.debug(
389-
"Updated Source for OCIRepository %s: %s",
390-
doc.name,
391-
doc.path,
392-
)
393-
break
394-
395-
elif not doc.source_kind or doc.source_kind == GIT_REPO_KIND:
396-
found_path = Path(doc.path)
397-
398-
if not found_path:
399-
_LOGGER.debug("Skipping kustomization %s; not known source", doc.name)
399+
if not (
400+
doc_path := adjust_ks_path(
401+
doc, root_path_selector.relative_path, path_selector.sources or []
402+
)
403+
):
400404
continue
401-
402-
if found_path not in visited:
403-
path_queue.put(found_path)
405+
doc.path = str(doc_path)
406+
if doc_path not in visited_paths:
407+
path_queue.put(doc_path)
404408
else:
405-
_LOGGER.debug("Already visited %s", found_path)
409+
_LOGGER.debug("Already visited %s", doc_path)
406410
result_docs.append(doc)
407411
kustomizations.extend(result_docs)
412+
kustomizations.sort(key=lambda x: (x.namespace, x.name))
408413
return kustomizations
409414

410415

@@ -452,15 +457,21 @@ async def get_clusters(
452457
]
453458
build = False
454459

460+
tasks = []
455461
for cluster in clusters:
456462
_LOGGER.debug("Building cluster %s %s", cluster.name, cluster.path)
457-
results = await kustomization_traversal(
458-
path_selector,
459-
PathSelector(path=Path(cluster.path), sources=path_selector.sources),
460-
build=build,
463+
tasks.append(
464+
kustomization_traversal(
465+
path_selector,
466+
PathSelector(path=Path(cluster.path), sources=path_selector.sources),
467+
build=build,
468+
)
461469
)
462-
results.sort(key=lambda x: (x.namespace, x.name))
463-
cluster.kustomizations = list(filter(kustomization_selector.predicate, results))
470+
finished = await asyncio.gather(*tasks)
471+
for cluster, results in zip(clusters, finished):
472+
cluster.kustomizations = [
473+
ks for ks in results if kustomization_selector.predicate(ks)
474+
]
464475
clusters.sort(key=lambda x: (x.path, x.namespace, x.name))
465476
return clusters
466477

tests/test_git_repo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ async def write(w: Path, x: Path, y: Any, cmd: Kustomize | None) -> None:
123123
kustomization = cluster.kustomizations[0]
124124
assert kustomization.name == "apps"
125125
assert kustomization.namespace == "flux-system"
126-
assert kustomization.path == "./tests/testdata/cluster/apps/prod"
126+
assert kustomization.path == "tests/testdata/cluster/apps/prod"
127127

128128
visits.sort()
129129
assert visits == [

tests/tool/testdata/get_cluster_yaml.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ stdout: |
1414
kustomizations:
1515
- name: apps
1616
namespace: flux-system
17-
path: ./tests/testdata/cluster/apps/prod
17+
path: tests/testdata/cluster/apps/prod
1818
helm_repos: []
1919
helm_releases:
2020
- name: podinfo
@@ -26,13 +26,13 @@ stdout: |
2626
cluster_policies: []
2727
- name: flux-system
2828
namespace: flux-system
29-
path: ./tests/testdata/cluster/clusters/prod
29+
path: tests/testdata/cluster/clusters/prod
3030
helm_repos: []
3131
helm_releases: []
3232
cluster_policies: []
3333
- name: infra-configs
3434
namespace: flux-system
35-
path: ./tests/testdata/cluster/infrastructure/configs
35+
path: tests/testdata/cluster/infrastructure/configs
3636
helm_repos:
3737
- name: bitnami
3838
namespace: flux-system
@@ -52,7 +52,7 @@ stdout: |
5252
namespace: null
5353
- name: infra-controllers
5454
namespace: flux-system
55-
path: ./tests/testdata/cluster/infrastructure/controllers
55+
path: tests/testdata/cluster/infrastructure/controllers
5656
helm_repos: []
5757
helm_releases:
5858
- name: weave-gitops

tests/tool/testdata/get_ks.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ args:
44
- --path
55
- tests/testdata/cluster
66
stdout: |
7-
NAME PATH
8-
apps ./tests/testdata/cluster/apps/prod
9-
flux-system ./tests/testdata/cluster/clusters/prod
10-
infra-configs ./tests/testdata/cluster/infrastructure/configs
11-
infra-controllers ./tests/testdata/cluster/infrastructure/controllers
7+
NAME PATH
8+
apps tests/testdata/cluster/apps/prod
9+
flux-system tests/testdata/cluster/clusters/prod
10+
infra-configs tests/testdata/cluster/infrastructure/configs
11+
infra-controllers tests/testdata/cluster/infrastructure/controllers

tests/tool/testdata/get_ks2.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ args:
44
- --path
55
- tests/testdata/cluster2
66
stdout: |
7-
NAME PATH
8-
cluster ./tests/testdata/cluster2/flux
9-
cluster-apps ./tests/testdata/cluster2/apps
10-
cluster-apps-ingress-nginx ./tests/testdata/cluster2/apps/networking/ingress-nginx/app
11-
cluster-apps-ingress-nginx-certificates ./tests/testdata/cluster2/apps/networking/ingress-nginx/certificates
12-
cluster-apps-kubernetes-dashboard ./tests/testdata/cluster2/apps/monitoring/kubernetes-dashboard/app
7+
NAME PATH
8+
cluster tests/testdata/cluster2/flux
9+
cluster-apps tests/testdata/cluster2/apps
10+
cluster-apps-ingress-nginx tests/testdata/cluster2/apps/networking/ingress-nginx/app
11+
cluster-apps-ingress-nginx-certificates tests/testdata/cluster2/apps/networking/ingress-nginx/certificates
12+
cluster-apps-kubernetes-dashboard tests/testdata/cluster2/apps/monitoring/kubernetes-dashboard/app

tests/tool/testdata/get_ks4.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ args:
44
- --path
55
- tests/testdata/cluster4
66
stdout: |
7-
NAME PATH
8-
cluster ./tests/testdata/cluster4/flux
9-
cluster-apps ./tests/testdata/cluster4/apps
10-
cluster-apps-kubernetes-dashboard ./tests/testdata/cluster4/apps/monitoring/kubernetes-dashboard
7+
NAME PATH
8+
cluster tests/testdata/cluster4/flux
9+
cluster-apps tests/testdata/cluster4/apps
10+
cluster-apps-kubernetes-dashboard tests/testdata/cluster4/apps/monitoring/kubernetes-dashboard

tests/tool/testdata/get_ks5.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ args:
44
- --path
55
- tests/testdata/cluster5
66
stdout: |
7-
NAME PATH
8-
flux-system ./tests/testdata/cluster5/clusters/prod
7+
NAME PATH
8+
flux-system tests/testdata/cluster5/clusters/prod

tests/tool/testdata/get_ks5_all.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ args:
55
- --path
66
- tests/testdata/cluster5
77
stdout: |
8-
NAMESPACE NAME PATH
9-
controllers infra-controllers tests/testdata/cluster5
10-
flux-system flux-system ./tests/testdata/cluster5/clusters/prod
8+
NAMESPACE NAME PATH
9+
controllers infra-controllers tests/testdata/cluster5
10+
flux-system flux-system tests/testdata/cluster5/clusters/prod

tests/tool/testdata/get_ks6.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ args:
44
- --path
55
- tests/testdata/cluster6
66
stdout: |
7-
NAME PATH
8-
apps ./tests/testdata/cluster6/apps/
9-
flux-system ./tests/testdata/cluster6/cluster
7+
NAME PATH
8+
apps tests/testdata/cluster6/apps
9+
flux-system tests/testdata/cluster6/cluster

tests/tool/testdata/get_ks_path.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ args:
44
- --path
55
- ./tests/testdata/cluster/clusters/prod
66
stdout: |
7-
NAME PATH
8-
apps ./tests/testdata/cluster/apps/prod
9-
flux-system ./tests/testdata/cluster/clusters/prod
10-
infra-configs ./tests/testdata/cluster/infrastructure/configs
11-
infra-controllers ./tests/testdata/cluster/infrastructure/controllers
7+
NAME PATH
8+
apps tests/testdata/cluster/apps/prod
9+
flux-system tests/testdata/cluster/clusters/prod
10+
infra-configs tests/testdata/cluster/infrastructure/configs
11+
infra-controllers tests/testdata/cluster/infrastructure/controllers

tests/tool/testdata/get_ks_wide.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ args:
66
- --path
77
- tests/testdata/cluster
88
stdout: |
9-
NAME PATH HELMREPOS RELEASES
10-
apps ./tests/testdata/cluster/apps/prod 0 1
11-
flux-system ./tests/testdata/cluster/clusters/prod 0 0
12-
infra-configs ./tests/testdata/cluster/infrastructure/configs 3 0
13-
infra-controllers ./tests/testdata/cluster/infrastructure/controllers 0 2
9+
NAME PATH HELMREPOS RELEASES
10+
apps tests/testdata/cluster/apps/prod 0 1
11+
flux-system tests/testdata/cluster/clusters/prod 0 0
12+
infra-configs tests/testdata/cluster/infrastructure/configs 3 0
13+
infra-controllers tests/testdata/cluster/infrastructure/controllers 0 2

0 commit comments

Comments
 (0)