Skip to content

Commit 7a4e35a

Browse files
committed
Allow set container_image labels by json file
1 parent 8e70c6b commit 7a4e35a

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
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+
labelsFilesArray 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(&labelsFilesArray, "labelsFile", "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 labelsFilesArray {
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def _add_create_image_config_args(
6464
manifest,
6565
config,
6666
labels,
67+
labels_files,
6768
label_files,
6869
entrypoint,
6970
cmd,
@@ -108,6 +109,10 @@ def _add_create_image_config_args(
108109
for key, value in labels.items():
109110
args.add("-labels", "{}={}".format(key, value))
110111

112+
for labels_file in ctx.files.labels_files:
113+
args.add("-labelsFile", labels_file)
114+
inputs += ctx.files.labels_files
115+
111116
for key, value in env.items():
112117
args.add("-env", "%s" % "=".join([
113118
ctx.expand_make_variables("env", key, {}),
@@ -175,6 +180,7 @@ def _image_config(
175180
null_entrypoint = False,
176181
null_cmd = False,
177182
labels = None,
183+
labels_files = None,
178184
label_files = None,
179185
label_file_strings = None):
180186
"""Create the configuration for a new container image."""
@@ -205,6 +211,7 @@ def _image_config(
205211
manifest,
206212
config,
207213
labels_fixed,
214+
labels_files,
208215
label_files,
209216
entrypoint,
210217
cmd,
@@ -678,6 +685,20 @@ _attrs = dicts.add(_layer.attrs, {
678685
679686
The values of this field support stamp variables.""",
680687
),
688+
"labels_files": attr.label_list(
689+
doc = """List of JSON files contains simple string-to-string dictionary with
690+
labels.
691+
692+
Example of file content:
693+
694+
{
695+
"com.example.foo": "bar",
696+
"com.example.baz": "@metadata.json"
697+
}
698+
699+
The values of this field support stamp variables.""",
700+
allow_files = True,
701+
),
681702
"launcher": attr.label(
682703
allow_single_file = True,
683704
doc = """If present, prefix the image's ENTRYPOINT with this file.

0 commit comments

Comments
 (0)