-
Notifications
You must be signed in to change notification settings - Fork 0
Dev 9359 support additional chart operations #61
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
601888a
Addd chart metadata command
facetoe 7aa0a86
Add chart list-changed command
facetoe 8fec6e3
Add lint command
facetoe 8cdc076
Refactor execute to be more flexible
facetoe c5bf22a
Support pushing to GHCR registry
facetoe 72f181d
Implement chart test command
facetoe 59495e1
Clean up test framework
facetoe 6377ec3
Add simple command for printing tools path
facetoe 5d07711
Pass registry config to build
facetoe b5a0dc1
Standardize how chart paths are resolved
facetoe 285221c
Support --since flag
facetoe e74e4bf
Only log push stdout on failure
facetoe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| from contextlib import chdir | ||
| from pathlib import Path | ||
| from subprocess import CalledProcessError | ||
|
|
||
| import click | ||
| from click import ClickException | ||
|
|
||
| from local_k8s.shared import execute, resolve_chart | ||
| from local_k8s.static import CT_YAML | ||
|
|
||
|
|
||
| @click.command("lint") | ||
| @click.option( | ||
| "--helm-dir", | ||
| type=click.Path( | ||
| exists=True, | ||
| file_okay=False, | ||
| dir_okay=True, | ||
| path_type=Path, | ||
| ), | ||
| required=True, | ||
| ) | ||
| @click.option( | ||
| "--chart", | ||
| type=click.Path( | ||
| exists=False, | ||
| file_okay=False, | ||
| dir_okay=True, | ||
| path_type=Path, | ||
| ), | ||
| required=True, | ||
| ) | ||
| def lint(helm_dir: Path, chart: Path) -> None: | ||
| helm_dir = helm_dir.resolve() | ||
| if not (helm_dir / CT_YAML).is_file(): | ||
| raise ClickException(f"{CT_YAML} is required in the root of --helm-dir") | ||
|
|
||
| resolved_chart = resolve_chart(helm_dir, chart) | ||
|
|
||
| with chdir(helm_dir): | ||
| try: | ||
| execute( | ||
| "ct", | ||
| "lint", | ||
| "--config", | ||
| str(CT_YAML), | ||
| "--charts", | ||
| str(resolved_chart.path_relative_to_helm_dir), | ||
| "--check-version-increment=true", | ||
| ) | ||
| except CalledProcessError as e: | ||
| raise ClickException(f"lint failed with rc={e.returncode}") from e |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import json | ||
| from contextlib import chdir | ||
| from pathlib import Path | ||
| from subprocess import CalledProcessError | ||
|
|
||
| import click | ||
| from click import ClickException | ||
|
|
||
| from local_k8s.shared import execute | ||
| from local_k8s.static import CT_YAML | ||
|
|
||
|
|
||
| @click.command("list-changed") | ||
| @click.option( | ||
| "--helm-dir", | ||
| type=click.Path( | ||
| exists=True, | ||
| file_okay=False, | ||
| dir_okay=True, | ||
| path_type=Path, | ||
| ), | ||
| required=True, | ||
| ) | ||
| @click.option("--target-branch", default="main", show_default=True) | ||
| @click.option("--since", default=None) | ||
| def list_changed(helm_dir: Path, target_branch: str, since: str | None) -> None: | ||
| helm_dir = helm_dir.resolve() | ||
| ct_path = helm_dir / CT_YAML | ||
| if not ct_path.is_file(): | ||
| raise ClickException(f"{CT_YAML} is required in the root of --helm-dir") | ||
|
|
||
| # Get the root of our git repo. | ||
| repo_root = Path( | ||
| execute( | ||
| "git", | ||
| "-C", | ||
| str(helm_dir), | ||
| "rev-parse", | ||
| "--show-toplevel", | ||
| skip_resolve=True, | ||
| capture_stdout=True, | ||
| ).stdout.strip() | ||
| ).resolve() | ||
|
|
||
| # ct list-changed operates a bit differently to the other CT commands. | ||
| # It must be executed from the root of the repo, and the paths to | ||
| # the chart-dir/config need to be relative not absolute. This is | ||
| # why we convert to relative below, if that is not done, it asplodes. | ||
| with chdir(repo_root): | ||
| args = [ | ||
| "ct", | ||
| "list-changed", | ||
| "--config", | ||
| str(ct_path.relative_to(repo_root)), | ||
| "--chart-dirs", | ||
| str((helm_dir / "charts").relative_to(repo_root)), | ||
| "--target-branch", | ||
| target_branch, | ||
| ] | ||
| if since is not None: | ||
| args.extend(["--since", since]) | ||
| try: | ||
| out = execute(*args, capture_stdout=True) | ||
| except CalledProcessError as e: | ||
| raise ClickException(f"list-changed failed with rc={e.returncode}") from e | ||
| charts = [line.strip() for line in out.stdout.splitlines() if line.strip()] | ||
| click.echo(json.dumps(charts)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
| import click | ||
| from click import ClickException | ||
| from pydantic import ValidationError | ||
|
|
||
| from local_k8s.models import ChartMetadata | ||
|
|
||
|
|
||
| @click.command("metadata") | ||
| @click.option( | ||
| "--chart", | ||
| type=click.Path( | ||
| exists=True, | ||
| file_okay=False, | ||
| dir_okay=True, | ||
| path_type=Path, | ||
| ), | ||
| required=True, | ||
| ) | ||
| def metadata(chart: Path) -> None: | ||
| try: | ||
| meta = ChartMetadata.from_chart_dir(chart) | ||
| except (ValueError, ValidationError) as e: | ||
| raise ClickException(str(e)) from e | ||
| click.echo(json.dumps(meta.model_dump(mode="json"))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import json | ||
| from pathlib import Path | ||
| from subprocess import CalledProcessError | ||
| from tempfile import TemporaryDirectory | ||
|
|
||
| import click | ||
| from click import ClickException | ||
| from pydantic import ValidationError | ||
|
|
||
| from local_k8s.models import ChartMetadata | ||
| from local_k8s.shared import execute | ||
|
|
||
| REGISTRY_HOST = "ghcr.io" | ||
|
|
||
|
|
||
| def _detailed_failure_for(step: str, e: CalledProcessError) -> ClickException: | ||
| stdout = e.output if isinstance(e.output, str) else "" | ||
| msg = f"{step} failed with rc={e.returncode}" | ||
| if stdout.strip(): | ||
| msg = f"{msg}\n{stdout.strip()}" | ||
| return ClickException(msg) | ||
|
|
||
|
|
||
| @click.command("push") | ||
| @click.option( | ||
| "--chart", | ||
| type=click.Path( | ||
| exists=True, | ||
| file_okay=False, | ||
| dir_okay=True, | ||
| path_type=Path, | ||
| ), | ||
| required=True, | ||
| ) | ||
| @click.option( | ||
| "--registry-config", | ||
| type=click.Path( | ||
| exists=True, | ||
| file_okay=True, | ||
| dir_okay=False, | ||
| path_type=Path, | ||
| ), | ||
| required=True, | ||
| ) | ||
| @click.option("--oci-repo", required=True, help="OCI path after host, e.g. owner/repo") | ||
| @click.option("--beta", default=None) | ||
| @click.option("--fail-if-exists", is_flag=True, default=False) | ||
| def push( | ||
| chart: Path, | ||
| registry_config: Path, | ||
| oci_repo: str, | ||
| beta: str | None, | ||
| fail_if_exists: bool, | ||
| ) -> None: | ||
| try: | ||
| meta = ChartMetadata.from_chart_dir(chart) | ||
| except (ValueError, ValidationError) as e: | ||
| raise ClickException(str(e)) from e | ||
|
|
||
| version = f"{meta.version}-beta.{beta}" if beta is not None else meta.version | ||
|
|
||
| oci_base = f"oci://{REGISTRY_HOST}/{oci_repo}" | ||
| oci_chart = f"{oci_base}/{meta.name}" | ||
| archive_name = f"{meta.name}-{version}.tgz" | ||
| registry_config_arg = str(registry_config) | ||
|
|
||
| if fail_if_exists: | ||
| result = execute( | ||
| "helm", | ||
| "show", | ||
| "chart", | ||
| oci_chart, | ||
| "--version", | ||
| version, | ||
| "--registry-config", | ||
| registry_config_arg, | ||
| check=False, | ||
| capture_stdout=True, | ||
| capture_stderr=True, | ||
| ) | ||
| if result.returncode == 0: | ||
| raise ClickException(f"{meta.name}:{version} already exists in registry") | ||
| text = f"{result.stderr}\n{result.stdout}".lower() | ||
| if "not found" not in text and "manifest unknown" not in text: | ||
| raise ClickException( | ||
| f"exist check failed (rc={result.returncode}): {result.stderr.strip()}" | ||
| ) | ||
|
|
||
| try: | ||
| execute( | ||
| "helm", | ||
| "dependency", | ||
| "build", | ||
| str(chart), | ||
| "--registry-config", | ||
| registry_config_arg, | ||
| capture_stdout=True, | ||
| ) | ||
| except CalledProcessError as e: | ||
| raise _detailed_failure_for("dependency build", e) from e | ||
|
|
||
| with TemporaryDirectory() as tmp: | ||
| package_args = [ | ||
| "helm", | ||
| "package", | ||
| str(chart), | ||
| "--destination", | ||
| tmp, | ||
| ] | ||
| if beta is not None: | ||
| package_args.extend(["--version", version]) | ||
| try: | ||
| execute(*package_args, capture_stdout=True) | ||
| execute( | ||
| "helm", | ||
| "push", | ||
| str(Path(tmp) / archive_name), | ||
| oci_base, | ||
| "--registry-config", | ||
| registry_config_arg, | ||
| capture_stdout=True, | ||
| ) | ||
| execute( | ||
| "helm", | ||
| "show", | ||
| "chart", | ||
| oci_chart, | ||
| "--version", | ||
| version, | ||
| "--registry-config", | ||
| registry_config_arg, | ||
| capture_stdout=True, | ||
| ) | ||
| except CalledProcessError as e: | ||
| raise _detailed_failure_for("push", e) from e | ||
|
|
||
| click.echo( | ||
| json.dumps( | ||
| { | ||
| "name": meta.name, | ||
| "version": version, | ||
| "oci_ref": f"{REGISTRY_HOST}/{oci_repo}/{meta.name}:{version}", | ||
| "archive": archive_name, | ||
| } | ||
| ) | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.