Skip to content

Commit eb56d8e

Browse files
committed
Allow set container_image labels by json file
1 parent 7d34678 commit eb56d8e

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

container/go/cmd/create_image_config/create_image_config.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919

2020
import (
2121
"bytes"
22+
"encoding/json"
2223
"flag"
2324
"io/ioutil"
2425
"log"
@@ -43,6 +44,7 @@ var (
4344
operatingSystem = flag.String("operatingSystem", "linux", "Operating system to create docker image for, eg. linux.")
4445
osVersion = flag.String("osVersion", "", "Operating system version to create docker image for (primarily for windows).")
4546
labelsArray utils.ArrayStringFlags
47+
labelFilesArray utils.ArrayStringFlags
4648
ports utils.ArrayStringFlags
4749
volumes utils.ArrayStringFlags
4850
entrypointPrefix utils.ArrayStringFlags
@@ -55,6 +57,7 @@ var (
5557

5658
func main() {
5759
flag.Var(&labelsArray, "labels", "Augment the Label of the previous layer.")
60+
flag.Var(&labelFilesArray, "labelFile", "Augment the Label of the previous layer (json file with string-to-string dict).")
5861
flag.Var(&ports, "ports", "Augment the ExposedPorts of the previous layer.")
5962
flag.Var(&volumes, "volumes", "Augment the Volumes of the previous layer.")
6063
flag.Var(&entrypointPrefix, "entrypointPrefix", "Prefix the Entrypoint with the specified arguments.")
@@ -83,6 +86,20 @@ func main() {
8386
}
8487
}
8588

89+
for _, labelFile := range labelFilesArray {
90+
labelsBlob, err := ioutil.ReadFile(labelFile)
91+
if err != nil {
92+
log.Fatalf("Failed to read the labels JSON file: %v", err)
93+
}
94+
labels := make(map[string]string)
95+
if err := json.Unmarshal(labelsBlob, &labels); err != nil {
96+
log.Fatalf("Can't parse JSON file %q: %v", labelFile, err)
97+
}
98+
for name, value := range labels {
99+
labelsArray = append(labelsArray, name+"="+value)
100+
}
101+
}
102+
86103
stamper, err := compat.NewStamper(stampInfoFile)
87104
if err != nil {
88105
log.Fatalf("Failed to initialize the stamper: %v", err)

container/image.bzl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ def _add_create_image_config_args(
107107
for key, value in labels.items():
108108
args.add("-labels", "{}={}".format(key, value))
109109

110+
for label_file in ctx.files.label_files:
111+
args.add("-labelFile", label_file)
112+
inputs += ctx.files.label_files
113+
110114
for key, value in env.items():
111115
args.add("-env", "%s" % "=".join([
112116
ctx.expand_make_variables("env", key, {}),
@@ -121,8 +125,8 @@ def _add_create_image_config_args(
121125
inputs += layer_names
122126
args.add_all(layer_names, before_each = "-layerDigestFile", format_each = "@%s")
123127

124-
if ctx.attr.label_files:
125-
inputs += ctx.files.label_files
128+
if ctx.attr.label_file_deps:
129+
inputs += ctx.files.label_file_deps
126130

127131
if base_config:
128132
args.add("-baseConfig", base_config)
@@ -178,7 +182,7 @@ def _image_config(
178182
manifest = ctx.actions.declare_file(name + "." + layer_name + ".manifest")
179183

180184
label_file_dict = _string_to_label(
181-
ctx.files.label_files,
185+
ctx.files.label_file_deps,
182186
ctx.attr.label_file_strings,
183187
)
184188

@@ -640,7 +644,7 @@ _attrs = dicts.add(_layer.attrs, {
640644
),
641645
"label_file_strings": attr.string_list(),
642646
# Implicit/Undocumented dependencies.
643-
"label_files": attr.label_list(
647+
"label_file_deps": attr.label_list(
644648
allow_files = True,
645649
),
646650
"labels": attr.string_dict(
@@ -661,6 +665,20 @@ _attrs = dicts.add(_layer.attrs, {
661665
662666
The values of this field support stamp variables.""",
663667
),
668+
"label_files": attr.label_list(
669+
doc = """List of JSON files contains simple string-to-string dictionary with
670+
labels.
671+
672+
Example of file content:
673+
674+
{
675+
"com.example.foo": "bar",
676+
"com.example.baz": "@metadata.json"
677+
}
678+
679+
The values of this field support stamp variables.""",
680+
allow_files = True,
681+
),
664682
"launcher": attr.label(
665683
allow_single_file = True,
666684
doc = """If present, prefix the image's ENTRYPOINT with this file.
@@ -1006,7 +1024,7 @@ def container_image(**kwargs):
10061024
)
10071025

10081026
reserved_attrs = [
1009-
"label_files",
1027+
"label_file_deps",
10101028
"label_file_strings",
10111029
"null_cmd",
10121030
"null_entrypoint",
@@ -1018,7 +1036,7 @@ def container_image(**kwargs):
10181036

10191037
if "labels" in kwargs:
10201038
files = sorted({v[1:]: None for v in kwargs["labels"].values() if v[0] == "@"}.keys())
1021-
kwargs["label_files"] = files
1039+
kwargs["label_file_deps"] = files
10221040
kwargs["label_file_strings"] = files
10231041

10241042
# If cmd is set but set to None, [] or "",

0 commit comments

Comments
 (0)