Skip to content

Commit

Permalink
Added support for helm plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
abrisco committed Oct 30, 2024
1 parent ea63d24 commit cca8527
Show file tree
Hide file tree
Showing 13 changed files with 409 additions and 42 deletions.
38 changes: 37 additions & 1 deletion MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,27 @@ Rules for creating Helm chart packages.
| <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

<pre>
helm_plugin(<a href="#helm_plugin-name">name</a>, <a href="#helm_plugin-data">data</a>, <a href="#helm_plugin-plugin_name">plugin_name</a>, <a href="#helm_plugin-yaml">yaml</a>)
</pre>

Define a [helm plugin](https://helm.sh/docs/topics/plugins/).

**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="helm_plugin-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="helm_plugin-data"></a>data | Additional files associated with the plugin. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="helm_plugin-plugin_name"></a>plugin_name | An explicit name for the plugin. If unset, `name` will be used. | String | optional | `""` |
| <a id="helm_plugin-yaml"></a>yaml | The yaml file representing the plugin | <a href="https://bazel.build/concepts/labels">Label</a> | required | |


<a id="helm_push"></a>

## helm_push
Expand Down Expand Up @@ -252,10 +273,10 @@ Produce an executable for performing a helm push to a registry.
## helm_toolchain

<pre>
helm_toolchain(<a href="#helm_toolchain-name">name</a>, <a href="#helm_toolchain-helm">helm</a>)
helm_toolchain(<a href="#helm_toolchain-name">name</a>, <a href="#helm_toolchain-helm">helm</a>, <a href="#helm_toolchain-plugins">plugins</a>)
</pre>

A helm toolchain
A helm toolchain.

**ATTRIBUTES**

Expand All @@ -264,6 +285,7 @@ A helm toolchain
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="helm_toolchain-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="helm_toolchain-helm"></a>helm | A helm binary | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="helm_toolchain-plugins"></a>plugins | Additional plugins to make available to helm. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |


<a id="helm_uninstall"></a>
Expand Down
10 changes: 6 additions & 4 deletions helm/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ load(
_helm_push = "helm_push",
_helm_push_images = "helm_push_images",
)
load(
"//helm/private:helm_toolchain.bzl",
_helm_plugin = "helm_plugin",
_helm_toolchain = "helm_toolchain",
)
load(
":providers.bzl",
_HelmPackageInfo = "HelmPackageInfo",
Expand All @@ -114,10 +119,6 @@ load(
_helm_register_toolchains = "helm_register_toolchains",
_rules_helm_dependencies = "rules_helm_dependencies",
)
load(
":toolchain.bzl",
_helm_toolchain = "helm_toolchain",
)

helm_chart = _helm_chart
helm_import = _helm_import
Expand All @@ -126,6 +127,7 @@ helm_install = _helm_install
helm_lint_aspect = _helm_lint_aspect
helm_lint_test = _helm_lint_test
helm_package = _helm_package
helm_plugin = _helm_plugin
helm_push = _helm_push
helm_push_images = _helm_push_images
helm_push_registry = _helm_push
Expand Down
2 changes: 1 addition & 1 deletion helm/private/current_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def _current_helm_toolchain_impl(ctx):
toolchain_info = ctx.toolchains["@rules_helm//helm:toolchain_type"]
return [
toolchain_info,
toolchain_info.default,
toolchain_info.default_info,
toolchain_info.template_variables,
]

Expand Down
8 changes: 7 additions & 1 deletion helm/private/helm_registry.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ def _helm_push_impl(ctx):
output = registrar,
substitutions = {
"{chart}": pkg_info.chart.short_path,
"{helm_plugins}": toolchain.plugins_dir.short_path,
"{helm}": toolchain.helm.short_path,
"{image_pushers}": image_commands,
"{registry_url}": registry_url,
},
is_executable = True,
)

runfiles = ctx.runfiles([registrar, toolchain.helm, pkg_info.chart]).merge(image_runfiles)
runfiles = ctx.runfiles([
registrar,
toolchain.helm,
pkg_info.chart,
toolchain.plugins_dir,
]).merge(image_runfiles)

return [
DefaultInfo(
Expand Down
132 changes: 132 additions & 0 deletions helm/private/helm_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
"""rules_helm toolchain implementation"""

HelmPluginInfo = provider(
doc = "Info about a Helm plugin.",
fields = {
"data": "Depset[File]: Files associated with the plugin.",
"name": "String: The name of the plugin.",
"yaml": "File: The yaml file representing the plugin.",
},
)

def _helm_plugin_impl(ctx):
name = ctx.attr.plugin_name
if not name:
name = ctx.label.name

return [
HelmPluginInfo(
yaml = ctx.file.yaml,
name = name,
data = depset(ctx.files.data),
),
]

helm_plugin = rule(
doc = "Define a [helm plugin](https://helm.sh/docs/topics/plugins/).",
implementation = _helm_plugin_impl,
attrs = {
"data": attr.label_list(
doc = "Additional files associated with the plugin.",
allow_files = True,
),
"plugin_name": attr.string(
doc = "An explicit name for the plugin. If unset, `name` will be used.",
),
"yaml": attr.label(
doc = "The yaml file representing the plugin",
allow_single_file = True,
mandatory = True,
),
},
)

def _create_plugins_dir(*, ctx, plugins, output):
manifest = ctx.actions.declare_file("{}.manifest".format(output.basename), sibling = output)
manifest_data = {}
inputs = [depset([manifest])]
for plugin in plugins:
info = plugin[HelmPluginInfo]
inputs.append(depset([info.yaml], transitive = [info.data]))

if info.name in manifest_data:
fail("Two plugins sharing the name {} were provided. Please update {}".format(
info.name,
ctx.label,
))

manifest_data[info.name] = {
"data": [f.path for f in info.data.to_list()],
"yaml": info.yaml.path,
}

ctx.actions.write(
output = manifest,
content = json.encode_indent(manifest_data, indent = " " * 4) + "\n",
)

args = ctx.actions.args()
args.add("-manifest", manifest)
args.add("-output", output.path)

ctx.actions.run(
mnemonic = "HelmPluginsDir",
progress_message = "HelmPluginsDir %{label}",
executable = ctx.executable._plugins_builder,
arguments = [args],
inputs = depset(transitive = inputs),
outputs = [output],
)

return output

def _helm_toolchain_impl(ctx):
binary = ctx.file.helm

plugins_dir = _create_plugins_dir(
ctx = ctx,
plugins = ctx.attr.plugins,
output = ctx.actions.declare_directory("{}.plugins".format(ctx.label.name)),
)

template_variables = platform_common.TemplateVariableInfo({
"HELM_BIN": binary.path,
"HELM_PLUGINS": plugins_dir.path,
})

default_info = DefaultInfo(
files = depset([binary]),
runfiles = ctx.runfiles(files = [binary, plugins_dir]),
)

toolchain_info = platform_common.ToolchainInfo(
helm = binary,
default_info = default_info,
plugins_dir = plugins_dir,
template_variables = template_variables,
)

return [default_info, toolchain_info, template_variables]

helm_toolchain = rule(
implementation = _helm_toolchain_impl,
doc = "A helm toolchain.",
attrs = {
"helm": attr.label(
doc = "A helm binary",
allow_single_file = True,
mandatory = True,
cfg = "exec",
),
"plugins": attr.label_list(
doc = "Additional plugins to make available to helm.",
cfg = "exec",
providers = [HelmPluginInfo],
),
"_plugins_builder": attr.label(
default = Label("//helm/private/plugin:plugin_builder"),
cfg = "exec",
executable = True,
),
},
)
7 changes: 7 additions & 0 deletions helm/private/plugin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary")

go_binary(
name = "plugin_builder",
srcs = ["plugin_builder.go"],
visibility = ["//visibility:public"],
)
Loading

0 comments on commit cca8527

Please sign in to comment.