Skip to content
This repository was archived by the owner on Dec 10, 2024. It is now read-only.

Commit 5734a10

Browse files
authored
Bugfix for charts with dependencies and make pre-commit hook function on pre-commit.ci (#46)
* Let the pre-commit hook install The Kubernetes package manager Common actions for Helm: - helm search: search for charts - helm pull: download a chart to your local directory to view - helm install: upload the chart to Kubernetes - helm list: list releases of charts Environment variables: | Name | Description | |------------------------------------|-----------------------------------------------------------------------------------| | $HELM_CACHE_HOME | set an alternative location for storing cached files. | | $HELM_CONFIG_HOME | set an alternative location for storing Helm configuration. | | $HELM_DATA_HOME | set an alternative location for storing Helm data. | | $HELM_DEBUG | indicate whether or not Helm is running in Debug mode | | $HELM_DRIVER | set the backend storage driver. Values are: configmap, secret, memory, sql. | | $HELM_DRIVER_SQL_CONNECTION_STRING | set the connection string the SQL storage driver should use. | | $HELM_MAX_HISTORY | set the maximum number of helm release history. | | $HELM_NAMESPACE | set the namespace used for the helm operations. | | $HELM_NO_PLUGINS | disable plugins. Set HELM_NO_PLUGINS=1 to disable plugins. | | $HELM_PLUGINS | set the path to the plugins directory | | $HELM_REGISTRY_CONFIG | set the path to the registry config file. | | $HELM_REPOSITORY_CACHE | set the path to the repository cache directory | | $HELM_REPOSITORY_CONFIG | set the path to the repositories file. | | $KUBECONFIG | set an alternative Kubernetes configuration file (default "~/.kube/config") | | $HELM_KUBEAPISERVER | set the Kubernetes API Server Endpoint for authentication | | $HELM_KUBECAFILE | set the Kubernetes certificate authority file. | | $HELM_KUBEASGROUPS | set the Groups to use for impersonation using a comma-separated list. | | $HELM_KUBEASUSER | set the Username to impersonate for the operation. | | $HELM_KUBECONTEXT | set the name of the kubeconfig context. | | $HELM_KUBETOKEN | set the Bearer KubeToken used for authentication. | Helm stores cache, configuration, and data based on the following configuration order: - If a HELM_*_HOME environment variable is set, it will be used - Otherwise, on systems supporting the XDG base directory specification, the XDG variables will be used - When no other location is set a default location will be used based on the operating system By default, the default directories depend on the Operating System. The defaults are listed below: | Operating System | Cache Path | Configuration Path | Data Path | |------------------|---------------------------|--------------------------------|-------------------------| | Linux | $HOME/.cache/helm | $HOME/.config/helm | $HOME/.local/share/helm | | macOS | $HOME/Library/Caches/helm | $HOME/Library/Preferences/helm | $HOME/Library/helm | | Windows | %TEMP%\helm | %APPDATA%\helm | %APPDATA%\helm | Usage: helm [command] Available Commands: completion generate autocompletion scripts for the specified shell create create a new chart with the given name dependency manage a chart's dependencies diff Preview helm upgrade changes as a diff env helm client environment information get download extended information of a named release help Help about any command history fetch release history install install a chart lint examine a chart for possible issues list list releases package package a chart directory into a chart archive plugin install, list, or uninstall Helm plugins pull download a chart from a repository and (optionally) unpack it in local directory push push a chart to remote registry login to or logout from a registry repo add, list, remove, update, and index chart repositories rollback roll back a release to a previous revision schema-gen generate json schema for values yaml search search for a keyword in charts show show information of a chart status display the status of the named release template locally render templates test run tests for a release uninstall uninstall a release upgrade upgrade a release verify verify that a chart at the given path has been signed and is valid version print the client version information Flags: --debug enable verbose output -h, --help help for helm --kube-apiserver string the address and the port for the Kubernetes API server --kube-as-group stringArray group to impersonate for the operation, this flag can be repeated to specify multiple groups. --kube-as-user string username to impersonate for the operation --kube-ca-file string the certificate authority file for the Kubernetes API server connection --kube-context string name of the kubeconfig context to use --kube-token string bearer token used for authentication --kubeconfig string path to the kubeconfig file -n, --namespace string namespace scope for this request --registry-config string path to the registry config file (default "/home/erik/.config/helm/registry/config.json") --repository-cache string path to the file containing cached repository indexes (default "/home/erik/.cache/helm/repository") --repository-config string path to the file containing repository names and URLs (default "/home/erik/.config/helm/repositories.yaml") Use "helm [command] --help" for more information about a command. via conda * Use subprocess.check_call instead of popen * fix: update/get chart deps before accessing them * Remove not needed helm repo update call * Run update_chart_dependencies once per chart * Strip irrelevant logic to update_chart_dependencies function * Update docstrings and refactor setting of root for readability * Fix inconsistent indentation in YAML file
1 parent 4153bf5 commit 5734a10

File tree

3 files changed

+44
-34
lines changed

3 files changed

+44
-34
lines changed

.pre-commit-hooks.yaml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
- id: frigate
2-
name: frigate
3-
entry: frigate hook
4-
description: Run frigate to generate documentation for your Helm charts.
5-
language: python
6-
pass_filenames: false
7-
always_run: true
1+
# This is a pre-commit hook definition, for more details see:
2+
# https://pre-commit.com/#new-hooks
3+
#
4+
- id: frigate
5+
name: frigate
6+
entry: frigate hook
7+
description: Run frigate to generate documentation for your Helm charts.
8+
# language=conda relies on the environment.yml file in this repo
9+
language: conda
10+
pass_filenames: false
11+
always_run: true

environment.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# These are dependencies for the pre-commit hook defined in
2+
# .pre-commit-hooks.yaml.
3+
#
4+
# This environment can provide `helm` via the kubernetes-helm package, but must
5+
# also explicitly install frigate itself within this repo.
6+
#
7+
name: pre-commit-hook-environment
8+
dependencies:
9+
- python
10+
- kubernetes-helm
11+
- pip
12+
- pip:
13+
- "."

frigate/gen.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def load_chart(chartdir, root=None):
2222
2323
Args:
2424
chartdir (str): Path to the Helm chart.
25-
root (str, optional): The root of the namespace we are currently at. Used for recursion.
25+
root (list, optional): The root of the namespace we are currently at. Used for recursion.
2626
2727
Returns:
2828
chart (dict): Contents of `Chart.yaml` loaded into a dict.
@@ -37,27 +37,27 @@ def load_chart(chartdir, root=None):
3737

3838

3939
def load_chart_with_dependencies(chartdir, root=None):
40-
"""Load the yaml information from a Helm chart directory and its dependencies.
41-
42-
Load in the `Chart.yaml` and `values.yaml` files from a Helm
43-
chart.
44-
45-
Inspect the `Chart.yaml` and if there are dependencies unpack their
46-
`values.yaml` and `Chart.yaml` and merge the values. Recur for all
47-
dependencies.
40+
"""
41+
Load and return dictionaries representing Chart.yaml and values.yaml from
42+
the Helm chart. If Chart.yaml declares dependencies, recursively merge in
43+
their values as well.
4844
4945
Args:
5046
chartdir (str): Path to the Helm chart.
51-
root (str, optional): The root of the namespace we are currently at. Used for recursion.
47+
root (list, optional): The root of the namespace we are currently at. Used for recursion.
5248
5349
Returns:
5450
chart (dict): Contents of `Chart.yaml` loaded into a dict.
5551
values (dict): Contents of `values.yaml` loaded into a dict.
56-
5752
"""
58-
root = [] if root is None else root
53+
if root is None:
54+
root = []
5955
chart, values = load_chart(chartdir, root=root)
6056
if "dependencies" in chart:
57+
# update the helm chart's charts/ folder
58+
update_chart_dependencies(chartdir)
59+
60+
# recursively update values by unpacking the helm charts in the charts/ folder
6161
for dependency in chart["dependencies"]:
6262
dependency_name = dependency["name"]
6363
dependency_path = os.path.join(
@@ -67,7 +67,6 @@ def load_chart_with_dependencies(chartdir, root=None):
6767
shutil.unpack_archive(dependency_path, tmpdirname)
6868
dependency_dir = os.path.join(tmpdirname, dependency_name)
6969

70-
update_chart_dependencies(tmpdirname, dependency_name)
7170
_, dependency_values = load_chart_with_dependencies(
7271
dependency_dir, root + [dependency_name]
7372
)
@@ -95,16 +94,16 @@ def squash_duplicate_values(values):
9594
return [(key, tmp[key][0], tmp[key][1]) for key in tmp]
9695

9796

98-
def update_chart_dependencies(path, chart_name):
97+
def update_chart_dependencies(chart_path):
9998
"""Update a helm charts local cache of dependencies.
10099
101100
In order to generate a values table including dependencies we need
102101
all dependencies to be checked out locally. For each chart we are generating
103102
values for we will call ``helm dep update <chart>``.
104103
105104
Args:
106-
path (string): Path to the directory containing the helm chart.
107-
chart_name (string): The name of the chart to update dependencies for.
105+
chart_path (string): Path to the directory containing the helm chart
106+
with dependencies to update to its charts/ folder.
108107
109108
"""
110109
if shutil.which("helm") is None:
@@ -114,18 +113,12 @@ def update_chart_dependencies(path, chart_name):
114113
"Alternatively run frigate again with the `--no-deps` flag to skip generating "
115114
"value table entried for dependencies."
116115
)
117-
subprocess.Popen(
118-
["helm", "repo", "update"],
119-
cwd=path,
116+
subprocess.check_call(
117+
["helm", "dep", "update", "."],
118+
cwd=chart_path,
120119
stdout=subprocess.PIPE,
121120
stderr=subprocess.PIPE,
122-
).communicate()
123-
subprocess.Popen(
124-
["helm", "dep", "update", chart_name],
125-
cwd=path,
126-
stdout=subprocess.PIPE,
127-
stderr=subprocess.PIPE,
128-
).communicate()
121+
)
129122
return None
130123

131124

@@ -206,7 +199,7 @@ def traverse(tree, root=None):
206199
207200
Args:
208201
comment (ruamel.yaml.comments.CommentedMap): Tree of config to traverse.
209-
root (str, optional): The root of the namespace we are currently at. Used for recursion.
202+
root (list, optional): The root of the namespace we are currently at. Used for recursion.
210203
211204
Yields:
212205
list(param, comment, value): Each namespaced parameter (str), the comment (str) and value (obj).

0 commit comments

Comments
 (0)