From 2abe34fe21fb1061c16e8a6579846fa94e86aad0 Mon Sep 17 00:00:00 2001 From: Aleksey Pesternikov Date: Sat, 26 Nov 2022 13:44:39 -0800 Subject: [PATCH] correct handling of dependency runtimes --- skylib/kustomize/kustomize.bzl | 18 +++++++++++++--- skylib/push.bzl | 39 +++++++++------------------------- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/skylib/kustomize/kustomize.bzl b/skylib/kustomize/kustomize.bzl index 70cf2a7f..22d6afe9 100644 --- a/skylib/kustomize/kustomize.bzl +++ b/skylib/kustomize/kustomize.bzl @@ -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) @@ -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: @@ -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] @@ -270,6 +274,7 @@ def _kustomize_impl(ctx): [ctx.outputs.yaml], transitive = transitive_files, ), + runfiles = runfiles, ), KustomizeInfo( image_pushes = depset( @@ -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" @@ -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: @@ -528,8 +538,6 @@ 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 = { @@ -537,8 +545,12 @@ def _kubectl_impl(ctx): }, 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( diff --git a/skylib/push.bzl b/skylib/push.bzl index 9c04c897..4f2b5170 100644 --- a/skylib/push.bzl +++ b/skylib/push.bzl @@ -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], @@ -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 @@ -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.", - ), - }, -) \ No newline at end of file