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 Nov 2, 2024
1 parent 9b166ff commit 9ec2762
Show file tree
Hide file tree
Showing 25 changed files with 638 additions and 113 deletions.
48 changes: 46 additions & 2 deletions MODULE.bazel.lock

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

34 changes: 29 additions & 5 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 @@ -267,10 +288,10 @@ if the following environment variables are defined:
## 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 @@ -279,6 +300,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 Expand Up @@ -379,8 +401,8 @@ str: A json encoded string which represents `Chart.yaml` contents.

<pre>
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-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>)
<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>

Rules for producing a helm package and some convenience targets.
Expand Down Expand Up @@ -413,6 +435,7 @@ Rules for producing a helm package and some convenience 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` |
| <a id="helm_chart-registry_url"></a>registry_url | The registry url for the helm chart. `{name}.push_registry` is only defined when a value is passed here. | `None` |
| <a id="helm_chart-login_url"></a>login_url | The registry url to log into for publishing helm charts. | `None` |
| <a id="helm_chart-helm_opts"></a>helm_opts | Additional options to pass to helm. | `[]` |
| <a id="helm_chart-install_opts"></a>install_opts | Additional options to pass to `helm install`. | `[]` |
| <a id="helm_chart-upgrade_opts"></a>upgrade_opts | Additional options to pass to `helm upgrade`. | `[]` |
Expand All @@ -427,7 +450,7 @@ Rules for producing a helm package and some convenience targets.
## helm_register_toolchains

<pre>
helm_register_toolchains(<a href="#helm_register_toolchains-version">version</a>, <a href="#helm_register_toolchains-helm_url_templates">helm_url_templates</a>)
helm_register_toolchains(<a href="#helm_register_toolchains-version">version</a>, <a href="#helm_register_toolchains-helm_url_templates">helm_url_templates</a>, <a href="#helm_register_toolchains-plugins">plugins</a>)
</pre>

Register helm toolchains.
Expand All @@ -439,6 +462,7 @@ Register helm toolchains.
| :------------- | :------------- | :------------- |
| <a id="helm_register_toolchains-version"></a>version | The version of Helm to use | `"3.16.1"` |
| <a id="helm_register_toolchains-helm_url_templates"></a>helm_url_templates | A list of url templates where helm can be downloaded. | `["https://get.helm.sh/helm-v{version}-{platform}.{compression}"]` |
| <a id="helm_register_toolchains-plugins"></a>plugins | Labels to `helm_plugin` targets to add to generated toolchains. | `[]` |


<a id="rules_helm_dependencies"></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
10 changes: 8 additions & 2 deletions helm/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ def _helm_impl(ctx):
options = module.tags.options
version = options[0].version
helm_url_templates = options[0].helm_url_templates
plugins = options[0].plugins

_register_toolchains(version, helm_url_templates)
_register_toolchains(version, helm_url_templates, plugins)
_register_go_yaml()

def _register_toolchains(version, helm_url_templates):
def _register_toolchains(version, helm_url_templates, plugins):
if not version in HELM_VERSIONS:
fail("{} is not a supported version ({})".format(version, HELM_VERSIONS.keys()))

Expand Down Expand Up @@ -73,6 +74,7 @@ def _register_toolchains(version, helm_url_templates):
helm_toolchain_repository,
name = name + "_toolchain",
platform = platform,
plugins = plugins,
exec_compatible_with = CONSTRAINTS[platform],
)

Expand Down Expand Up @@ -100,6 +102,10 @@ options = tag_class(attrs = {
),
default = DEFAULT_HELM_URL_TEMPLATES,
),
"plugins": attr.string_list(
doc = "A list of plugins to add to the generated toolchain.",
default = [],
),
"version": attr.string(
doc = "The version of helm to download for the toolchain.",
default = DEFAULT_HELM_VERSION,
Expand Down
1 change: 0 additions & 1 deletion helm/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ stamp_build_setting(name = "stamp")
filegroup(
name = "bzl_srcs",
srcs = glob(["**/*.bzl"]) + [
"//helm/private/packager:bzl_srcs",
"//helm/private/pusher:bzl_srcs",
"//helm/private/runner:bzl_srcs",
"//helm/private/stamp:bzl_srcs",
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
4 changes: 4 additions & 0 deletions helm/private/helm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def helm_chart(
deps = None,
install_name = None,
registry_url = None,
login_url = None,
helm_opts = [],
install_opts = [],
upgrade_opts = [],
Expand Down Expand Up @@ -50,6 +51,7 @@ def helm_chart(
install_name (str, optional): The `helm install` name to use. `name` will be used if unset.
registry_url (str, Optional): The registry url for the helm chart. `{name}.push_registry`
is only defined when a value is passed here.
login_url (str, optional): The registry url to log into for publishing helm charts.
helm_opts (list, optional): Additional options to pass to helm.
install_opts (list, optional): Additional options to pass to `helm install`.
uninstall_opts (list, optional): Additional options to pass to `helm uninstall`.
Expand Down Expand Up @@ -91,6 +93,7 @@ def helm_chart(
package = name,
include_images = False,
registry_url = registry_url,
login_url = login_url,
**kwargs
)

Expand All @@ -99,6 +102,7 @@ def helm_chart(
include_images = True,
package = name,
registry_url = registry_url,
login_url = login_url,
**kwargs
)

Expand Down
8 changes: 8 additions & 0 deletions helm/private/helm_cmd/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "helm_cmd",
srcs = ["helm_cmd.go"],
importpath = "github.com/abrisco/rules_helm/helm/private/helm_cmd",
visibility = ["//helm:__subpackages__"],
)
43 changes: 43 additions & 0 deletions helm/private/helm_cmd/helm_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package helm_cmd

import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
)

func BuildHelmCommand(helmPath string, args []string, pluginsDir string) (exec.Cmd, error) {
// Create a temporary directory with a specified prefix.
tempDir, err := os.MkdirTemp(os.Getenv("TEST_TMPDIR"), "helm_cmd-")
if err != nil {
log.Fatal(err)
}

// Generate a fake kubeconfig for more consistent results when building packages
kubeconfig := filepath.Join(tempDir, ".kubeconfig")
file, err := os.Create(kubeconfig)
if err != nil {
log.Fatal(err)
}
if err := file.Chmod(0700); err != nil {
log.Fatal(err)
}
file.Close()

// Set the HELM_PLUGINS environment variable for plugins directory
cmd := exec.Command(helmPath, args...)

env := os.Environ()
env = append(env, fmt.Sprintf("HELM_PLUGINS=%s", pluginsDir))
env = append(env, fmt.Sprintf("HELM_CACHE_HOME=%s", filepath.Join(tempDir, "cache")))
env = append(env, fmt.Sprintf("HELM_CONFIG_HOME=%s", filepath.Join(tempDir, "config")))
env = append(env, fmt.Sprintf("HELM_DATA_HOME=%s", filepath.Join(tempDir, "data")))
env = append(env, fmt.Sprintf("HELM_REPOSITORY_CACHE=%s", filepath.Join(tempDir, "repository_cache")))
env = append(env, fmt.Sprintf("HELM_REPOSITORY_CONFIG=%s", filepath.Join(tempDir, "repositories.yaml")))
env = append(env, fmt.Sprintf("HELM_REGISTRY_CONFIG=%s", filepath.Join(tempDir, "config.json")))
env = append(env, fmt.Sprintf("KUBECONFIG=%s", kubeconfig))

return *cmd, nil
}
Loading

0 comments on commit 9ec2762

Please sign in to comment.