github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/tools/images.mk (about)

     1  #!/usr/bin/make -f
     2  
     3  # Copyright 2018 The gVisor Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #   http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  ##
    18  ## Docker image targets.
    19  ##
    20  ##   Images used by the tests must also be built and available locally.
    21  ##   The canonical test targets defined below will automatically load
    22  ##   relevant images. These can be loaded or built manually via these
    23  ##   targets.
    24  ##
    25  ##   (*) Note that you may provide an ARCH parameter in order to build
    26  ##   and load images from an alternate archiecture (using qemu). When
    27  ##   bazel is run as a server, this has the effect of running an full
    28  ##   cross-architecture chain, and can produce cross-compiled binaries.
    29  ##
    30  
    31  # ARCH is the architecture used for the build. This may be overriden at the
    32  # command line in order to perform a cross-build (in a limited capacity).
    33  ARCH := $(shell uname -m)
    34  ifneq ($(ARCH),$(shell uname -m))
    35  DOCKER_PLATFORM_ARGS := --platform=$(ARCH)
    36  else
    37  DOCKER_PLATFORM_ARGS :=
    38  endif
    39  
    40  # Note that the image prefixes used here must match the image mangling in
    41  # runsc/testutil.MangleImage. Names are mangled in this way to ensure that all
    42  # tests are using locally-defined images (that are consistent and idempotent).
    43  REMOTE_IMAGE_PREFIX ?= gcr.io/gvisor-presubmit
    44  LOCAL_IMAGE_PREFIX ?= gvisor.dev/images
    45  ALL_IMAGES   := $(subst /,_,$(subst images/,,$(shell find images/ -name Dockerfile -o -name Dockerfile.$(ARCH) | xargs -n 1 dirname | uniq)))
    46  SUB_IMAGES   := $(foreach image,$(ALL_IMAGES),$(if $(findstring _,$(image)),$(image),))
    47  IMAGE_GROUPS := $(sort $(foreach image,$(SUB_IMAGES),$(firstword $(subst _, ,$(image)))))
    48  
    49  define expand_group =
    50  load-$(1): $$(patsubst $(1)_%, load-$(1)_%, $$(filter $(1)_%,$$(ALL_IMAGES)))
    51  	@
    52  .PHONY: load-$(1)
    53  push-$(1): $$(patsubst $(1)_%, push-$(1)_%, $$(filter $(1)_%,$$(ALL_IMAGES)))
    54  	@
    55  .PHONY: push-$(1)
    56  endef
    57  $(foreach group,$(IMAGE_GROUPS),$(eval $(call expand_group,$(group))))
    58  
    59  list-all-images: ## List all images.
    60  	@for image in $(ALL_IMAGES); do echo $${image}; done
    61  .PHONY: list-all-images
    62  
    63  load-all-images: ## Load all images.
    64  load-all-images: $(patsubst %,load-%,$(ALL_IMAGES))
    65  .PHONY: load-all-images
    66  
    67  push-all-images: ## Push all images.
    68  push-all-images: $(patsubst %,push-%,$(ALL_IMAGES))
    69  .PHONY: push-all-images
    70  
    71  # path and dockerfile are used to extract the relevant path and dockerfile
    72  # (depending on what's available for the given architecture).
    73  path = images/$(subst _,/,$(1))
    74  dockerfile = $$(if [ -f "$(call path,$(1))/Dockerfile.$(ARCH)" ]; then echo Dockerfile.$(ARCH); else echo Dockerfile; fi)
    75  
    76  # The tag construct is used to memoize the image generated (see README.md).
    77  # This scheme is used to enable aggressive caching in a central repository, but
    78  # ensuring that images will always be sourced using the local files.
    79  tag = $(shell cd images && find $(subst _,/,$(1)) -type f | sort | xargs -n 1 sha256sum | sha256sum - | cut -c 1-16)
    80  remote_image = $(REMOTE_IMAGE_PREFIX)/$(subst _,/,$(1))_$(ARCH)
    81  local_image = $(LOCAL_IMAGE_PREFIX)/$(subst _,/,$(1))
    82  
    83  # Include all existing images as targets here.
    84  #
    85  # Note that we use a _ for the tag separator, instead of :, as the latter is
    86  # interpreted by Make, unfortunately. tag_expand expands the generic rules to
    87  # tag-specific targets. These is needed to provide sensible targets for load
    88  # below, with caching. Basically, if there is a rule generated here, then the
    89  # load will be skipped. If there is no load generated here, then the default
    90  # rule for load will kick in.
    91  #
    92  # Note that if this rule does not successfully rule, we will simply have
    93  # additional Docker pull commands that run for all images that are already
    94  # pulled. No real harm done.
    95  EXISTING_IMAGES = $(shell docker images --format '{{.Repository}}_{{.Tag}}' | grep -v '<none>')
    96  define existing_image_rule =
    97  loaded0_$(1)=load-$$(1): tag-$$(1) # Already available.
    98  loaded1_$(1)=.PHONY: load-$$(1)
    99  endef
   100  $(foreach image, $(EXISTING_IMAGES), $(eval $(call existing_image_rule,$(image))))
   101  define tag_expand_rule =
   102  $(eval $(loaded0_$(call remote_image,$(1))_$(call tag,$(1))))
   103  $(eval $(loaded1_$(call remote_image,$(1))_$(call tag,$(1))))
   104  endef
   105  $(foreach image, $(ALL_IMAGES), $(eval $(call tag_expand_rule,$(image))))
   106  
   107  # tag tags a local image. This applies both the hash-based tag from above to
   108  # ensure that caching works as expected, as well as the "latest" tag that is
   109  # used by the tests.
   110  local_tag = \
   111    docker tag $(call remote_image,$(1)):$(call tag,$(1)) $(call local_image,$(1)):$(call tag,$(1)) >&2
   112  latest_tag = \
   113    docker tag $(call local_image,$(1)):$(call tag,$(1)) $(call local_image,$(1)) >&2
   114  tag-%: ## Tag a local image.
   115  	@$(call header,TAG $*)
   116  	@$(call local_tag,$*) && $(call latest_tag,$*)
   117  
   118  # pull forces the image to be pulled.
   119  pull = \
   120    $(call header,PULL $(1)) && \
   121    docker pull $(DOCKER_PLATFORM_ARGS) $(call remote_image,$(1)):$(call tag,$(1)) >&2 && \
   122    $(call local_tag,$(1)) && \
   123    $(call latest_tag,$(1))
   124  pull-%: register-cross ## Force a repull of the image.
   125  	@$(call pull,$*)
   126  
   127  # rebuild builds the image locally. Only the "remote" tag will be applied. Note
   128  # we need to explicitly repull the base layer in order to ensure that the
   129  # architecture is correct. Note that we use the term "rebuild" here to avoid
   130  # conflicting with the bazel "build" terminology, which is used elsewhere.
   131  rebuild = \
   132    $(call header,REBUILD $(1)) && \
   133    (T=$$(mktemp -d) && cp -a $(call path,$(1))/* $$T && \
   134    $(foreach image,$(shell grep FROM "$(call path,$(1))/$(call dockerfile,$(1))" 2>/dev/null | cut -d' ' -f2),docker pull $(DOCKER_PLATFORM_ARGS) $(image) >&2 &&) \
   135    docker build $(DOCKER_PLATFORM_ARGS) \
   136      -f "$$T/$(call dockerfile,$(1))" \
   137      -t "$(call remote_image,$(1)):$(call tag,$(1))" \
   138      $$T >&2 && \
   139    rm -rf $$T) && \
   140    $(call local_tag,$(1)) && \
   141    $(call latest_tag,$(1))
   142  rebuild-%: register-cross ## Force rebuild an image locally.
   143  	@$(call rebuild,$*)
   144  
   145  # load will either pull the "remote" or build it locally. This is the preferred
   146  # entrypoint, as it should never fail. The local tag should always be set after
   147  # this returns (either by the pull or the build).
   148  load-%: register-cross ## Pull or build an image locally.
   149  	@($(call pull,$*)) || ($(call rebuild,$*))
   150  
   151  # push pushes the remote image, after either pulling (to validate that the tag
   152  # already exists) or building manually. Note that this generic rule will match
   153  # the fully-expanded remote image tag.
   154  push-%: load-% ## Push a given image.
   155  	@docker push $(call remote_image,$*):$(call tag,$*) >&2
   156  
   157  # register-cross registers the necessary qemu binaries for cross-compilation.
   158  # This may be used by any target that may execute containers that are not the
   159  # native format. Note that this will only apply on the first execution.
   160  register-cross:
   161  ifneq ($(ARCH),$(shell uname -m))
   162  ifeq (,$(wildcard /proc/sys/fs/binfmt_misc/qemu-*))
   163  	@docker run --rm --privileged multiarch/qemu-user-static --reset --persistent yes >&2
   164  else
   165  	@
   166  endif
   167  else
   168  	@
   169  endif