Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow set container_image labels by json file #2211

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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