Skip to content

Commit

Permalink
Allow adding arbitrary files to helm chart
Browse files Browse the repository at this point in the history
## What
This is a continuation of abrisco#102 with a bit nicer API

This allows the user to add arbitrary source of generated files to the helm chart at their desired location.
  • Loading branch information
purkhusid committed Dec 16, 2024
1 parent 7d1e59e commit 8a206eb
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 37 deletions.
69 changes: 36 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,37 +165,6 @@ A rule for performing `helm lint` on a helm package
| <a id="helm_lint_test-chart"></a>chart | The helm package to run linting on. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |


<a id="helm_package"></a>

## helm_package

<pre>
load("@rules_helm//helm:defs.bzl", "helm_package")

helm_package(<a href="#helm_package-name">name</a>, <a href="#helm_package-deps">deps</a>, <a href="#helm_package-chart">chart</a>, <a href="#helm_package-chart_json">chart_json</a>, <a href="#helm_package-crds">crds</a>, <a href="#helm_package-images">images</a>, <a href="#helm_package-stamp">stamp</a>, <a href="#helm_package-substitutions">substitutions</a>, <a href="#helm_package-templates">templates</a>, <a href="#helm_package-values">values</a>,
<a href="#helm_package-values_json">values_json</a>)
</pre>

Rules for creating Helm chart packages.

**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="helm_package-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="helm_package-deps"></a>deps | Other helm packages this package depends on. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="helm_package-chart"></a>chart | The `Chart.yaml` file of the helm chart | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="helm_package-chart_json"></a>chart_json | The `Chart.yaml` file of the helm chart as a json object | String | optional | `""` |
| <a id="helm_package-crds"></a>crds | All crds associated with the current helm chart. E.g., the `./crds` directory | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="helm_package-images"></a>images | A list of [oci_push](https://github.com/bazel-contrib/rules_oci/blob/main/docs/push.md#oci_push_rule-remote_tags) targets. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="helm_package-stamp"></a>stamp | Whether to encode build information into the helm actions. Possible values:<br><br>- `stamp = 1`: Always stamp the build information into the helm actions, even in [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds. This setting should be avoided, since it potentially kills remote caching for the target and any downstream actions that depend on it.<br><br>- `stamp = 0`: Always replace build information by constant values. This gives good build result caching.<br><br>- `stamp = -1`: Embedding of build information is controlled by the [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.<br><br>Stamped targets are not rebuilt unless their dependencies change. | Integer | optional | `-1` |
| <a id="helm_package-substitutions"></a>substitutions | A dictionary of substitutions to apply to the `values.yaml` file. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
| <a id="helm_package-templates"></a>templates | All templates associated with the current helm chart. E.g., the `./templates` directory | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="helm_package-values"></a>values | The `values.yaml` file for the current package. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="helm_package-values_json"></a>values_json | The `values.yaml` file for the current package as a json object. | String | optional | `""` |


<a id="helm_plugin"></a>

## helm_plugin
Expand Down Expand Up @@ -451,8 +420,8 @@ str: A json encoded string which represents `Chart.yaml` contents.
<pre>
load("@rules_helm//helm:defs.bzl", "helm_chart")

helm_chart(<a href="#helm_chart-name">name</a>, <a href="#helm_chart-chart">chart</a>, <a href="#helm_chart-chart_json">chart_json</a>, <a href="#helm_chart-crds">crds</a>, <a href="#helm_chart-values">values</a>, <a href="#helm_chart-values_json">values_json</a>, <a href="#helm_chart-substitutions">substitutions</a>, <a href="#helm_chart-templates">templates</a>, <a href="#helm_chart-images">images</a>,
<a href="#helm_chart-deps">deps</a>, <a href="#helm_chart-install_name">install_name</a>, <a href="#helm_chart-registry_url">registry_url</a>, <a href="#helm_chart-login_url">login_url</a>, <a href="#helm_chart-helm_opts">helm_opts</a>, <a href="#helm_chart-install_opts">install_opts</a>, <a href="#helm_chart-upgrade_opts">upgrade_opts</a>,
helm_chart(<a href="#helm_chart-name">name</a>, <a href="#helm_chart-chart">chart</a>, <a href="#helm_chart-chart_json">chart_json</a>, <a href="#helm_chart-crds">crds</a>, <a href="#helm_chart-values">values</a>, <a href="#helm_chart-values_json">values_json</a>, <a href="#helm_chart-substitutions">substitutions</a>, <a href="#helm_chart-templates">templates</a>, <a href="#helm_chart-files">files</a>,
<a href="#helm_chart-images">images</a>, <a href="#helm_chart-deps">deps</a>, <a href="#helm_chart-install_name">install_name</a>, <a href="#helm_chart-registry_url">registry_url</a>, <a href="#helm_chart-login_url">login_url</a>, <a href="#helm_chart-helm_opts">helm_opts</a>, <a href="#helm_chart-install_opts">install_opts</a>, <a href="#helm_chart-upgrade_opts">upgrade_opts</a>,
<a href="#helm_chart-uninstall_opts">uninstall_opts</a>, <a href="#helm_chart-data">data</a>, <a href="#helm_chart-stamp">stamp</a>, <a href="#helm_chart-kwargs">**kwargs</a>)
</pre>

Expand Down Expand Up @@ -482,6 +451,7 @@ Rules for producing a helm package and some convenience targets.
| <a id="helm_chart-values_json"></a>values_json | The json encoded contents of `values.yaml`. | `None` |
| <a id="helm_chart-substitutions"></a>substitutions | A dictionary of substitutions to apply to `values.yaml`. | `{}` |
| <a id="helm_chart-templates"></a>templates | A list of template files to include in the package. | `None` |
| <a id="helm_chart-files"></a>files | A a map with lists of files to be copied to their respective folders within the helm chart. | `{}` |
| <a id="helm_chart-images"></a>images | A list of [oci_push](https://github.com/bazel-contrib/rules_oci/blob/main/docs/push.md#oci_push_rule-remote_tags) targets | `[]` |
| <a id="helm_chart-deps"></a>deps | A list of helm package dependencies. | `None` |
| <a id="helm_chart-install_name"></a>install_name | The `helm install` name to use. `name` will be used if unset. | `None` |
Expand All @@ -496,6 +466,39 @@ Rules for producing a helm package and some convenience targets.
| <a id="helm_chart-kwargs"></a>kwargs | Additional keyword arguments for `helm_package`. | none |


<a id="helm_package"></a>

## helm_package

<pre>
load("@rules_helm//helm:defs.bzl", "helm_package")

helm_package(<a href="#helm_package-name">name</a>, <a href="#helm_package-chart">chart</a>, <a href="#helm_package-chart_json">chart_json</a>, <a href="#helm_package-crds">crds</a>, <a href="#helm_package-values">values</a>, <a href="#helm_package-values_json">values_json</a>, <a href="#helm_package-substitutions">substitutions</a>, <a href="#helm_package-templates">templates</a>, <a href="#helm_package-files">files</a>,
<a href="#helm_package-images">images</a>, <a href="#helm_package-deps">deps</a>, <a href="#helm_package-stamp">stamp</a>, <a href="#helm_package-kwargs">**kwargs</a>)
</pre>

Rules for creating Helm chart packages.

**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="helm_package-name"></a>name | The name of the target | none |
| <a id="helm_package-chart"></a>chart | "The `Chart.yaml` file of the helm chart" | `None` |
| <a id="helm_package-chart_json"></a>chart_json | "The `Chart.yaml` file of the helm chart as a json object" | `None` |
| <a id="helm_package-crds"></a>crds | All crds associated with the current helm chart. E.g., the `./crds` directory | `None` |
| <a id="helm_package-values"></a>values | The `values.yaml` file for the current package. | `None` |
| <a id="helm_package-values_json"></a>values_json | The `values.yaml` file for the current package as a json object. | `None` |
| <a id="helm_package-substitutions"></a>substitutions | A dictionary of substitutions to apply to the `values.yaml` file. | `{}` |
| <a id="helm_package-templates"></a>templates | All templates associated with the current helm chart. E.g., the `./templates` directory | `None` |
| <a id="helm_package-files"></a>files | Additional files to be added to the chart specified as a map from string to list of labels. | `{}` |
| <a id="helm_package-images"></a>images | A list of [oci_push](https://github.com/bazel-contrib/rules_oci/blob/main/docs/push.md#oci_push_rule-remote_tags) targets | `[]` |
| <a id="helm_package-deps"></a>deps | Other helm packages this package depends on. | `None` |
| <a id="helm_package-stamp"></a>stamp | Whether to encode build information into the helm actions. Possible values:<br><br>- `stamp = 1`: Always stamp the build information into the helm actions, even in [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds. This setting should be avoided, since it potentially kills remote caching for the target and any downstream actions that depend on it.<br><br>- `stamp = 0`: Always replace build information by constant values. This gives good build result caching.<br><br>- `stamp = -1`: Embedding of build information is controlled by the [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.<br><br>Stamped targets are not rebuilt unless their dependencies change. | `None` |
| <a id="helm_package-kwargs"></a>kwargs | Additional keyword arguments. | none |


<a id="helm_register_toolchains"></a>

## helm_register_toolchains
Expand Down
3 changes: 3 additions & 0 deletions helm/private/helm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def helm_chart(
values_json = None,
substitutions = {},
templates = None,
files = {},
images = [],
deps = None,
install_name = None,
Expand Down Expand Up @@ -46,6 +47,7 @@ def helm_chart(
values_json (str, optional): The json encoded contents of `values.yaml`.
substitutions (dict, optional): A dictionary of substitutions to apply to `values.yaml`.
templates (list, optional): A list of template files to include in the package.
files (map[string, list[label]], optional): A a map with lists of files to be copied to their respective folders within the helm chart.
images (list, optional): A list of [oci_push](https://github.com/bazel-contrib/rules_oci/blob/main/docs/push.md#oci_push_rule-remote_tags) targets
deps (list, optional): A list of helm package dependencies.
install_name (str, optional): The `helm install` name to use. `name` will be used if unset.
Expand Down Expand Up @@ -74,6 +76,7 @@ def helm_chart(
deps = deps,
images = images,
templates = templates,
files = files,
values = values,
values_json = values_json,
substitutions = substitutions,
Expand Down
83 changes: 81 additions & 2 deletions helm/private/helm_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def _helm_package_impl(ctx):
)
args.add("-templates_manifest", templates_manifest)

for ln in ctx.attr.files:
fmt = "%s=" + ctx.attr.files[ln]
args.add_all(ln.files, before_each = "-add_files", format_each = fmt, uniquify = True)

crds_manifest = ctx.actions.declare_file("{}/crds_manifest.json".format(ctx.label.name))
ctx.actions.write(
output = crds_manifest,
Expand Down Expand Up @@ -180,7 +184,7 @@ def _helm_package_impl(ctx):
executable = ctx.executable._packager,
outputs = [output, metadata_output],
inputs = depset(
ctx.files.templates + ctx.files.crds + stamps + image_inputs + deps + [
ctx.files.templates + ctx.files.files + ctx.files.crds + stamps + image_inputs + deps + [
chart_yaml,
values_yaml,
templates_manifest,
Expand Down Expand Up @@ -208,7 +212,77 @@ def _helm_package_impl(ctx):
),
]

helm_package = rule(
def helm_package(
name,
chart = None,
chart_json = None,
crds = None,
values = None,
values_json = None,
substitutions = {},
templates = None,
files = {},
images = [],
deps = None,
stamp = None,
**kwargs):
"""Rules for creating Helm chart packages.
Args:
name: The name of the target
chart: "The `Chart.yaml` file of the helm chart"
chart_json: "The `Chart.yaml` file of the helm chart as a json object"
crds: All crds associated with the current helm chart. E.g., the `./crds` directory
values (str, optional): The `values.yaml` file for the current package.
values_json: The `values.yaml` file for the current package as a json object.
substitutions: A dictionary of substitutions to apply to the `values.yaml` file.
templates: All templates associated with the current helm chart. E.g., the `./templates` directory
files: Additional files to be added to the chart specified as a map from string to list of labels.
images: A list of [oci_push](https://github.com/bazel-contrib/rules_oci/blob/main/docs/push.md#oci_push_rule-remote_tags) targets
deps: Other helm packages this package depends on.
stamp: Whether to encode build information into the helm actions. Possible values:
- `stamp = 1`: Always stamp the build information into the helm actions, even in \
[--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds. \
This setting should be avoided, since it potentially kills remote caching for the target and \
any downstream actions that depend on it.
- `stamp = 0`: Always replace build information by constant values. This gives good build result caching.
- `stamp = -1`: Embedding of build information is controlled by the \
[--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.
Stamped targets are not rebuilt unless their dependencies change.
**kwargs (dict): Additional keyword arguments.
"""

# We wrap this in a macro so that we can provide a better API for the `files` attribute
# If https://github.com/bazelbuild/bazel/issues/7989 ever gets implemented we can remove the macro
filegroups = {}
for folder, folder_files in files.items():
native.filegroup(
name = "{}_filegroup".format(folder),
srcs = folder_files,
)
filegroups[":{}_filegroup".format(folder)] = folder

_helm_package(
name = name,
chart = chart,
chart_json = chart_json,
crds = crds,
deps = deps,
images = images,
templates = templates,
files = filegroups,
values = values,
values_json = values_json,
substitutions = substitutions,
stamp = stamp,
**kwargs
)

_helm_package = rule(
implementation = _helm_package_impl,
doc = "Rules for creating Helm chart packages.",
attrs = {
Expand All @@ -228,6 +302,11 @@ helm_package = rule(
doc = "Other helm packages this package depends on.",
providers = [HelmPackageInfo],
),
"files": attr.label_keyed_string_dict(
doc = "Additional files to be added to the chart specified as a map from string to list of labels.",
allow_empty = True,
allow_files = True,
),
"images": attr.label_list(
doc = """\
A list of \
Expand Down
Loading

0 comments on commit 8a206eb

Please sign in to comment.