Skip to content

Commit 096cea9

Browse files
committed
allow to repackage third party charts
1 parent 9093218 commit 096cea9

5 files changed

Lines changed: 89 additions & 26 deletions

File tree

docs/rules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Defines a helm chart.
1919
| :------------- | :------------- | :------------- | :------------- | :------------- |
2020
| <a id="helm_chart-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
2121
| <a id="helm_chart-app_version"></a>app_version | Set the appVersion on the chart to this version. | String | optional | "" |
22-
| <a id="helm_chart-deps"></a>deps | Chart dependencies; must much the ones defined in a Chart.yaml file.. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
22+
| <a id="helm_chart-deps"></a>deps | Chart dependencies; must much the ones defined in a Chart.yaml file. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
2323
| <a id="helm_chart-image_digest_vars"></a>image_digest_vars | Map of container image labels to a template keys, to extract digest from. <pre><code>image_digest_vars = {<br><br> ":image": "{img_digest}",<br><br>}</code></pre> | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: Label -> String</a> | optional | {} |
2424
| <a id="helm_chart-srcs"></a>srcs | Files to package into a chart; must contain a Chart.yaml file. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
2525
| <a id="helm_chart-values_files"></a>values_files | Specify values in a YAML file (can specify multiple). | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |

example/repackage/BUILD.bazel

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
load("//helm:defs.bzl", "helm_chart", "helm_install", "helm_lint_test", "helm_template")
2+
load("@bazel_tools//tools/build_rules:test_rules.bzl", "rule_test")
3+
4+
helm_chart(
5+
name = "chart",
6+
srcs = ["@redis//:srcs"],
7+
values_json = json.encode({
8+
"image": {
9+
"tag": "some-tag",
10+
},
11+
}),
12+
values_files = ["values.yaml"],
13+
)
14+
15+
rule_test(
16+
name = "chart_test",
17+
generates = ["chart.tgz"],
18+
rule = ":chart",
19+
)
20+
21+
helm_install(
22+
name = "install",
23+
chart = ":chart",
24+
release_name = "hello-world",
25+
)
26+
27+
rule_test(
28+
name = "install_test",
29+
generates = ["install"],
30+
rule = ":install",
31+
)
32+
33+
helm_template(
34+
name = "template",
35+
chart = ":chart",
36+
release_name = "hello-world",
37+
)
38+
39+
rule_test(
40+
name = "template_test",
41+
generates = ["template.yaml"],
42+
rule = ":template",
43+
)
44+
45+
helm_lint_test(
46+
name = "lint",
47+
chart = ":chart",
48+
)

example/repackage/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
architecture: standalone

helm/private/rules/chart.bzl

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ _ATTRS = {
4545
allow_files = [".yml", ".yaml"],
4646
),
4747
"deps": attr.label_list(
48-
doc = "Chart dependencies; must much the ones defined in a Chart.yaml file..",
48+
doc = "Chart dependencies; must much the ones defined in a Chart.yaml file.",
4949
allow_files = [".tgz"],
5050
),
5151
"_windows_constraint": attr.label(
@@ -109,14 +109,12 @@ def _build_user_values_file(ctx):
109109
return out_yaml
110110

111111
def _package(ctx, srcs, user_values_file):
112-
chart_path = None
113-
for f in srcs:
114-
if f.path.endswith("Chart.yaml"):
115-
chart_path = f.dirname
116-
break
117-
if not chart_path:
112+
chart_yaml = _root_file("Chart.yaml", srcs)
113+
if chart_yaml == None:
118114
fail("Chart.yaml file is not found in srcs")
119115

116+
chart_path = chart_yaml.dirname
117+
120118
out = ctx.actions.declare_directory(ctx.label.name + "-helm-chart-out")
121119

122120
args = []
@@ -150,24 +148,32 @@ def _package(ctx, srcs, user_values_file):
150148

151149
return out
152150

153-
def _merge_values(ctx, user_values_file):
154-
default_values_file = None
155-
for f in ctx.files.srcs:
156-
if f.path.endswith("values.yaml"):
157-
default_values_file = f
158-
break
159-
160-
if not default_values_file:
151+
def _file_len(file):
152+
str = file.short_path
153+
return len(str)
154+
155+
def _root_file(name, files):
156+
found = []
157+
for f in files:
158+
if f.basename == name:
159+
found.append(f)
160+
found = sorted(found, key = _file_len)
161+
if len(found) == 0:
161162
return None
163+
return found[0]
164+
165+
def _merge_values(ctx, user_values_file):
166+
default_values_file = _root_file("values.yaml", ctx.files.srcs)
162167

163168
out_yaml = ctx.actions.declare_file(ctx.label.name + "-merged-values.yaml")
164169

170+
inputs = []
165171
args = ctx.actions.args()
166172
args.add("merge-yamls")
167173
args.add("-output", out_yaml.path)
168-
args.add("-file", default_values_file.path)
169-
170-
inputs = [default_values_file]
174+
if default_values_file:
175+
args.add("-file", default_values_file.path)
176+
inputs.append(default_values_file)
171177

172178
for f in ctx.files.values_files:
173179
args.add("-file", f.path)
@@ -188,13 +194,12 @@ def _merge_values(ctx, user_values_file):
188194
return out_yaml
189195

190196
def _copy_chart(ctx, merged_values_file):
197+
values_file = _root_file("values.yaml", ctx.files.srcs)
198+
191199
outs = []
192-
chart_path = None
193200
for f in ctx.files.srcs:
194201
src = f
195-
if f.path.endswith("Chart.yaml"):
196-
chart_path = ctx.label.name + "/" + f.dirname
197-
if f.path.endswith("values.yaml") and merged_values_file:
202+
if f == values_file and merged_values_file:
198203
src = merged_values_file
199204
copy = ctx.actions.declare_file(ctx.label.name + "/" + f.path)
200205
outs.append(copy)
@@ -212,8 +217,10 @@ def _copy_chart(ctx, merged_values_file):
212217
progress_message = "Copying %s file to %s " % (src.short_path, copy.short_path),
213218
)
214219

215-
if not chart_path:
220+
chart_yaml = _root_file("Chart.yaml", ctx.files.srcs)
221+
if chart_yaml == None:
216222
fail("Chart.yaml file is not found in srcs")
223+
chart_path = ctx.label.name + "/" + chart_yaml.dirname
217224

218225
for f in ctx.files.deps:
219226
src = f

helm/private/rules/import.bzl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,20 @@ def _impl(repository_ctx):
3333
),
3434
sha256 = repository_ctx.attr.sha256,
3535
)
36+
repository_ctx.extract(
37+
archive = file_name,
38+
)
3639
repository_ctx.file("BUILD.bazel", content = """
3740
package(default_visibility = ["//visibility:public"])
3841
filegroup(
3942
name = "chart",
40-
srcs = glob(["{}"]),
43+
srcs = ["{}"],
44+
)
45+
filegroup(
46+
name = "srcs",
47+
srcs = glob(["{}/**"]),
4148
)
42-
""".format(file_name))
49+
""".format(file_name, repository_ctx.attr.chart_name))
4350

4451
helm_import = repository_rule(
4552
doc = _DOC,

0 commit comments

Comments
 (0)