Skip to content

Commit

Permalink
feat(*_image): allow setting env vars
Browse files Browse the repository at this point in the history
Introduce an `env` parameter in every `*_image` rule, which is a dict
whose keys and values will become environment variables in the resulting
Docker image.

Apply consistent formatting to every rule's docstring (args in the
docstring are now listed in the same order as in the method declaration)
as well as the container structure tests (`metadataTest` keys are now
sorted alphabetically).
  • Loading branch information
sxlijin committed Mar 11, 2022
1 parent a17900c commit 25ead45
Show file tree
Hide file tree
Showing 34 changed files with 131 additions and 58 deletions.
6 changes: 4 additions & 2 deletions cc/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,17 @@ DEFAULT_BASE = select({
"//conditions:default": "@cc_image_base//image",
})

def cc_image(name, base = None, deps = [], layers = [], binary = None, **kwargs):
def cc_image(name, base = None, deps = [], layers = [], env = {}, binary = None, **kwargs):
"""Constructs a container image wrapping a cc_binary target.
Args:
name: Name of the cc_image target.
base: Base image to use for the cc_image.
deps: Dependencies of the cc_image.
binary: An alternative binary target to use instead of generating one.
layers: Augments "deps" with dependencies that should be put into
their own layers.
env: Environment variables for the cc_image.
binary: An alternative binary target to use instead of generating one.
**kwargs: See cc_binary.
"""
if layers:
Expand All @@ -94,6 +95,7 @@ def cc_image(name, base = None, deps = [], layers = [], binary = None, **kwargs)
app_layer(
name = name,
base = base,
env = env,
binary = binary,
visibility = visibility,
tags = tags,
Expand Down
6 changes: 4 additions & 2 deletions d/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ def repositories():
"""
_repositories()

def d_image(name, base = None, deps = [], layers = [], binary = None, **kwargs):
def d_image(name, base = None, deps = [], layers = [], env = {}, binary = None, **kwargs):
"""Constructs a container image wrapping a d_binary target.
Args:
name: Name of the d_image target.
base: Base image to use for the d_image.
deps: Dependencies of the d_image target.
binary: An alternative binary target to use instead of generating one.
layers: Augments "deps" with dependencies that should be put into
their own layers.
env: Environment variables for the d_image.
binary: An alternative binary target to use instead of generating one.
**kwargs: See d_binary.
"""
if layers:
Expand All @@ -63,6 +64,7 @@ def d_image(name, base = None, deps = [], layers = [], binary = None, **kwargs):
app_layer(
name = name,
base = base,
env = env,
binary = binary,
visibility = visibility,
tags = tags,
Expand Down
6 changes: 4 additions & 2 deletions go/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,16 @@ STATIC_DEFAULT_BASE = select({
"//conditions:default": "@go_image_static//image",
})

def go_image(name, base = None, deps = [], layers = [], binary = None, **kwargs):
def go_image(name, base = None, deps = [], layers = [], env = {}, binary = None, **kwargs):
"""Constructs a container image wrapping a go_binary target.
Args:
name: Name of the go_image target.
base: Base image to use to build the go_image.
deps: Dependencies of the go image target.
binary: An alternative binary target to use instead of generating one.
layers: Augments "deps" with dependencies that should be put into their own layers.
env: Environment variables for the go_image.
binary: An alternative binary target to use instead of generating one.
**kwargs: See go_binary.
"""
if layers:
Expand All @@ -123,6 +124,7 @@ def go_image(name, base = None, deps = [], layers = [], binary = None, **kwargs)
app_layer(
name = name,
base = base,
env = env,
binary = binary,
visibility = visibility,
tags = tags,
Expand Down
5 changes: 4 additions & 1 deletion groovy/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def groovy_image(
srcs = [],
deps = [],
layers = [],
env = {},
jvm_flags = [],
classpath_as_file = None,
**kwargs):
Expand All @@ -44,9 +45,10 @@ def groovy_image(
srcs: List of groovy source files that will be used to build the binary
to be included in the groovy_image.
deps: The dependencies of the groovy_image target.
jvm_flags: The flags to pass to the JVM when running the groovy image.
layers: Augments "deps" with dependencies that should be put into
their own layers.
env: Environment variables for the groovy_image.
jvm_flags: The flags to pass to the JVM when running the groovy image.
**kwargs: See groovy_binary.
"""
binary_name = name + ".binary"
Expand Down Expand Up @@ -84,6 +86,7 @@ def groovy_image(
jar_app_layer(
name = name,
base = base,
env = env,
binary = binary_name,
main_class = main_class,
jvm_flags = jvm_flags,
Expand Down
16 changes: 12 additions & 4 deletions java/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ jar_dep_layer = rule(
def _jar_app_layer_impl(ctx):
"""Appends the app layer with all remaining runfiles."""

for k in ctx.attr.env:
print("jar_app_layer", k, ctx.attr.env[k])

# layers don't include runfiles
available = depset(transitive = [java_files(jar) for jar in ctx.attr.jar_layers])

Expand Down Expand Up @@ -214,9 +217,9 @@ def _jar_app_layer_impl(ctx):
ctx,
# We use all absolute paths.
directory = "/",
env = {
env = dicts.add({
"JAVA_RUNFILES": "/app",
},
}, ctx.attr.env),
file_map = file_map,
entrypoint = entrypoint,
)
Expand Down Expand Up @@ -266,6 +269,7 @@ def java_image(
deps = [],
runtime_deps = [],
layers = [],
env = {},
jvm_flags = [],
classpath_as_file = None,
**kwargs):
Expand All @@ -276,9 +280,10 @@ def java_image(
base: Base image to use for the java image.
deps: Dependencies of the java image rule.
runtime_deps: Runtime dependencies of the java image.
jvm_flags: Flags to pass to the JVM when running the java image.
layers: Augments "deps" with dependencies that should be put into
their own layers.
env: Environment variables for the java_image.
jvm_flags: Flags to pass to the JVM when running the java image.
main_class: This parameter is optional. If provided it will be used in the
compilation of any additional sources, and as part of the
construction of the container entrypoint. If not provided, the
Expand Down Expand Up @@ -327,6 +332,7 @@ def java_image(
jar_layers = layers,
visibility = visibility,
tags = tags,
env = env,
args = kwargs.get("args"),
data = kwargs.get("data"),
testonly = kwargs.get("testonly"),
Expand Down Expand Up @@ -406,7 +412,7 @@ _war_app_layer = rule(
cfg = _container.image.cfg,
)

def war_image(name, base = None, deps = [], layers = [], **kwargs):
def war_image(name, base = None, deps = [], layers = [], env = {}, **kwargs):
"""Builds a container image overlaying the java_library as an exploded WAR.
TODO(mattmoor): For `bazel run` of this to be useful, we need to be able
Expand All @@ -419,6 +425,7 @@ def war_image(name, base = None, deps = [], layers = [], **kwargs):
deps: Dependencies of the way image target.
layers: Augments "deps" with dependencies that should be put into
their own layers.
env: Environment variables for the war_image.
**kwargs: See java_library.
"""
library_name = name + ".library"
Expand All @@ -438,6 +445,7 @@ def war_image(name, base = None, deps = [], layers = [], **kwargs):
base = base,
library = library_name,
jar_layers = layers,
env = env,
visibility = visibility,
tags = tags,
)
27 changes: 15 additions & 12 deletions kotlin/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,25 @@ def kt_jvm_image(
srcs = [],
deps = [],
layers = [],
env = {},
jvm_flags = [],
classpath_as_file = None,
**kwargs):
"""Builds a container image overlaying the kt_jvm_binary.
Args:
name: Name of the kt_jvm_image target.
base: Base image to use for the kt_jvm_image.
main_class: The main entrypoint class in the kotlin image.
srcs: List of kotlin source files that will be used to build the binary
to be included in the kt_jvm_image.
deps: The dependencies of the kt_jvm_image target.
jvm_flags: The flags to pass to the JVM when running the kotlin image.
layers: Augments "deps" with dependencies that should be put into
their own layers.
**kwargs: See kt_jvm_binary.
"""
Args:
name: Name of the kt_jvm_image target.
base: Base image to use for the kt_jvm_image.
main_class: The main entrypoint class in the kotlin image.
srcs: List of kotlin source files that will be used to build the binary
to be included in the kt_jvm_image.
deps: The dependencies of the kt_jvm_image target.
layers: Augments "deps" with dependencies that should be put into
their own layers.
env: Environment variables for the kt_jvm_image.
jvm_flags: The flags to pass to the JVM when running the kotlin image.
**kwargs: See kt_jvm_binary.
"""
binary_name = name + ".binary"

# This is an inlined copy of kt_jvm_binary so that we properly collect
Expand Down Expand Up @@ -83,6 +85,7 @@ def kt_jvm_image(
jar_app_layer(
name = name,
base = base,
env = env,
binary = binary_name,
main_class = main_class,
jvm_flags = jvm_flags,
Expand Down
3 changes: 3 additions & 0 deletions nodejs/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def nodejs_image(
base = None,
data = [],
layers = [],
env = {},
binary = None,
launcher = None,
launcher_args = None,
Expand All @@ -130,6 +131,7 @@ def nodejs_image(
data: Runtime dependencies of the nodejs_image.
layers: Augments "deps" with dependencies that should be put into
their own layers.
env: Environment variables for the nodejs_image.
binary: An alternative binary target to use instead of generating one.
launcher: The container_image launcher to set.
launcher_args: The args for the container_image launcher.
Expand Down Expand Up @@ -172,6 +174,7 @@ def nodejs_image(
app_layer(
name = name,
base = npm_deps_layer_name,
env = env,
binary = binary,
visibility = visibility,
tags = tags,
Expand Down
20 changes: 11 additions & 9 deletions python/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,18 @@ def py_layer(name, deps, filter = "", **kwargs):
native.py_library(name = binary_name, deps = deps, **kwargs)
filter_layer(name = name, dep = binary_name, filter = filter)

def py_image(name, base = None, deps = [], layers = [], **kwargs):
def py_image(name, base = None, deps = [], layers = [], env = {}, **kwargs):
"""Constructs a container image wrapping a py_binary target.
Args:
name: Name of the py_image target.
base: Base image to use in the py_image.
deps: Dependencies of the py_image target.
layers: Augments "deps" with dependencies that should be put into
their own layers.
**kwargs: See py_binary.
"""
Args:
name: Name of the py_image target.
base: Base image to use in the py_image.
deps: Dependencies of the py_image target.
layers: Augments "deps" with dependencies that should be put into
their own layers.
env: Environment variables for the py_image.
**kwargs: See py_binary.
"""
binary_name = name + ".binary"

if "main" not in kwargs:
Expand Down Expand Up @@ -117,6 +118,7 @@ def py_image(name, base = None, deps = [], layers = [], **kwargs):
name = name,
base = base,
entrypoint = ["/usr/bin/python"],
env = env,
binary = binary_name,
visibility = visibility,
tags = tags,
Expand Down
4 changes: 3 additions & 1 deletion python3/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ DEFAULT_BASE = select({
"//conditions:default": "@py3_image_base//image",
})

def py3_image(name, base = None, deps = [], layers = [], **kwargs):
def py3_image(name, base = None, deps = [], layers = [], env = {}, **kwargs):
"""Constructs a container image wrapping a py_binary target.
Args:
Expand All @@ -82,6 +82,7 @@ def py3_image(name, base = None, deps = [], layers = [], **kwargs):
deps: Dependencies of the py3_image.
layers: Augments "deps" with dependencies that should be put into
their own layers.
env: Environment variables for the py_image.
**kwargs: See py_binary.
"""
binary_name = name + ".binary"
Expand Down Expand Up @@ -113,6 +114,7 @@ def py3_image(name, base = None, deps = [], layers = [], **kwargs):
name = name,
base = base,
entrypoint = ["/usr/bin/python"],
env = env,
binary = binary_name,
visibility = visibility,
tags = tags,
Expand Down
18 changes: 10 additions & 8 deletions rust/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ def repositories():
"""
_repositories()

def rust_image(name, base = None, deps = [], layers = [], binary = None, **kwargs):
def rust_image(name, base = None, deps = [], layers = [], env = {}, binary = None, **kwargs):
"""Constructs a container image wrapping a rust_binary target.
Args:
name: Name of the rust_image target.
base: Base image to use for the rust_image.
deps: Dependencies of the rust_image target.
layers: Augments "deps" with dependencies that should be put into their own layers.
binary: An alternative binary target to use instead of generating one.
**kwargs: See rust_binary.
Args:
name: Name of the rust_image target.
base: Base image to use for the rust_image.
deps: Dependencies of the rust_image target.
layers: Augments "deps" with dependencies that should be put into their own layers.
env: Environment variables for the rust_image.
binary: An alternative binary target to use instead of generating one.
**kwargs: See rust_binary.
"""
if layers:
# buildifier: disable=print
Expand All @@ -63,6 +64,7 @@ def rust_image(name, base = None, deps = [], layers = [], binary = None, **kwarg
app_layer(
name = name,
base = base,
env = env,
binary = binary,
visibility = visibility,
tags = tags,
Expand Down
5 changes: 4 additions & 1 deletion scala/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def scala_image(
deps = [],
runtime_deps = [],
layers = [],
env = {},
jvm_flags = [],
classpath_as_file = None,
**kwargs):
Expand All @@ -44,9 +45,10 @@ def scala_image(
image.
deps: Dependencies of the scala image target.
runtime_deps: Runtime dependencies of the scala image.
jvm_flags: Flags to pass to the JVM when running the scala image.
layers: Augments "deps" with dependencies that should be put into
their own layers.
env: Environment variables for the scala_image.
jvm_flags: Flags to pass to the JVM when running the scala image.
**kwargs: See scala_binary.
"""
binary_name = name + ".binary"
Expand Down Expand Up @@ -81,6 +83,7 @@ def scala_image(
deps = deps,
runtime_deps = runtime_deps,
jar_layers = layers,
env = env,
visibility = visibility,
tags = tags,
args = kwargs.get("args"),
Expand Down
1 change: 1 addition & 0 deletions testdata/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,7 @@ java_image(
war_image(
name = "war_image",
srcs = ["Servlet.java"],
env = {"WAR_IMAGE_TEST_KEY": "war_image_test_value"},
layers = [
":java_image_library",
"@javax_servlet_api//jar:jar",
Expand Down
1 change: 1 addition & 0 deletions testdata/Servlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ public class Servlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
out.println(Library.SayHello());
out.printf("WAR_IMAGE_TEST_KEY=%s\n", System.getenv("WAR_IMAGE_TEST_KEY"));
}
}
Loading

0 comments on commit 25ead45

Please sign in to comment.