diff --git a/examples/bzlmod/root/kustomization.yaml b/examples/bzlmod/root/kustomization.yaml index 79fe6e3..2e2b3d2 100644 --- a/examples/bzlmod/root/kustomization.yaml +++ b/examples/bzlmod/root/kustomization.yaml @@ -15,3 +15,8 @@ patches: buildMetadata: - originAnnotations + +# commonLabels is deprecated and builds produce warnings to stderr +# These warnings are not shown for successful builds if the option `silent_on_success` is set to true +commonLabels: + "car": "ford-mustang" diff --git a/kustomize/kustomize-build-from-archived-runfiles b/kustomize/kustomize-build-from-archived-runfiles index 3773ef6..2861cc9 100755 --- a/kustomize/kustomize-build-from-archived-runfiles +++ b/kustomize/kustomize-build-from-archived-runfiles @@ -44,4 +44,27 @@ runfiles_archive_file="$(mktemp runfiles-archive-XXXXXX)" kustomization_dir="$(mktemp -d kustomization-target-XXXXXX)" "${zipper}" x "${runfiles_archive_file}" -d "${kustomization_dir}" -"${kustomize}" build "${kustomization_dir}/$(dirname ${target_file})" "${@}" +if [ "${KUSTOMIZE_RESOURCES__SILENT_ON_SUCCESS:-}" ]; then + STDERR_CAPTURE=$(mktemp) +fi + +_exit() { + EXIT_CODE=$? + + if [ "${STDERR_CAPTURE:-}" ]; then + if [ "$EXIT_CODE" != 0 ] || [ -z "${KUSTOMIZE_RESOURCES__SILENT_ON_SUCCESS:-}" ]; then + cat "$STDERR_CAPTURE" >&2 + fi + rm "$STDERR_CAPTURE" + fi + + exit $EXIT_CODE +} + +trap _exit EXIT + +if [ "${STDERR_CAPTURE:-}" ]; then + "${kustomize}" build "${kustomization_dir}/$(dirname ${target_file})" "${@}" 2> "$STDERR_CAPTURE" +else + "${kustomize}" build "${kustomization_dir}/$(dirname ${target_file})" "${@}" +fi diff --git a/kustomize/kustomize-build-from-runfiles b/kustomize/kustomize-build-from-runfiles index 480f6d5..f54205f 100755 --- a/kustomize/kustomize-build-from-runfiles +++ b/kustomize/kustomize-build-from-runfiles @@ -19,4 +19,27 @@ if [ -z "${target_runfile_path}" ]; then exit 1 fi -"${kustomize}" build "$(dirname ${target_runfile_path})" "${@}" +if [ "${KUSTOMIZE_RESOURCES__SILENT_ON_SUCCESS:-}" ]; then + STDERR_CAPTURE=$(mktemp) +fi + +_exit() { + EXIT_CODE=$? + + if [ "${STDERR_CAPTURE:-}" ]; then + if [ "$EXIT_CODE" != 0 ] || [ -z "${KUSTOMIZE_RESOURCES__SILENT_ON_SUCCESS:-}" ]; then + cat "$STDERR_CAPTURE" >&2 + fi + rm "$STDERR_CAPTURE" + fi + + exit $EXIT_CODE +} + +trap _exit EXIT + +if [ "${STDERR_CAPTURE:-}" ]; then + "${kustomize}" build "$(dirname ${target_runfile_path})" "${@}" 2> "$STDERR_CAPTURE" +else + "${kustomize}" build "$(dirname ${target_runfile_path})" "${@}" +fi diff --git a/kustomize/kustomize.bzl b/kustomize/kustomize.bzl index c015310..08e9281 100644 --- a/kustomize/kustomize.bzl +++ b/kustomize/kustomize.bzl @@ -153,6 +153,10 @@ _kustomized_resources_attrs = { doc = "The built result, as a YAML stream of KRM resources in separate documents.", mandatory = True, ), + "silent_on_success": attr.bool( + doc = "If true, suppress stderr output when the build succeeds.", + default = True, + ), } _kustomize_toolchain_type = "//tools/kustomize:toolchain_type" @@ -220,6 +224,9 @@ def _kustomized_resources_impl(ctx): ([helm_tool] if kustomization.requires_helm else []), outputs = [ctx.outputs.result], use_default_shell_env = use_default_shell_env, + env = { + "KUSTOMIZE_RESOURCES__SILENT_ON_SUCCESS": "1" if ctx.attr.silent_on_success else "", + }, mnemonic = mnemonic, progress_message = progress_message, )