Skip to content

Commit

Permalink
Plumb os.version through container image config files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonjohnsonjr committed Feb 20, 2020
1 parent b7abf82 commit 73ffa7f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,13 @@ container_image(name, base, data_path, directory, files, legacy_repository_namin
<p>The desired CPU architecture to be used as label in the container image.</p>
</td>
</tr>
<tr>
<td><code>os_version</code></td>
<td>
<p><code>String; optional</code></p>
<p>The desired OS version to be used in the container image config.</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 @@ -41,6 +41,7 @@ var (
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.")
osVersion = flag.String("osVersion", "", "Operating system version to create docker image for (primarily for windows).")
labelsArray utils.ArrayStringFlags
ports utils.ArrayStringFlags
volumes utils.ArrayStringFlags
Expand Down Expand Up @@ -97,6 +98,7 @@ func main() {
NullCmd: *nullCmd,
Architecture: *architecture,
OperatingSystem: *operatingSystem,
OSVersion: *osVersion,
CreatedBy: "bazel build ...",
Author: "Bazel",
LabelsArray: labelsArray,
Expand Down
5 changes: 5 additions & 0 deletions container/go/pkg/compat/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ type OverrideConfigOpts struct {
Architecture string
// OperatingSystem is the operating system to creater docker image for.
OperatingSystem string
// OSVersion is the operating system version to creater docker image for.
OSVersion string
// CreatedBy is the command that generated the image. Default
// "bazel build ...".
CreatedBy string
Expand Down Expand Up @@ -443,6 +445,9 @@ func updateConfig(overrideInfo *OverrideConfigOpts) error {
overrideInfo.ConfigFile.Author = overrideInfo.Author
overrideInfo.ConfigFile.OS = overrideInfo.OperatingSystem
overrideInfo.ConfigFile.Architecture = overrideInfo.Architecture
if overrideInfo.OSVersion != "" {
overrideInfo.ConfigFile.OSVersion = overrideInfo.Stamper.Stamp(overrideInfo.OSVersion)
}

creationTime, err := getCreationTime(overrideInfo)
// creationTime is the RFC 3339 formatted time derived from createTime input.
Expand Down
14 changes: 13 additions & 1 deletion container/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ def _add_create_image_config_args(
base_config,
base_manifest,
architecture,
operating_system):
operating_system,
os_version):
"""
Add args for the create_image_config Go binary.
"""
Expand Down Expand Up @@ -166,6 +167,9 @@ def _add_create_image_config_args(
if operating_system:
args.add("-operatingSystem", operating_system)

if os_version:
args.add("-osVersion", os_version)

if ctx.attr.stamp:
stamp_inputs = [ctx.info_file, ctx.version_file]
args.add_all(stamp_inputs, before_each = "-stampInfoFile")
Expand All @@ -192,6 +196,7 @@ def _image_config(
base_manifest = None,
architecture = None,
operating_system = None,
os_version = None,
layer_name = None,
workdir = None,
null_entrypoint = False,
Expand Down Expand Up @@ -235,6 +240,7 @@ def _image_config(
base_manifest,
architecture,
operating_system,
os_version,
)

ctx.actions.run(
Expand Down Expand Up @@ -295,6 +301,7 @@ def _impl(
tars = None,
architecture = None,
operating_system = None,
os_version = None,
output_executable = None,
output_tarball = None,
output_config = None,
Expand Down Expand Up @@ -324,6 +331,7 @@ def _impl(
tars: File list, overrides ctx.files.tars
architecture: str, overrides ctx.attr.architecture
operating_system: Operating system to target (e.g. linux, windows)
os_version: Operating system version to target
output_executable: File to use as output for script to load docker image
output_tarball: File, overrides ctx.outputs.out
output_config: File, overrides ctx.outputs.config
Expand All @@ -338,6 +346,7 @@ def _impl(
cmd = cmd or ctx.attr.cmd
architecture = architecture or ctx.attr.architecture
operating_system = operating_system or ctx.attr.operating_system
os_version = os_version or ctx.attr.os_version
creation_time = creation_time or ctx.attr.creation_time
build_executable = output_executable or ctx.outputs.build_script
output_tarball = output_tarball or ctx.outputs.out
Expand Down Expand Up @@ -426,6 +435,7 @@ def _impl(
base_manifest = manifest_file,
architecture = architecture,
operating_system = operating_system,
os_version = os_version,
layer_name = str(i),
workdir = workdir or ctx.attr.workdir,
null_entrypoint = null_entrypoint,
Expand Down Expand Up @@ -551,6 +561,8 @@ _attrs = dicts.add(_layer.attrs, {
# We need these flags to distinguish them.
"null_cmd": attr.bool(default = False),
"null_entrypoint": attr.bool(default = False),
"operating_system": attr.string(default = "linux"),
"os_version": attr.string(),
"ports": attr.string_list(), # Skylark doesn't support int_list...
"repository": attr.string(default = "bazel"),
"stamp": attr.bool(default = False),
Expand Down
6 changes: 6 additions & 0 deletions tests/container/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -844,4 +844,10 @@ file_test(
file = "@pause_tar//image:001.tar.gz",
regexp = ".*",
)

file_test(
name = "windows_base_image_test_config",
file = ":basic_windows_image",
regexp = ".*\"os.version\":\"10.0.17134.165\",.*",
)
# END_DO_NOT_IMPORT

0 comments on commit 73ffa7f

Please sign in to comment.