Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Commit

Permalink
Allow set container_image labels by json file
Browse files Browse the repository at this point in the history
  • Loading branch information
bozaro committed Jul 19, 2023
1 parent 8e70c6b commit 7a4e35a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
17 changes: 17 additions & 0 deletions container/go/cmd/create_image_config/create_image_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main

import (
"bytes"
"encoding/json"
"flag"
"io/ioutil"
"log"
Expand All @@ -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
Expand All @@ -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.")
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions container/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def _add_create_image_config_args(
manifest,
config,
labels,
labels_files,
label_files,
entrypoint,
cmd,
Expand Down Expand Up @@ -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, {}),
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -205,6 +211,7 @@ def _image_config(
manifest,
config,
labels_fixed,
labels_files,
label_files,
entrypoint,
cmd,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 7a4e35a

Please sign in to comment.