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 Jan 12, 2023
1 parent 7d34678 commit eb56d8e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 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
labelFilesArray 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(&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.")
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 24 additions & 6 deletions container/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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, {}),
Expand All @@ -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)
Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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(
Expand All @@ -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.
Expand Down Expand Up @@ -1006,7 +1024,7 @@ def container_image(**kwargs):
)

reserved_attrs = [
"label_files",
"label_file_deps",
"label_file_strings",
"null_cmd",
"null_entrypoint",
Expand All @@ -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 "",
Expand Down

0 comments on commit eb56d8e

Please sign in to comment.