-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat(template): Check that Helm defined templates carry the chart name #647
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
+108
−0
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c91762d
feat(template): Check that Helm defined templates carry the chart name
lfrancke f73e52c
fix(template): Apply review feedback
lfrancke 26f1b40
feat(template): Run the script tests from prek
lfrancke 8fba6cb
Update template/scripts/check_namespaced_defines.py
lfrancke a16dbe8
Update template/scripts/check_namespaced_defines.py
lfrancke 1eea0bf
Update template/scripts/check_namespaced_defines.py
lfrancke e5fc4f5
Update template/scripts/check_namespaced_defines.py
lfrancke 588e75b
Update template/scripts/check_namespaced_defines.py
lfrancke 6eca093
fix(template): Finish the rename to file_contents
lfrancke 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| """ | ||
| Fails when a Helm defined template, or a call to one, is not prefixed with the | ||
| name of the chart it lives in. | ||
|
|
||
| To run tests for this script: | ||
| python3 -m unittest check_namespaced_defines.py | ||
| """ | ||
|
|
||
| import re | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| NAME_PATTERN = r'\b(?:define|include|template)\s+"([^"]+)"' | ||
| CHART_PATTERN = r"(?:^|/)deploy/helm/(?P<chart>[^/]+)/templates/" | ||
|
|
||
|
|
||
| def chart_of(path): | ||
| """The chart whose templates directory holds path, or None if it is outside one.""" | ||
| found = re.search(CHART_PATTERN, Path(path).as_posix()) | ||
| return found.group("chart") if found else None | ||
|
|
||
|
|
||
| def unprefixed(file_contents, chart_name): | ||
| """The defined template names in file_contents, and the calls to them, lacking the chart prefix.""" | ||
| names = re.findall(NAME_PATTERN, file_contents) | ||
| return sorted({name for name in names if not name.startswith(f"{chart_name}.")}) | ||
|
|
||
|
|
||
| # Run these with: python3 -m unittest check_namespaced_defines.py | ||
| class TestCoreMethods(unittest.TestCase): | ||
|
Techassi marked this conversation as resolved.
|
||
| def test_chart_of(self): | ||
| self.assertEqual( | ||
| chart_of("deploy/helm/trino-operator/templates/x.yaml"), "trino-operator" | ||
| ) | ||
| # An absolute path has to give the same answer, or a manual run invents a chart name. | ||
| self.assertEqual( | ||
| chart_of("/tmp/wt/deploy/helm/secret-operator/templates/a/b.yaml"), | ||
| "secret-operator", | ||
| ) | ||
| self.assertIsNone(chart_of("deploy/helm/trino-operator/values.yaml")) | ||
| self.assertIsNone(chart_of("README.md")) | ||
|
|
||
| def test_prefixed_is_accepted(self): | ||
| text = '{{- define "trino-operator.labels" -}}\n{{ include "trino-operator.chart" . }}\n' | ||
| self.assertEqual(unprefixed(text, "trino-operator"), []) | ||
|
|
||
| def test_unprefixed_define_is_reported(self): | ||
| self.assertEqual( | ||
| unprefixed('{{- define "helper.thing" -}}', "trino-operator"), | ||
| ["helper.thing"], | ||
| ) | ||
|
|
||
| def test_unprefixed_call_is_reported(self): | ||
| # A renamed definition whose call sites did not move with it stops the chart rendering. | ||
| text = ( | ||
| '{{ include "operator.fullname" . }}\n{{ template "operator.labels" . }}\n' | ||
| ) | ||
| self.assertEqual( | ||
| unprefixed(text, "trino-operator"), ["operator.fullname", "operator.labels"] | ||
| ) | ||
|
|
||
| def test_keyword_must_stand_alone(self): | ||
| # Without a word boundary any identifier ending in the keyword matches. | ||
| self.assertEqual( | ||
| unprefixed('{{ .Values.xinclude "bar.baz" }}', "foo-operator"), [] | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import sys | ||
|
|
||
| if not sys.argv[1:]: | ||
| print(f"usage: {sys.argv[0]} deploy/helm/<chart>/templates/*", file=sys.stderr) | ||
| sys.exit(2) | ||
|
|
||
| failed = False | ||
| for path in sys.argv[1:]: | ||
| chart_name = chart_of(path) | ||
| if chart_name is None: | ||
| print( | ||
| f"{sys.argv[0]}: {path} is not inside deploy/helm/<chart>/templates/", | ||
| file=sys.stderr, | ||
| ) | ||
| sys.exit(2) | ||
| file_contents = Path(path).read_text(encoding="utf-8") | ||
| unprefixed_names = unprefixed(file_contents, chart_name) | ||
| if unprefixed_names: | ||
| failed = True | ||
| print( | ||
| f"{path}: defined templates and the calls to them must be prefixed with '{chart_name}.'" | ||
| ) | ||
| for name in unprefixed_names: | ||
| print(f" {name}") | ||
|
|
||
| sys.exit(1 if failed else 0) | ||
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.