Skip to content

Commit

Permalink
Update cluster output with images
Browse files Browse the repository at this point in the history
  • Loading branch information
allenporter committed Dec 21, 2023
1 parent 8983ff9 commit f5ba8f7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
3 changes: 1 addition & 2 deletions flux_local/git_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,9 @@ async def build_kustomization(
docs = await cmd.grep(regexp).objects(
target_namespace=kustomization.target_namespace
)

if selector.doc_visitor:
doc_kinds = set(selector.doc_visitor.kinds)
_LOGGER.debug(doc_kinds)
for doc in docs:
if doc.get("kind") not in doc_kinds:
continue
Expand Down
11 changes: 10 additions & 1 deletion flux_local/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
from typing import Any

from . import git_repo
from . import git_repo, manifest

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -57,9 +57,18 @@ def add_image(name: str, doc: dict[str, Any]) -> None:
Updates the image set with the images found in the document.
"""
images = _extract_images(doc)
if not images:
return
if name in self.images:
self.images[name].update(images)
else:
self.images[name] = set(images)

return git_repo.DocumentVisitor(kinds=KINDS, func=add_image)

def update_manifest(self, manifest: manifest.Manifest) -> None:
"""Update the manifest with the images found in the repo."""
for cluster in manifest.clusters:
for kustomization in cluster.kustomizations:
if images := self.images.get(kustomization.namespaced_name):
kustomization.images = list(images)
14 changes: 12 additions & 2 deletions flux_local/tool/get.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Flux-local get action."""

import logging
from argparse import ArgumentParser, _SubParsersAction as SubParsersAction
from argparse import ArgumentParser, BooleanOptionalAction, _SubParsersAction as SubParsersAction
from typing import cast, Any
import sys

from flux_local import git_repo
from flux_local import git_repo, image

from .format import PrintFormatter, YamlFormatter
from . import selector
Expand Down Expand Up @@ -152,6 +152,9 @@ def register(
selector.add_cluster_selector_flags(args)
args.add_argument(
"--enable-images",
type=str,
default=False,
action=BooleanOptionalAction,
help="Output container images when traversing the cluster",
)
args.add_argument(
Expand All @@ -173,17 +176,24 @@ async def run( # type: ignore[no-untyped-def]
"""Async Action implementation."""
query = selector.build_cluster_selector(**kwargs)
query.helm_release.enabled = output == "yaml"

image_visitor: image.ImageVisitor | None = None
if enable_images:
if output != "yaml":
print(
"Flag --enable-images only works with --output yaml",
file=sys.stderr,
)
return
image_visitor = image.ImageVisitor()
query.doc_visitor = image_visitor.repo_visitor()

manifest = await git_repo.build_manifest(
selector=query, options=selector.options(**kwargs)
)
if output == "yaml":
if image_visitor:
image_visitor.update_manifest(manifest)
YamlFormatter().print([manifest.compact_dict()])
return

Expand Down
25 changes: 4 additions & 21 deletions tests/tool/__snapshots__/test_get_cluster.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -62,39 +62,22 @@

'''
# ---
# name: test_get_cluster[yaml-cluster7-images]
# name: test_get_cluster[yaml-cluster8-images]
'''
---
clusters:
- path: tests/testdata/cluster7
- path: tests/testdata/cluster8
kustomizations:
- name: apps
namespace: flux-system
path: tests/testdata/cluster7/flux/apps
path: tests/testdata/cluster8/apps
helm_repos: []
helm_releases:
- name: postgresql
namespace: database
chart:
name: postgresql
repo_name: bitnami-charts
repo_namespace: flux-system
cluster_policies: []
images: []
- name: charts
namespace: flux-system
path: tests/testdata/cluster7/flux/charts
helm_repos:
- name: bitnami-charts
namespace: flux-system
url: oci://registry-1.docker.io/bitnamicharts
repo_type: oci
helm_releases: []
cluster_policies: []
images: []
- name: flux-system
namespace: flux-system
path: tests/testdata/cluster7/clusters/home
path: tests/testdata/cluster8/cluster
helm_repos: []
helm_releases: []
cluster_policies: []
Expand Down
4 changes: 2 additions & 2 deletions tests/tool/test_get_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
(["--path", "tests/testdata/cluster7"]),
(["--all-namespaces", "--path", "tests/testdata/cluster/"]),
(["--path", "tests/testdata/cluster", "-o", "yaml"]),
(["--path", "tests/testdata/cluster7", "-o", "yaml"]),
(["--path", "tests/testdata/cluster8", "-o", "yaml"]),
],
ids=[
"cluster",
Expand All @@ -40,7 +40,7 @@
"cluster7",
"all-namespaces",
"yaml",
"yaml-cluster7-images"
"yaml-cluster8-images"
],
)
async def test_get_cluster(args: list[str], snapshot: SnapshotAssertion) -> None:
Expand Down

0 comments on commit f5ba8f7

Please sign in to comment.