Skip to content

Commit

Permalink
Cleanup code for ready vs not-ready kustomizations
Browse files Browse the repository at this point in the history
  • Loading branch information
allenporter committed Apr 20, 2024
1 parent 27bac39 commit 51b0afc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
46 changes: 22 additions & 24 deletions flux_local/git_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,23 @@ async def build_kustomization(
]


def _ready_kustomizations(kustomizations: list[Kustomization], visited: set[str]) -> tuple[Kustomization, Kustomization]:
"""Split the kustomizations into those that are ready vs pending."""
ready = []
pending = []
for kustomization in kustomizations:
if not_ready := (set(kustomization.depends_on or {}) - visited):
_LOGGER.debug(
"Waiting for %s before building %s",
not_ready,
kustomization.namespaced_name,
)
pending.append(kustomization)
else:
ready.append(kustomization)
return (ready, pending)


async def build_manifest(
path: Path | None = None,
selector: ResourceSelector = ResourceSelector(),
Expand Down Expand Up @@ -670,23 +687,9 @@ async def update_kustomization(cluster: Cluster) -> None:
# cluster_config = values.ks_cluster_config(cluster.kustomizations)
while queue:
build_tasks = []
in_flight = []
pending = []
while queue:
kustomization = queue.pop(0)
if not_ready := (set(kustomization.depends_on or {}) - visited):
_LOGGER.debug(
"Waiting for %s before building %s",
not_ready,
kustomization.namespaced_name,
)
pending.append(kustomization)
continue
_LOGGER.debug(
"Processing kustomization '%s': %s",
kustomization.name,
kustomization.path,
)
(ready, pending) = _ready_kustomizations(queue, visited)
for kustomization in ready:
_LOGGER.debug("Processing kustomization '%s': %s", kustomization.name, kustomization.path)

if kustomization.postbuild_substitute_from:
values.expand_postbuild_substitute_reference(
Expand All @@ -697,7 +700,6 @@ async def update_kustomization(cluster: Cluster) -> None:
# missing the postbuild substitutions.
builder.remove(kustomization)

in_flight.append(kustomization.namespaced_name)
build_tasks.append(
build_kustomization(
kustomization,
Expand All @@ -712,10 +714,8 @@ async def update_kustomization(cluster: Cluster) -> None:
"Internal error: Unexpected loop without build tasks"
)
await asyncio.gather(*build_tasks)
visited.update(in_flight)

for kustomization in pending:
queue.append(kustomization)
visited.update([ks.namespaced_name for ks in ready])
queue = pending

# Validate all Kustomizations have valid dependsOn attributes since later
# we'll be using them to order processing.
Expand Down Expand Up @@ -749,8 +749,6 @@ async def update_kustomization(cluster: Cluster) -> None:
values.expand_value_references(helm_release, kustomization)
for helm_release in kustomization.helm_releases
]
if kustomization.name == "apps":
_LOGGER.warning("apps = %s", kustomization.helm_releases)

# Visit Helm resources
for cluster in clusters:
Expand Down
1 change: 0 additions & 1 deletion tests/__snapshots__/test_git_repo.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,6 @@
"Cluster 'tests/testdata/cluster'": dict({
"Build 'flux-system/apps'": dict({
'cmds': list([
'flux build tests/testdata/cluster/apps/prod (abs)',
"kustomize cfg grep 'kind=^(CustomResourceDefinition|Secret)$' --invert-match",
"kustomize cfg grep 'kind=^(HelmRepository|HelmRelease|ConfigMap|Secret|ClusterPolicy)$'",
]),
Expand Down
2 changes: 1 addition & 1 deletion tests/tool/__snapshots__/test_build.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@
values:
ingress:
additionalLabels:
cluster_label: example-value
cluster_label: null
className: nginx
enabled: true
hosts:
Expand Down

0 comments on commit 51b0afc

Please sign in to comment.