Skip to content

Commit

Permalink
Expose architecture in container_image (#1427)
Browse files Browse the repository at this point in the history
  • Loading branch information
murilofv authored Feb 19, 2020
1 parent e3ccf50 commit b7abf82
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ platforms:
- "//docker/util:config_stripper_test"
- "//tests/container:alpine_custom_attr_digest_test"
- "//tests/container:alpine_linux_armv6_tar_test_image_tar"
- "//tests/container:architecture_test"
- "//tests/container:basic_windows_image_test"
- "//tests/container:build_tar_test"
- "//tests/container:distroless_fixed_id_digest_test"
Expand Down Expand Up @@ -82,6 +83,7 @@ platforms:
- "//docker/util:config_stripper_test"
- "//tests/container:alpine_custom_attr_digest_test"
- "//tests/container:alpine_linux_armv6_tar_test_image_tar"
- "//tests/container:architecture_test"
- "//tests/container:basic_windows_image_test"
- "//tests/container:build_tar_test"
- "//tests/container:distroless_fixed_id_digest_test"
Expand Down Expand Up @@ -155,6 +157,7 @@ platforms:
- "--"
- "//tests/container:alpine_custom_attr_digest_test"
- "//tests/container:alpine_linux_armv6_tar_test_image_tar"
- "//tests/container:architecture_test"
- "//tests/container:basic_windows_image_test"
- "//tests/container:build_tar_test"
- "//tests/container:distroless_fixed_id_digest_test"
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2118,6 +2118,13 @@ container_image(name, base, data_path, directory, files, legacy_repository_namin
<code>False</code>.</p>
</td>
</tr>
<tr>
<td><code>architecture</code></td>
<td>
<p><code>String; optional, default to amd64</code></p>
<p>The desired CPU architecture to be used as label in the container image.</p>
</td>
</tr>
</tbody>
</table>

Expand Down
2 changes: 2 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 @@ -39,6 +39,7 @@ var (
workdir = flag.String("workdir", "", "Set the working directory of the layer.")
nullEntryPoint = flag.Bool("nullEntryPoint", false, "If true, Entrypoint will be set to null.")
nullCmd = flag.Bool("nullCmd", false, "If true, Cmd will be set to null.")
architecture = flag.String("architecture", "amd64", "The architecture of the docker image.")
operatingSystem = flag.String("operatingSystem", "linux", "Operating system to create docker image for, eg. linux.")
labelsArray utils.ArrayStringFlags
ports utils.ArrayStringFlags
Expand Down Expand Up @@ -94,6 +95,7 @@ func main() {
Workdir: *workdir,
NullEntryPoint: *nullEntryPoint,
NullCmd: *nullCmd,
Architecture: *architecture,
OperatingSystem: *operatingSystem,
CreatedBy: "bazel build ...",
Author: "Bazel",
Expand Down
6 changes: 3 additions & 3 deletions container/go/pkg/compat/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ import (
)

const (
// defaultProcArch is the default architecture type based on legacy create_image_config.py.
defaultProcArch = "amd64"
// defaultTimeStamp is the unix epoch 0 time representation in 32 bits.
defaultTimestamp = "1970-01-01T00:00:00Z"
)
Expand All @@ -63,6 +61,8 @@ type OverrideConfigOpts struct {
// NullCmd indicates if there is a command.
// If true, Command will be set to null.
NullCmd bool
// Architecture is the architecture of the docker image
Architecture string
// OperatingSystem is the operating system to creater docker image for.
OperatingSystem string
// CreatedBy is the command that generated the image. Default
Expand Down Expand Up @@ -442,7 +442,7 @@ func updateConfigLayers(overrideInfo *OverrideConfigOpts, layerDigests []string,
func updateConfig(overrideInfo *OverrideConfigOpts) error {
overrideInfo.ConfigFile.Author = overrideInfo.Author
overrideInfo.ConfigFile.OS = overrideInfo.OperatingSystem
overrideInfo.ConfigFile.Architecture = defaultProcArch
overrideInfo.ConfigFile.Architecture = overrideInfo.Architecture

creationTime, err := getCreationTime(overrideInfo)
// creationTime is the RFC 3339 formatted time derived from createTime input.
Expand Down
11 changes: 11 additions & 0 deletions container/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def _add_create_image_config_args(
layer_names,
base_config,
base_manifest,
architecture,
operating_system):
"""
Add args for the create_image_config Go binary.
Expand Down Expand Up @@ -159,6 +160,9 @@ def _add_create_image_config_args(
args.add("-baseManifest", base_manifest)
inputs += [base_manifest]

if architecture:
args.add("-architecture", architecture)

if operating_system:
args.add("-operatingSystem", operating_system)

Expand Down Expand Up @@ -186,6 +190,7 @@ def _image_config(
env = None,
base_config = None,
base_manifest = None,
architecture = None,
operating_system = None,
layer_name = None,
workdir = None,
Expand Down Expand Up @@ -228,6 +233,7 @@ def _image_config(
layer_names,
base_config,
base_manifest,
architecture,
operating_system,
)

Expand Down Expand Up @@ -287,6 +293,7 @@ def _impl(
layers = None,
debs = None,
tars = None,
architecture = None,
operating_system = None,
output_executable = None,
output_tarball = None,
Expand Down Expand Up @@ -315,6 +322,7 @@ def _impl(
layers: label List, overrides ctx.attr.layers
debs: File list, overrides ctx.files.debs
tars: File list, overrides ctx.files.tars
architecture: str, overrides ctx.attr.architecture
operating_system: Operating system to target (e.g. linux, windows)
output_executable: File to use as output for script to load docker image
output_tarball: File, overrides ctx.outputs.out
Expand All @@ -328,6 +336,7 @@ def _impl(
name = name or ctx.label.name
entrypoint = entrypoint or ctx.attr.entrypoint
cmd = cmd or ctx.attr.cmd
architecture = architecture or ctx.attr.architecture
operating_system = operating_system or ctx.attr.operating_system
creation_time = creation_time or ctx.attr.creation_time
build_executable = output_executable or ctx.outputs.build_script
Expand Down Expand Up @@ -415,6 +424,7 @@ def _impl(
env = layer.env,
base_config = config_file,
base_manifest = manifest_file,
architecture = architecture,
operating_system = operating_system,
layer_name = str(i),
workdir = workdir or ctx.attr.workdir,
Expand Down Expand Up @@ -505,6 +515,7 @@ def _impl(
]

_attrs = dicts.add(_layer.attrs, {
"architecture": attr.string(default = "amd64"),
"base": attr.label(allow_files = container_filetype),
"cmd": attr.string_list(),
"create_image_config": attr.label(
Expand Down
18 changes: 18 additions & 0 deletions tests/container/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,24 @@ container_test(
driver = "tar",
image = ":new_alpine_linux_ppc64le_image_oci_go_join_layers",
)

# This test is used to check if an architecture label is set properly when set in container_image
# args are the architecture to be matched and the target
container_image(
name = "alpine_arch_ppc64le",
architecture = "ppc64le",
base = "@alpine_linux_ppc64le//image",
)

sh_test(
name = "architecture_test",
srcs = ["architecture_test.sh"],
args = [
"ppc64le",
"alpine_arch_ppc64le",
],
data = [":alpine_arch_ppc64le"],
)
# END_DO_NOT_IMPORT

# The following targets are not imported.
Expand Down
14 changes: 14 additions & 0 deletions tests/container/architecture_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

BASEDIR=$(dirname "$0")

# Expose content of config file, just for logging
cat ${BASEDIR}/${2}.0.config; echo

# Check if architecture passed in arg ${1} matches the config file
if grep -q \"architecture\":\"${1}\" ${BASEDIR}/${2}.0.config; then
echo "Architecture ${1} in arg matches the config file"
else
echo "Architecture ${1} in arg does not match the config file"
exit 1
fi

0 comments on commit b7abf82

Please sign in to comment.