Skip to content

Commit

Permalink
correct handling of dependency runtimes
Browse files Browse the repository at this point in the history
  • Loading branch information
apesternikov committed Nov 26, 2022
1 parent 3083989 commit 2abe34f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
18 changes: 15 additions & 3 deletions skylib/kustomize/kustomize.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def _kustomize_impl(ctx):
else:
ctx.actions.write(kustomization_yaml_file, kustomization_yaml)

transitive_runfiles = []
resolver_part = ""
if ctx.attr.images:
resolver_part += " | {resolver} ".format(resolver = ctx.executable._resolver.path)
Expand All @@ -199,6 +200,7 @@ def _kustomize_impl(ctx):
if kpi.legacy_image_name:
resolver_part += " --image {}={}@$(cat {})".format(kpi.legacy_image_name, regrepo, kpi.digestfile.path)
tmpfiles.append(kpi.digestfile)
transitive_runfiles.append(img[DefaultInfo].default_runfiles)

template_part = ""
if ctx.attr.substitutions or ctx.attr.deps:
Expand Down Expand Up @@ -258,6 +260,8 @@ def _kustomize_impl(ctx):
tools = [ctx.executable._kustomize_bin],
)

runfiles = ctx.runfiles(files = ctx.files.deps).merge_all(transitive_runfiles)

transitive_files = [m[DefaultInfo].files for m in ctx.attr.manifests if KustomizeInfo in m]
transitive_files += [obj[DefaultInfo].files for obj in ctx.attr.objects]

Expand All @@ -270,6 +274,7 @@ def _kustomize_impl(ctx):
[ctx.outputs.yaml],
transitive = transitive_files,
),
runfiles = runfiles,
),
KustomizeInfo(
image_pushes = depset(
Expand Down Expand Up @@ -501,10 +506,14 @@ def _kubectl_impl(ctx):

statements = ""
transitive = None
transitive_runfiles = []

files += [ctx.executable._template_engine, ctx.file._info_file]

if ctx.attr.push:
trans_img_pushes = depset(transitive = [obj[KustomizeInfo].image_pushes for obj in ctx.attr.srcs]).to_list()
statements += "\n".join([
"# {}\n".format(exe[K8sPushInfo].image_label) +
"echo pushing {}/{}".format(exe[K8sPushInfo].registry, exe[K8sPushInfo].repository)
for exe in trans_img_pushes
]) + "\n"
Expand All @@ -514,6 +523,7 @@ def _kubectl_impl(ctx):
]) + "\nwaitpids\n"
files += [obj.files_to_run.executable for obj in trans_img_pushes]
transitive = depset(transitive = [obj.default_runfiles.files for obj in trans_img_pushes])
transitive_runfiles += [exe[DefaultInfo].default_runfiles for exe in trans_img_pushes]

namespace = ctx.attr.namespace
for inattr in ctx.attr.srcs:
Expand All @@ -528,17 +538,19 @@ def _kubectl_impl(ctx):
info_file = ctx.file._info_file.short_path,
)

files += [ctx.executable._template_engine, ctx.file._info_file]

ctx.actions.expand_template(
template = ctx.file._template,
substitutions = {
"%{statements}": statements,
},
output = ctx.outputs.executable,
)

runfiles = ctx.runfiles(files = files, transitive_files = transitive)
runfiles = runfiles.merge_all(transitive_runfiles)

return [
DefaultInfo(runfiles = ctx.runfiles(files = files, transitive_files = transitive)),
DefaultInfo(runfiles = runfiles),
]

kubectl = rule(
Expand Down
39 changes: 10 additions & 29 deletions skylib/push.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ def _impl(ctx):
# the image was already pushed, just rename if needed. Ignore registry and repository parameters
kpi = ctx.attr.image[K8sPushInfo]
ctx.actions.write(
content = "#!/bin/bash\n# This file intentionally left blank\n",
content = "#!/bin/bash\n{actual_pusher}\n".format(
actual_pusher = ctx.attr.image[DefaultInfo].files_to_run.executable.short_path,
),
output = ctx.outputs.executable,
is_executable = True,
)

runfiles = ctx.runfiles(files = []).merge(ctx.attr.image[DefaultInfo].default_runfiles)

ctx.actions.run_shell(
tools = [kpi.digestfile],
outputs = [ctx.outputs.digest],
Expand All @@ -67,7 +71,11 @@ def _impl(ctx):
)

return [
# we do not need to provide any executable
# we need to provide executable that calls the actual pusher
DefaultInfo(
executable = ctx.outputs.executable,
runfiles = runfiles,
),
K8sPushInfo(
image_label = kpi.image_label,
legacy_image_name = ctx.attr.legacy_image_name, # this is the only difference
Expand Down Expand Up @@ -270,30 +278,3 @@ Args:
},
)

def _rename_impl(ctx):
kpi = ctx.attr.image_push[K8sPushInfo]
return [
K8sPushInfo(
image_label = kpi.image_label,
legacy_image_name = ctx.attr.legacy_image_name,
registry = kpi.registry,
repository = kpi.repository,
digestfile = kpi.digestfile,
),
]

rename = rule(
doc = "Rename a container push.",
implementation = _rename_impl,
attrs = {
"image_push": attr.label(
providers = [K8sPushInfo],
mandatory = True,
doc = "The label of the pushed image.",
),
"legacy_image_name": attr.string(
mandatory = True,
doc = "The new name of the image.",
),
},
)

0 comments on commit 2abe34f

Please sign in to comment.