From eb56d8eedbfb019a55ee1d4a9afce929a9534d50 Mon Sep 17 00:00:00 2001 From: "Artem V. Navrotskiy" Date: Wed, 13 Apr 2022 09:44:27 +0300 Subject: [PATCH] Allow set `container_image` labels by json file --- .../create_image_config.go | 17 +++++++++++ container/image.bzl | 30 +++++++++++++++---- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/container/go/cmd/create_image_config/create_image_config.go b/container/go/cmd/create_image_config/create_image_config.go index 3866951f1..6482ba32f 100644 --- a/container/go/cmd/create_image_config/create_image_config.go +++ b/container/go/cmd/create_image_config/create_image_config.go @@ -19,6 +19,7 @@ package main import ( "bytes" + "encoding/json" "flag" "io/ioutil" "log" @@ -43,6 +44,7 @@ var ( operatingSystem = flag.String("operatingSystem", "linux", "Operating system to create docker image for, eg. linux.") osVersion = flag.String("osVersion", "", "Operating system version to create docker image for (primarily for windows).") labelsArray utils.ArrayStringFlags + labelFilesArray utils.ArrayStringFlags ports utils.ArrayStringFlags volumes utils.ArrayStringFlags entrypointPrefix utils.ArrayStringFlags @@ -55,6 +57,7 @@ var ( func main() { flag.Var(&labelsArray, "labels", "Augment the Label of the previous layer.") + flag.Var(&labelFilesArray, "labelFile", "Augment the Label of the previous layer (json file with string-to-string dict).") flag.Var(&ports, "ports", "Augment the ExposedPorts of the previous layer.") flag.Var(&volumes, "volumes", "Augment the Volumes of the previous layer.") flag.Var(&entrypointPrefix, "entrypointPrefix", "Prefix the Entrypoint with the specified arguments.") @@ -83,6 +86,20 @@ func main() { } } + for _, labelFile := range labelFilesArray { + labelsBlob, err := ioutil.ReadFile(labelFile) + if err != nil { + log.Fatalf("Failed to read the labels JSON file: %v", err) + } + labels := make(map[string]string) + if err := json.Unmarshal(labelsBlob, &labels); err != nil { + log.Fatalf("Can't parse JSON file %q: %v", labelFile, err) + } + for name, value := range labels { + labelsArray = append(labelsArray, name+"="+value) + } + } + stamper, err := compat.NewStamper(stampInfoFile) if err != nil { log.Fatalf("Failed to initialize the stamper: %v", err) diff --git a/container/image.bzl b/container/image.bzl index 1893a3364..e4b4390fe 100644 --- a/container/image.bzl +++ b/container/image.bzl @@ -107,6 +107,10 @@ def _add_create_image_config_args( for key, value in labels.items(): args.add("-labels", "{}={}".format(key, value)) + for label_file in ctx.files.label_files: + args.add("-labelFile", label_file) + inputs += ctx.files.label_files + for key, value in env.items(): args.add("-env", "%s" % "=".join([ ctx.expand_make_variables("env", key, {}), @@ -121,8 +125,8 @@ def _add_create_image_config_args( inputs += layer_names args.add_all(layer_names, before_each = "-layerDigestFile", format_each = "@%s") - if ctx.attr.label_files: - inputs += ctx.files.label_files + if ctx.attr.label_file_deps: + inputs += ctx.files.label_file_deps if base_config: args.add("-baseConfig", base_config) @@ -178,7 +182,7 @@ def _image_config( manifest = ctx.actions.declare_file(name + "." + layer_name + ".manifest") label_file_dict = _string_to_label( - ctx.files.label_files, + ctx.files.label_file_deps, ctx.attr.label_file_strings, ) @@ -640,7 +644,7 @@ _attrs = dicts.add(_layer.attrs, { ), "label_file_strings": attr.string_list(), # Implicit/Undocumented dependencies. - "label_files": attr.label_list( + "label_file_deps": attr.label_list( allow_files = True, ), "labels": attr.string_dict( @@ -661,6 +665,20 @@ _attrs = dicts.add(_layer.attrs, { The values of this field support stamp variables.""", ), + "label_files": attr.label_list( + doc = """List of JSON files contains simple string-to-string dictionary with + labels. + + Example of file content: + + { + "com.example.foo": "bar", + "com.example.baz": "@metadata.json" + } + + The values of this field support stamp variables.""", + allow_files = True, + ), "launcher": attr.label( allow_single_file = True, doc = """If present, prefix the image's ENTRYPOINT with this file. @@ -1006,7 +1024,7 @@ def container_image(**kwargs): ) reserved_attrs = [ - "label_files", + "label_file_deps", "label_file_strings", "null_cmd", "null_entrypoint", @@ -1018,7 +1036,7 @@ def container_image(**kwargs): if "labels" in kwargs: files = sorted({v[1:]: None for v in kwargs["labels"].values() if v[0] == "@"}.keys()) - kwargs["label_files"] = files + kwargs["label_file_deps"] = files kwargs["label_file_strings"] = files # If cmd is set but set to None, [] or "",