forked from ddelnano/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kustomize.bzl
82 lines (71 loc) · 2.72 KB
/
kustomize.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
def _kustomize_build_impl(ctx):
output_fname = "{}.yaml".format(ctx.attr.name)
out = ctx.actions.declare_file(output_fname)
cmds = [
'KUSTOMIZE_BIN="$(realpath "{}")"'.format(ctx.executable._kustomize.path),
'TMP="$(mktemp -d)"',
]
for file in ctx.files.srcs:
cmds.append('cp --parents "{}" "$TMP"'.format(file.path))
cmds.append('cp --parents "{}" "$TMP"'.format(ctx.file.kustomization.path))
if len(ctx.attr.replacements) > 0:
cmds.append('pushd "$TMP/$(dirname "{}")" &> /dev/null'.format(ctx.file.kustomization.path))
for old, new in ctx.attr.replacements.items():
old_expanded = old.format(**ctx.var)
new_expanded = new.format(**ctx.var)
cmds.append('"$KUSTOMIZE_BIN" edit set image {}={}'.format(old_expanded, new_expanded))
cmds.append("popd &> /dev/null")
cmds.append('"$KUSTOMIZE_BIN" build "$TMP/$(dirname "{}")" -o "{}"'.format(ctx.file.kustomization.path, out.path))
cmds.append('rm -rf "$TMP"')
ctx.actions.run_shell(
tools = [ctx.executable._kustomize],
outputs = [out],
inputs = ctx.files.srcs + [ctx.file.kustomization],
command = " && ".join(cmds),
mnemonic = "KustomizeBuild",
)
return [
DefaultInfo(
files = depset([out]),
),
]
kustomize_build = rule(
implementation = _kustomize_build_impl,
attrs = dict({
"kustomization": attr.label(
mandatory = True,
allow_single_file = True,
),
"replacements": attr.string_dict(
doc = """
Will be passed as args to `kustomize edit set image <key>=<value>`
Supports make vars as templates. i.e. {MY_VAR} will be replaced with
values ctx.vars["MY_VAR"] if available.
""",
),
"srcs": attr.label_list(
mandatory = True,
allow_files = True,
),
"_kustomize": attr.label(
default = Label("@io_k8s_sigs_kustomize_kustomize_v4//:v4"),
executable = True,
cfg = "exec",
),
}),
)