Skip to content

Commit

Permalink
Added include_images to helm_push rule. (#110)
Browse files Browse the repository at this point in the history
This change also renames the `helm_push` rule to `helm_push_images` so
that `helm_push_registry` can be renamed to `helm_push`.
  • Loading branch information
abrisco authored Oct 29, 2024
1 parent a655ce5 commit ea63d24
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 116 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module(
name = "rules_helm",
version = "0.7.0",
version = "0.8.0",
compatibility_level = 1,
)

Expand Down
16 changes: 10 additions & 6 deletions MODULE.bazel.lock

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

57 changes: 40 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bazel_dep(name = "rules_helm", version = "{version}")
- [helm_lint_test](#helm_lint_test)
- [helm_package](#helm_package)
- [helm_push](#helm_push)
- [helm_push_registry](#helm_push_registry)
- [helm_push_images](#helm_push_images)
- [helm_register_toolchains](#helm_register_toolchains)
- [helm_toolchain](#helm_toolchain)
- [helm_uninstall](#helm_uninstall)
Expand Down Expand Up @@ -123,7 +123,7 @@ A rule that allows pre-packaged Helm charts to be used within Bazel.
helm_install(<a href="#helm_install-name">name</a>, <a href="#helm_install-data">data</a>, <a href="#helm_install-helm_opts">helm_opts</a>, <a href="#helm_install-install_name">install_name</a>, <a href="#helm_install-opts">opts</a>, <a href="#helm_install-package">package</a>)
</pre>

Produce a script for performing a helm install action
Produce an executable for performing a `helm install` operation.

**ATTRIBUTES**

Expand Down Expand Up @@ -191,36 +191,58 @@ Rules for creating Helm chart packages.
## helm_push

<pre>
helm_push(<a href="#helm_push-name">name</a>, <a href="#helm_push-package">package</a>)
helm_push(<a href="#helm_push-name">name</a>, <a href="#helm_push-include_images">include_images</a>, <a href="#helm_push-package">package</a>, <a href="#helm_push-registry_url">registry_url</a>)
</pre>

Produce a script for pushing all oci images used by a helm chart
Produce an executable for performing a helm push to a registry.

**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="helm_push-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="helm_push-package"></a>package | The helm package to upload images from. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="helm_push-include_images"></a>include_images | If True, images depended on by `package` will be pushed as well. | Boolean | optional | `False` |
| <a id="helm_push-package"></a>package | The helm package to push to the registry. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="helm_push-registry_url"></a>registry_url | The URL of the registry. | String | required | |


<a id="helm_push_images"></a>

## helm_push_images

<pre>
helm_push_images(<a href="#helm_push_images-name">name</a>, <a href="#helm_push_images-package">package</a>)
</pre>

Produce an executable for pushing all oci images used by a helm chart.

**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="helm_push_images-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="helm_push_images-package"></a>package | The helm package to upload images from. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |


<a id="helm_push_registry"></a>

## helm_push_registry

<pre>
helm_push_registry(<a href="#helm_push_registry-name">name</a>, <a href="#helm_push_registry-package">package</a>, <a href="#helm_push_registry-registry_url">registry_url</a>)
helm_push_registry(<a href="#helm_push_registry-name">name</a>, <a href="#helm_push_registry-include_images">include_images</a>, <a href="#helm_push_registry-package">package</a>, <a href="#helm_push_registry-registry_url">registry_url</a>)
</pre>

Produce a script for performing a helm push to a registry
Produce an executable for performing a helm push to a registry.

**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="helm_push_registry-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="helm_push_registry-include_images"></a>include_images | If True, images depended on by `package` will be pushed as well. | Boolean | optional | `False` |
| <a id="helm_push_registry-package"></a>package | The helm package to push to the registry. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="helm_push_registry-registry_url"></a>registry_url | The URL of the registry. | String | required | |

Expand Down Expand Up @@ -252,7 +274,7 @@ A helm toolchain
helm_uninstall(<a href="#helm_uninstall-name">name</a>, <a href="#helm_uninstall-data">data</a>, <a href="#helm_uninstall-helm_opts">helm_opts</a>, <a href="#helm_uninstall-install_name">install_name</a>, <a href="#helm_uninstall-opts">opts</a>)
</pre>

Produce a script for performing a helm uninstall action
Produce an executable for performing a `helm uninstall` operation.

**ATTRIBUTES**

Expand All @@ -274,7 +296,7 @@ Produce a script for performing a helm uninstall action
helm_upgrade(<a href="#helm_upgrade-name">name</a>, <a href="#helm_upgrade-data">data</a>, <a href="#helm_upgrade-helm_opts">helm_opts</a>, <a href="#helm_upgrade-install_name">install_name</a>, <a href="#helm_upgrade-opts">opts</a>, <a href="#helm_upgrade-package">package</a>)
</pre>

Produce a script for performing a helm upgrade action
Produce an executable for performing a `helm upgrade` operation.

**ATTRIBUTES**

Expand Down Expand Up @@ -348,14 +370,15 @@ helm_chart(<a href="#helm_chart-name">name</a>, <a href="#helm_chart-chart">char

Rules for producing a helm package and some convenience targets.

| target | rule |
| --- | --- |
| `{name}` | [helm_package](#helm_package) |
| `{name}.push` | [helm_push](#helm_push) |
| `{name}.push_registry` | [helm_push_registry](#helm_push_registry) |
| `{name}.install` | [helm_install](#helm_install) |
| `{name}.uninstall` | [helm_uninstall](#helm_uninstall) |
| `{name}.upgrade` | [helm_upgrade](#helm_upgrade) |
| target | rule | condition |
| --- | --- | --- |
| `{name}` | [helm_package](#helm_package) | `None` |
| `{name}.push_images` | [helm_push_images](#helm_push_images) | `None` |
| `{name}.push_registry` | [helm_push](#helm_push) (`include_images = False`) | `registry_url` is defined. |
| `{name}.push` | [helm_push](#helm_push) (`include_images = True`) | `registry_url` is defined. |
| `{name}.install` | [helm_install](#helm_install) | `None` |
| `{name}.uninstall` | [helm_uninstall](#helm_uninstall) | `None` |
| `{name}.upgrade` | [helm_upgrade](#helm_upgrade) | `None` |


**PARAMETERS**
Expand Down
9 changes: 5 additions & 4 deletions helm/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bazel_dep(name = "rules_helm", version = "{version}")
- [helm_lint_test](#helm_lint_test)
- [helm_package](#helm_package)
- [helm_push](#helm_push)
- [helm_push_registry](#helm_push_registry)
- [helm_push_images](#helm_push_images)
- [helm_register_toolchains](#helm_register_toolchains)
- [helm_toolchain](#helm_toolchain)
- [helm_uninstall](#helm_uninstall)
Expand Down Expand Up @@ -88,7 +88,6 @@ load(
load(
"//helm/private:helm_install.bzl",
_helm_install = "helm_install",
_helm_push = "helm_push",
_helm_uninstall = "helm_uninstall",
_helm_upgrade = "helm_upgrade",
)
Expand All @@ -103,7 +102,8 @@ load(
)
load(
"//helm/private:helm_registry.bzl",
_helm_push_registry = "helm_push_registry",
_helm_push = "helm_push",
_helm_push_images = "helm_push_images",
)
load(
":providers.bzl",
Expand All @@ -126,8 +126,9 @@ helm_install = _helm_install
helm_lint_aspect = _helm_lint_aspect
helm_lint_test = _helm_lint_test
helm_package = _helm_package
helm_push_registry = _helm_push_registry
helm_push = _helm_push
helm_push_images = _helm_push_images
helm_push_registry = _helm_push
helm_register_toolchains = _helm_register_toolchains
helm_toolchain = _helm_toolchain
helm_uninstall = _helm_uninstall
Expand Down
36 changes: 23 additions & 13 deletions helm/private/helm.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Helm rules"""

load("//helm/private:helm_install.bzl", "helm_install", "helm_push", "helm_uninstall", "helm_upgrade")
load("//helm/private:helm_install.bzl", "helm_install", "helm_uninstall", "helm_upgrade")
load("//helm/private:helm_package.bzl", "helm_package")
load("//helm/private:helm_registry.bzl", "helm_push_registry")
load("//helm/private:helm_registry.bzl", "helm_push", "helm_push_images")

def helm_chart(
name,
Expand All @@ -26,14 +26,15 @@ def helm_chart(
**kwargs):
"""Rules for producing a helm package and some convenience targets.
| target | rule |
| --- | --- |
| `{name}` | [helm_package](#helm_package) |
| `{name}.push` | [helm_push](#helm_push) |
| `{name}.push_registry` | [helm_push_registry](#helm_push_registry) |
| `{name}.install` | [helm_install](#helm_install) |
| `{name}.uninstall` | [helm_uninstall](#helm_uninstall) |
| `{name}.upgrade` | [helm_upgrade](#helm_upgrade) |
| target | rule | condition |
| --- | --- | --- |
| `{name}` | [helm_package](#helm_package) | `None` |
| `{name}.push_images` | [helm_push_images](#helm_push_images) | `None` |
| `{name}.push_registry` | [helm_push](#helm_push) (`include_images = False`) | `registry_url` is defined. |
| `{name}.push` | [helm_push](#helm_push) (`include_images = True`) | `registry_url` is defined. |
| `{name}.install` | [helm_install](#helm_install) | `None` |
| `{name}.uninstall` | [helm_uninstall](#helm_uninstall) | `None` |
| `{name}.upgrade` | [helm_upgrade](#helm_upgrade) | `None` |
Args:
name (str): The name of the [helm_package](#helm_package) target.
Expand Down Expand Up @@ -78,16 +79,25 @@ def helm_chart(
**kwargs
)

helm_push(
name = name + ".push",
helm_push_images(
name = name + ".push_images",
package = name,
**kwargs
)

if registry_url:
helm_push_registry(
helm_push(
name = name + ".push_registry",
package = name,
include_images = False,
registry_url = registry_url,
**kwargs
)

helm_push(
name = name + ".push",
include_images = True,
package = name,
registry_url = registry_url,
**kwargs
)
Expand Down
69 changes: 3 additions & 66 deletions helm/private/helm_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _helm_install_impl(ctx):
]

helm_install = rule(
doc = "Produce a script for performing a helm install action",
doc = "Produce an executable for performing a `helm install` operation.",
implementation = _helm_install_impl,
executable = True,
attrs = {
Expand Down Expand Up @@ -179,7 +179,7 @@ def _helm_upgrade_impl(ctx):
]

helm_upgrade = rule(
doc = "Produce a script for performing a helm upgrade action",
doc = "Produce an executable for performing a `helm upgrade` operation.",
implementation = _helm_upgrade_impl,
executable = True,
attrs = {
Expand Down Expand Up @@ -274,7 +274,7 @@ def _helm_uninstall_impl(ctx):
]

helm_uninstall = rule(
doc = "Produce a script for performing a helm uninstall action",
doc = "Produce an executable for performing a `helm uninstall` operation.",
implementation = _helm_uninstall_impl,
executable = True,
attrs = {
Expand Down Expand Up @@ -312,66 +312,3 @@ helm_uninstall = rule(
str(Label("//helm:toolchain_type")),
],
)

def _helm_push_impl(ctx):
toolchain = ctx.toolchains[Label("//helm:toolchain_type")]

if toolchain.helm.basename.endswith(".exe"):
pusher = ctx.actions.declare_file(ctx.label.name + ".bat")
else:
pusher = ctx.actions.declare_file(ctx.label.name + ".sh")

pkg_info = ctx.attr.package[HelmPackageInfo]

image_pushers = []
image_runfiles = []
for image in pkg_info.images:
image_pushers.append(image[DefaultInfo].files_to_run.executable)
image_runfiles.append(image[DefaultInfo].default_runfiles)

if image_pushers:
image_commands = "\n".join([pusher.short_path for pusher in image_pushers])
else:
image_commands = "echo 'No OCI images to push for Helm chart'"

ctx.actions.expand_template(
template = ctx.file._pusher,
output = pusher,
substitutions = {
"{image_pushers}": image_commands,
},
is_executable = True,
)

runfiles = ctx.runfiles([pusher] + image_pushers)
for ir in image_runfiles:
runfiles = runfiles.merge(ir)

return [
DefaultInfo(
files = depset([pusher]),
runfiles = runfiles,
executable = pusher,
),
]

helm_push = rule(
doc = "Produce a script for pushing all oci images used by a helm chart",
implementation = _helm_push_impl,
executable = True,
attrs = {
"package": attr.label(
doc = "The helm package to upload images from.",
providers = [HelmPackageInfo],
mandatory = True,
),
"_pusher": attr.label(
doc = "A template used to produce the pusher executable.",
allow_single_file = True,
default = Label("//helm/private/pusher:template"),
),
},
toolchains = [
str(Label("//helm:toolchain_type")),
],
)
Loading

0 comments on commit ea63d24

Please sign in to comment.