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

Improve kustomization handling for more complex repo setups #264

Merged
merged 7 commits into from
Jul 6, 2023
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
23 changes: 19 additions & 4 deletions flux_local/git_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
GIT_REPO_KIND = "GitRepository"
OCI_REPO_KIND = "OCIRepository"
DEFAULT_NAMESPACE = "flux-system"
ROOT_KUSTOMIZATION_NAME = "flux-system"


@dataclass
Expand Down Expand Up @@ -381,7 +382,8 @@ async def kustomization_traversal(path_selector: PathSelector) -> list[Kustomiza
# full prefix relative to the root.
for kustomization in docs:
if not kustomization.path:
kustomization.path = str(root / path)
_LOGGER.debug("Assigning implicit path %s", path_selector.relative_path)
kustomization.path = str(path_selector.relative_path)
if not kustomization.source_path:
continue
kustomization.source_path = str(
Expand Down Expand Up @@ -467,6 +469,9 @@ def make_clusters(
)

graph.add_node(node_name(ks), ks=ks)
if ks.name == ROOT_KUSTOMIZATION_NAME and ks.namespace == DEFAULT_NAMESPACE:
# Do not attempt parent search below
continue

# Find the parent Kustomization that produced this based on the
# matching the kustomize source parent paths with a Kustomization
Expand All @@ -491,7 +496,10 @@ def make_clusters(
ks.namespaced_name,
candidate.namespaced_name,
)
graph.add_edge(node_name(candidate), node_name(ks))
if graph.has_edge(node_name(ks), node_name(candidate)):
_LOGGER.debug("Already has opposite edge; Skipping cycle")
else:
graph.add_edge(node_name(candidate), node_name(ks))
else:
_LOGGER.debug(
"Skipping candidate source %s", candidate.namespaced_name
Expand All @@ -502,8 +510,11 @@ def make_clusters(
# Clusters are subgraphs within the graph that are connected, with the root
# node being the cluster itself. All children Kustomizations are flattended.
_LOGGER.debug("Creating clusters based on connectivity")
for node, degree in graph.in_degree():
_LOGGER.debug("Node: %s, degree: %s", node, degree)
roots = [node for node, degree in graph.in_degree() if degree == 0]
roots.sort()

clusters: list[Cluster] = []
_LOGGER.debug("roots=%s", roots)
for root in roots:
Expand Down Expand Up @@ -662,11 +673,15 @@ async def build_manifest(
_LOGGER.debug("No clusters found; Processing as a Kustomization: %s", selector)
# The argument path may be a Kustomization inside a cluster. Create a synthetic
# cluster with any found Kustomizations
cluster = Cluster(name="cluster", namespace="", path=str(selector.path.path))
cluster = Cluster(
name="cluster", namespace="", path=str(selector.path.relative_path)
)
objects = await get_kustomizations(selector.path.path)
if objects:
cluster.kustomizations = [
Kustomization(name="kustomization", path=str(selector.path.path))
Kustomization(
name="kustomization", path=str(selector.path.relative_path)
)
]
clusters.append(cluster)

Expand Down
2 changes: 1 addition & 1 deletion flux_local/tool/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def create_diff_path(
which is useful when run from CI.
"""
if path_orig := kwargs.get("path_orig"):
yield git_repo.PathSelector(path_orig)
yield git_repo.PathSelector(path_orig, sources=kwargs.get("sources"))
return

with git_repo.create_worktree(selector.repo) as worktree:
Expand Down
2 changes: 2 additions & 0 deletions flux_local/tool/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ async def run( # type: ignore[no-untyped-def]
cols = ["name", "path"]
if output == "wide":
cols.extend(["helmrepos", "releases"])
if query.kustomization.namespace is None:
cols.insert(0, "namespace")
if len(manifest.clusters) > 1:
cols.insert(0, "cluster")
for cluster in manifest.clusters:
Expand Down
3 changes: 2 additions & 1 deletion flux_local/tool/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,10 @@ def build_cluster_selector( # type: ignore[no-untyped-def]
selector.path = git_repo.PathSelector(
kwargs.get("path"), sources=kwargs.get("sources")
)
selector.cluster.namespace = kwargs.get("namespace")
selector.kustomization.namespace = kwargs.get("namespace")
if kwargs.get("all_namespaces"):
selector.cluster.namespace = None
selector.kustomization.namespace = None
return selector


Expand Down
10 changes: 10 additions & 0 deletions flux_local/tool/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ class ResourceKey:
namespace: str
name: str

def __post_init__(self) -> None:
if self.cluster_path.startswith("/"):
raise AssertionError(
f"Expected cluster_path as relative: {self.cluster_path}"
)
if self.kustomization_path.startswith("/"):
raise AssertionError(
f"Expected kustomization_path as relative: {self.kustomization_path}"
)

@property
def label(self) -> str:
parts = []
Expand Down
4 changes: 3 additions & 1 deletion tests/tool/testdata/get_cluster3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ args:
- cluster
- --path
- tests/testdata/cluster3/
- --sources
- cluster=tests/testdata/cluster3
stdout: |
NAME PATH KUSTOMIZATIONS
flux-system ./tests/testdata/cluster3/ 1
flux-system ./tests/testdata/cluster3/ 3
10 changes: 10 additions & 0 deletions tests/tool/testdata/get_ks5_all.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
args:
- get
- ks
- -A
- --path
- tests/testdata/cluster5
stdout: |
NAMESPACE NAME PATH
controllers infra-controllers tests/testdata/cluster5
flux-system flux-system ./tests/testdata/cluster5/clusters/prod
4 changes: 2 additions & 2 deletions tests/tool/testdata/get_ks_path_ks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ args:
- --path
- ./tests/testdata/cluster/apps/prod
stdout: |
NAME PATH
kustomization tests/testdata/cluster/apps/prod
NAMESPACE NAME PATH
None kustomization tests/testdata/cluster/apps/prod