Skip to content

Commit e295941

Browse files
authored
Register toolchains for all versions of helm (#246)
This change registers all versions of helm known to `rules_helm` and adds `--@rules_helm//helm/settings:version` to control which version is used.
1 parent bf2c596 commit e295941

File tree

5 files changed

+62
-84
lines changed

5 files changed

+62
-84
lines changed

MODULE.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use_repo(
3434
)
3535

3636
helm = use_extension("@rules_helm//helm:extensions.bzl", "helm")
37-
helm.toolchain()
3837
helm.host_tools()
3938
use_repo(
4039
helm,

helm/extensions.bzl

Lines changed: 26 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Bzlmod extensions"""
22

3-
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
43
load(
54
"//helm/private:repositories.bzl",
65
"helm_host_alias_repository",
@@ -33,27 +32,17 @@ def _find_modules(module_ctx):
3332
def _helm_impl(module_ctx):
3433
root_mod, rules_mod = _find_modules(module_ctx)
3534

36-
toolchains = root_mod.tags.toolchain
37-
if not toolchains:
38-
toolchains = rules_mod.tags.toolchain
39-
4035
host_tools = root_mod.tags.host_tools
4136
if not host_tools:
4237
host_tools = rules_mod.tags.host_tools
4338

44-
for attrs in toolchains:
45-
if attrs.version not in HELM_VERSIONS:
46-
fail("Helm toolchain hub `{}` was given unsupported version `{}`. Try: {}".format(
47-
attrs.name,
48-
attrs.version,
49-
HELM_VERSIONS.keys(),
50-
))
51-
available = HELM_VERSIONS[attrs.version]
52-
toolchain_names = []
53-
toolchain_labels = {}
54-
target_compatible_with = {}
55-
exec_compatible_with = {}
39+
toolchain_names = []
40+
toolchain_labels = {}
41+
target_compatible_with = {}
42+
exec_compatible_with = {}
43+
target_settings = {}
5644

45+
for version, available in HELM_VERSIONS.items():
5746
for platform, integrity in available.items():
5847
if platform.startswith("windows"):
5948
compression = "zip"
@@ -67,99 +56,53 @@ def _helm_impl(module_ctx):
6756
if url_platform == "linux-i386":
6857
url_platform = "linux-386"
6958

70-
toolchain_repo_name = "{}__{}_{}_bin".format(attrs.name, attrs.version, platform.replace("-", "_"))
59+
toolchain_repo_name = "helm_toolchains__{}_{}_bin".format(version, platform.replace("-", "_"))
7160

72-
# Create the hub-specific binary repository
73-
maybe(
74-
helm_toolchain_repository,
61+
helm_toolchain_repository(
7562
name = toolchain_repo_name,
7663
urls = [
7764
template.replace(
7865
"{version}",
79-
attrs.version,
66+
version,
8067
).replace(
8168
"{platform}",
8269
url_platform,
8370
).replace(
8471
"{compression}",
8572
compression,
8673
)
87-
for template in attrs.helm_url_templates
74+
for template in DEFAULT_HELM_URL_TEMPLATES
8875
],
8976
integrity = integrity,
9077
strip_prefix = url_platform,
91-
plugins = attrs.plugins,
9278
platform = platform,
9379
)
9480

9581
toolchain_names.append(toolchain_repo_name)
9682
toolchain_labels[toolchain_repo_name] = "@{}".format(toolchain_repo_name)
9783
target_compatible_with[toolchain_repo_name] = []
9884
exec_compatible_with[toolchain_repo_name] = CONSTRAINTS[platform]
85+
target_settings[toolchain_repo_name] = ["@rules_helm//helm/settings:version_{}".format(version)]
86+
87+
helm_toolchain_repository_hub(
88+
name = "helm_toolchains",
89+
toolchain_labels = toolchain_labels,
90+
toolchain_names = toolchain_names,
91+
exec_compatible_with = exec_compatible_with,
92+
target_compatible_with = target_compatible_with,
93+
target_settings = target_settings,
94+
)
9995

100-
maybe(
101-
helm_toolchain_repository_hub,
102-
name = attrs.name,
103-
toolchain_labels = toolchain_labels,
104-
toolchain_names = toolchain_names,
105-
exec_compatible_with = exec_compatible_with,
106-
target_compatible_with = target_compatible_with,
107-
)
108-
109-
# Process host_tools tags
11096
for host_tools_attrs in host_tools:
111-
maybe(
112-
helm_host_alias_repository,
97+
helm_host_alias_repository(
11398
name = host_tools_attrs.name,
99+
toolchain_repo_prefix = "helm_toolchains__{}".format(host_tools_attrs.version),
114100
)
115101

116102
return module_ctx.extension_metadata(
117103
reproducible = True,
118104
)
119105

120-
_toolchain = tag_class(
121-
doc = """\
122-
An extension for defining a `helm_toolchain` from a download archive.
123-
124-
An example of defining and registering toolchains:
125-
126-
```python
127-
helm = use_extension("@rules_helm//helm:extensions.bzl", "helm")
128-
helm.toolchain(
129-
name = "helm_toolchains",
130-
version = "3.14.4",
131-
)
132-
use_repo(helm, "helm_toolchains")
133-
134-
register_toolchains(
135-
"@helm_toolchains//:all",
136-
)
137-
```
138-
""",
139-
attrs = {
140-
"helm_url_templates": attr.string_list(
141-
doc = (
142-
"A url template used to download helm. The template can contain the following " +
143-
"format strings `{platform}` for the helm platform, `{version}` for the helm " +
144-
"version, and `{compression}` for the archive type containing the helm binary."
145-
),
146-
default = DEFAULT_HELM_URL_TEMPLATES,
147-
),
148-
"name": attr.string(
149-
doc = "The name of the toolchain hub repository.",
150-
default = "helm_toolchains",
151-
),
152-
"plugins": attr.string_list(
153-
doc = "A list of plugins to add to the generated toolchain.",
154-
default = [],
155-
),
156-
"version": attr.string(
157-
doc = "The version of helm to download for the toolchain.",
158-
default = DEFAULT_HELM_VERSION,
159-
),
160-
},
161-
)
162-
163106
_host_tools = tag_class(
164107
doc = """\
165108
An extension for creating a host alias repository that provides a shorter name for the host platform's helm binary.
@@ -179,13 +122,16 @@ use_repo(helm, "helm")
179122
doc = "The name of the host alias repository.",
180123
default = "helm",
181124
),
125+
"version": attr.string(
126+
doc = "The version of helm to use for host tools.",
127+
default = DEFAULT_HELM_VERSION,
128+
),
182129
},
183130
)
184131

185132
helm = module_extension(
186133
implementation = _helm_impl,
187134
tag_classes = {
188135
"host_tools": _host_tools,
189-
"toolchain": _toolchain,
190136
},
191137
)

helm/private/repositories.bzl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ exports_files(["helm{ext}"])
163163
ext = ext,
164164
))
165165

166-
repository_ctx.symlink("../{name}_{platform}/helm{ext}".format(
167-
name = repository_ctx.attr.name,
166+
repository_ctx.symlink("../{prefix}_{platform}_bin/helm{ext}".format(
167+
prefix = repository_ctx.attr.toolchain_repo_prefix,
168168
platform = _platform(repository_ctx),
169169
ext = ext,
170170
), "helm{ext}".format(ext = ext))
@@ -174,13 +174,20 @@ helm_host_alias_repository = repository_rule(
174174
doc = """Creates a repository with a shorter name meant for the host platform, which contains
175175
a BUILD.bazel file that exports symlinks to the host platform's binaries
176176
""",
177+
attrs = {
178+
"toolchain_repo_prefix": attr.string(
179+
doc = "The name prefix for binary repos to symlink to. The host alias constructs the full repo name as {prefix}_{platform}_bin.",
180+
mandatory = True,
181+
),
182+
},
177183
)
178184

179185
_BUILD_FILE_FOR_TOOLCHAIN_HUB_TEMPLATE = """
180186
toolchain(
181187
name = "{name}",
182188
exec_compatible_with = {exec_constraint_sets_serialized},
183189
target_compatible_with = {target_constraint_sets_serialized},
190+
target_settings = {target_settings_serialized},
184191
toolchain = "{toolchain}",
185192
toolchain_type = "@rules_helm//helm:toolchain_type",
186193
visibility = ["//visibility:public"],
@@ -191,12 +198,14 @@ def _BUILD_for_toolchain_hub(
191198
toolchain_names,
192199
toolchain_labels,
193200
target_compatible_with,
194-
exec_compatible_with):
201+
exec_compatible_with,
202+
target_settings):
195203
return "\n".join([_BUILD_FILE_FOR_TOOLCHAIN_HUB_TEMPLATE.format(
196204
name = toolchain_name,
197205
exec_constraint_sets_serialized = json.encode(exec_compatible_with.get(toolchain_name, [])),
198206
target_constraint_sets_serialized = json.encode(target_compatible_with.get(toolchain_name, [])),
199207
toolchain = toolchain_labels[toolchain_name],
208+
target_settings_serialized = repr(target_settings.get(toolchain_name, None)),
200209
) for toolchain_name in toolchain_names])
201210

202211
def _helm_toolchain_repository_hub_impl(repository_ctx):
@@ -209,6 +218,7 @@ def _helm_toolchain_repository_hub_impl(repository_ctx):
209218
toolchain_labels = repository_ctx.attr.toolchain_labels,
210219
target_compatible_with = repository_ctx.attr.target_compatible_with,
211220
exec_compatible_with = repository_ctx.attr.exec_compatible_with,
221+
target_settings = repository_ctx.attr.target_settings,
212222
))
213223

214224
helm_toolchain_repository_hub = repository_rule(
@@ -226,6 +236,10 @@ helm_toolchain_repository_hub = repository_rule(
226236
doc = "A list of constraints for the target platform for this toolchain, keyed by toolchain name.",
227237
mandatory = True,
228238
),
239+
"target_settings": attr.string_list_dict(
240+
doc = "A list of target settings for this toolchain, keyed by toolchain name.",
241+
mandatory = True,
242+
),
229243
"toolchain_labels": attr.string_dict(
230244
doc = "The name of the toolchain implementation target, keyed by toolchain name.",
231245
mandatory = True,

helm/settings/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
22
load(
33
":settings.bzl",
44
"lint_default_strict",
5+
"version",
56
)
67

78
package(default_visibility = ["//visibility:public"])
@@ -15,3 +16,5 @@ bzl_library(
1516
)
1617

1718
lint_default_strict()
19+
20+
version()

helm/settings/settings.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ Definitions for all `@rules_helm//helm` settings
66
load(
77
"@bazel_skylib//rules:common_settings.bzl",
88
"bool_flag",
9+
"string_flag",
910
)
11+
load("//helm/private:versions.bzl", "DEFAULT_HELM_VERSION", "HELM_VERSIONS")
1012

1113
def lint_default_strict():
1214
"""A flag to control whether or not `helm_lint_*` rules default `-strict` to `True`
@@ -15,3 +17,17 @@ def lint_default_strict():
1517
name = "lint_default_strict",
1618
build_setting_default = True,
1719
)
20+
21+
def version(name = "version"):
22+
"""The target version of helm"""
23+
string_flag(
24+
name = name,
25+
values = HELM_VERSIONS.keys(),
26+
build_setting_default = DEFAULT_HELM_VERSION,
27+
)
28+
29+
for ver in HELM_VERSIONS.keys():
30+
native.config_setting(
31+
name = "{}_{}".format(name, ver),
32+
flag_values = {str(Label("//helm/settings:{}".format(name))): ver},
33+
)

0 commit comments

Comments
 (0)