container.bzl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Copyright 2019 The Kubernetes Authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. load("@io_bazel_rules_docker//container:container.bzl", "container_bundle", "container_image")
  15. load("@io_bazel_rules_docker//contrib:push-all.bzl", "docker_push")
  16. load("//build:platforms.bzl", "go_platform_constraint")
  17. # multi_arch_container produces a private internal container_image, multiple
  18. # arch-specific tagged container_bundles (named NAME-ARCH), an alias
  19. # from NAME to the appropriately NAME-ARCH container_bundle target, and a
  20. # genrule for NAME.tar copying the appropriate NAME-ARCH container bundle
  21. # tarball output for the currently-configured architecture.
  22. # Additionally, if docker_push_tags is provided, uses multi_arch_container_push
  23. # to create container_bundles named push-NAME-ARCH with the provided push tags,
  24. # along with a push-NAME docker_push target.
  25. # Args:
  26. # name: name used for the alias; the internal container_image and
  27. # container_bundles are based on this name
  28. # architectures: list of architectures (in GOARCH naming parlance) to
  29. # configure
  30. # base: base image to use for the containers. The format string {ARCH} will
  31. # be replaced with the configured GOARCH.
  32. # docker_tags: list of docker tags to apply to the image. The format string
  33. # {ARCH} will be replaced with the configured GOARCH; any stamping variables
  34. # should be escaped, e.g. {{STABLE_MY_VAR}}.
  35. # docker_push_tags: list of docker tags to apply to the image for pushing.
  36. # The format string {ARCH} will be replaced with the configured GOARCH;
  37. # any stamping variables should be escaped, e.g. {{STABLE_MY_VAR}}.
  38. # tags: will be applied to all targets
  39. # visiblity: will be applied only to the container_bundles; the internal
  40. # container_image is private
  41. # All other args will be applied to the internal container_image.
  42. def multi_arch_container(
  43. name,
  44. architectures,
  45. base,
  46. docker_tags,
  47. docker_push_tags = None,
  48. tags = None,
  49. visibility = None,
  50. **kwargs):
  51. container_image(
  52. name = "%s-internal" % name,
  53. base = select({
  54. go_platform_constraint(os = "linux", arch = arch): base.format(ARCH = arch)
  55. for arch in architectures
  56. }),
  57. tags = tags,
  58. visibility = ["//visibility:private"],
  59. **kwargs
  60. )
  61. for arch in architectures:
  62. container_bundle(
  63. name = "%s-%s" % (name, arch),
  64. images = {
  65. docker_tag.format(ARCH = arch): ":%s-internal" % name
  66. for docker_tag in docker_tags
  67. },
  68. tags = tags,
  69. visibility = visibility,
  70. )
  71. native.alias(
  72. name = name,
  73. actual = select({
  74. go_platform_constraint(os = "linux", arch = arch): "%s-%s" % (name, arch)
  75. for arch in architectures
  76. }),
  77. )
  78. native.genrule(
  79. name = "gen_%s.tar" % name,
  80. outs = ["%s.tar" % name],
  81. srcs = select({
  82. go_platform_constraint(os = "linux", arch = arch): ["%s-%s.tar" % (name, arch)]
  83. for arch in architectures
  84. }),
  85. cmd = "cp $< $@",
  86. output_to_bindir = True,
  87. )
  88. if docker_push_tags:
  89. multi_arch_container_push(
  90. name = name,
  91. architectures = architectures,
  92. docker_tags_images = {docker_push_tag: ":%s-internal" % name for docker_push_tag in docker_push_tags},
  93. tags = tags,
  94. )
  95. # multi_arch_container_push creates container_bundles named push-NAME-ARCH for
  96. # the provided architectures, populating them with the images directory.
  97. # It additionally creates a push-NAME docker_push rule which can be run to
  98. # push the images to a Docker repository.
  99. # Args:
  100. # name: name used for targets created by this macro; the internal
  101. # container_bundles are based on this name
  102. # architectures: list of architectures (in GOARCH naming parlance) to
  103. # configure
  104. # docker_tags_images: dictionary mapping docker tag to the corresponding
  105. # container_image target. The format string {ARCH} will be replaced
  106. # in tags with the configured GOARCH; any stamping variables should be
  107. # escaped, e.g. {{STABLE_MY_VAR}}.
  108. # tags: applied to container_bundle targets
  109. def multi_arch_container_push(
  110. name,
  111. architectures,
  112. docker_tags_images,
  113. tags = None):
  114. for arch in architectures:
  115. container_bundle(
  116. name = "push-%s-%s" % (name, arch),
  117. images = {tag.format(ARCH = arch): image for tag, image in docker_tags_images.items()},
  118. tags = tags,
  119. visibility = ["//visibility:private"],
  120. )
  121. docker_push(
  122. name = "push-%s" % name,
  123. bundle = select({
  124. go_platform_constraint(os = "linux", arch = arch): "push-%s-%s" % (name, arch)
  125. for arch in architectures
  126. }),
  127. )