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..6d7addbdc 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 + labelsFilesArray 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(&labelsFilesArray, "labelsFile", "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 labelsFilesArray { + 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 bb27848d1..bda5d35d1 100644 --- a/container/image.bzl +++ b/container/image.bzl @@ -64,6 +64,7 @@ def _add_create_image_config_args( manifest, config, labels, + labels_files, label_files, entrypoint, cmd, @@ -108,6 +109,10 @@ def _add_create_image_config_args( for key, value in labels.items(): args.add("-labels", "{}={}".format(key, value)) + for labels_file in ctx.files.labels_files: + args.add("-labelsFile", labels_file) + inputs += ctx.files.labels_files + for key, value in env.items(): args.add("-env", "%s" % "=".join([ ctx.expand_make_variables("env", key, {}), @@ -175,6 +180,7 @@ def _image_config( null_entrypoint = False, null_cmd = False, labels = None, + labels_files = None, label_files = None, label_file_strings = None): """Create the configuration for a new container image.""" @@ -205,6 +211,7 @@ def _image_config( manifest, config, labels_fixed, + labels_files, label_files, entrypoint, cmd, @@ -678,6 +685,20 @@ _attrs = dicts.add(_layer.attrs, { The values of this field support stamp variables.""", ), + "labels_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.