cilium
/
Makefile.docker
155 строк · 7.1 Кб
1# Copyright Authors of Cilium
2# SPDX-License-Identifier: Apache-2.0
3
4DOCKER_BUILDER := default
5
6# Export with value expected by docker
7export DOCKER_BUILDKIT=1
8
9# Docker Buildx support. If ARCH is defined, a builder instance 'cross'
10# on the local node is configured for amd64 and arm64 platform targets.
11# Otherwise build on the current (typically default) builder for the host
12# platform only.
13ifdef ARCH
14# Default to multi-arch builds, always create the builder for all the platforms we support
15DOCKER_PLATFORMS := linux/arm64,linux/amd64
16DOCKER_BUILDER := $(shell docker buildx ls | grep -E -e "[a-zA-Z0-9-]+ \*" | cut -d ' ' -f1)
17ifneq (,$(filter $(DOCKER_BUILDER),default desktop-linux))
18DOCKER_BUILDKIT_DRIVER :=
19ifdef DOCKER_BUILDKIT_IMAGE
20DOCKER_BUILDKIT_DRIVER := --driver docker-container --driver-opt image=$(DOCKER_BUILDKIT_IMAGE)
21endif
22BUILDER_SETUP := $(shell docker buildx create --platform $(DOCKER_PLATFORMS) $(DOCKER_BUILDKIT_DRIVER) --use)
23endif
24# Override default for a single platform
25ifneq ($(ARCH),multi)
26DOCKER_PLATFORMS := linux/$(ARCH)
27endif
28DOCKER_FLAGS += --push --platform $(DOCKER_PLATFORMS)
29else
30ifeq ($(findstring --output,$(DOCKER_FLAGS)),)
31ifeq ($(findstring --push,$(DOCKER_FLAGS)),)
32# ARCH, --output, and --push are not specified, build for the host platform without pushing, mimicking regular docker build
33DOCKER_FLAGS += --load
34endif
35endif
36endif
37DOCKER_BUILDER := $(shell docker buildx ls | grep -E -e "[a-zA-Z0-9-]+ \*" | cut -d ' ' -f1)
38
39##@ Docker Images
40.PHONY: builder-info
41builder-info: ## Print information about the docker builder that will be used for building images.
42@echo "Using Docker Buildx builder \"$(DOCKER_BUILDER)\" with build flags \"$(DOCKER_FLAGS)\"."
43
44# Generic rule for augmented .dockerignore files.
45GIT_IGNORE_FILES := $(shell find . -not -path "./vendor*" -name .gitignore -print)
46.PRECIOUS: %.dockerignore
47%.dockerignore: $(GIT_IGNORE_FILES) Makefile.docker
48@-mkdir -p $(dir $@)
49@echo "/hack" > $@
50@echo ".git" >> $@
51@echo "/Makefile.docker" >> $@
52@echo $(dir $(GIT_IGNORE_FILES)) | tr ' ' '\n' | xargs -P1 -I {DIR} -n1 sed \
53-e '# Remove lines with white space, comments and files that must be passed to docker, "$$" due to make. #' \
54-e '/^[[:space:]]*$$/d' -e '/^#/d' -e '/GIT_VERSION/d' \
55-e '# Apply pattern in all directories if it contains no "/", keep "!" up front. #' \
56-e '/^[^!/][^/]*$$/s<^<**/<' -e '/^![^/]*$$/s<^!<!**/<' \
57-e '# Prepend with the directory name, keep "!" up front. #' \
58-e '/^[^!]/s<^<{DIR}<' -e '/^!/s<^!<!{DIR}<'\
59-e '# Remove leading "./", keep "!" up front. #' \
60-e 's<^\./<<' -e 's<^!\./<!<' \
61-e '# Append newline to the last line if missing. GNU sed does not do this automatically. #' \
62-e '$$a\' \
63{DIR}.gitignore >> $@
64
65DOCKER_REGISTRY ?= quay.io
66ifeq ($(findstring /,$(DOCKER_DEV_ACCOUNT)),/)
67# DOCKER_DEV_ACCOUNT already contains '/', assume it specifies a registry
68IMAGE_REPOSITORY := $(DOCKER_DEV_ACCOUNT)
69else
70IMAGE_REPOSITORY := $(DOCKER_REGISTRY)/$(DOCKER_DEV_ACCOUNT)
71endif
72
73#
74# Template for Docker images. Paramaters are:
75# $(1) image target name
76# $(2) Dockerfile path
77# $(3) image name stem (e.g., cilium, cilium-operator, etc)
78# $(4) image tag
79# $(5) target
80#
81define DOCKER_IMAGE_TEMPLATE
82.PHONY: $(1)
83$(1): GIT_VERSION $(2) $(2).dockerignore GIT_VERSION builder-info
84$(ECHO_DOCKER)$(2) $(IMAGE_REPOSITORY)/$(IMAGE_NAME)$${UNSTRIPPED}:$(4)
85$(eval IMAGE_NAME := $(subst %,$$$$*,$(3)))
86ifeq ($(5),debug)
87@export NOSTRIP=1
88endif
89$(QUIET) $(CONTAINER_ENGINE) buildx build -f $(subst %,$$*,$(2)) \
90$(DOCKER_BUILD_FLAGS) $(DOCKER_FLAGS) \
91$(if $(BASE_IMAGE),--build-arg BASE_IMAGE=$(BASE_IMAGE),) \
92--build-arg NOSTRIP=$${NOSTRIP} \
93--build-arg NOOPT=${NOOPT} \
94--build-arg LOCKDEBUG=${LOCKDEBUG} \
95--build-arg RACE=${RACE}\
96--build-arg V=${V} \
97--build-arg LIBNETWORK_PLUGIN=${LIBNETWORK_PLUGIN} \
98--build-arg CILIUM_SHA=$(firstword $(GIT_VERSION)) \
99--build-arg OPERATOR_VARIANT=$(IMAGE_NAME) \
100--build-arg DEBUG_HOLD=$(DEBUG_HOLD) \
101--target $(5) \
102-t $(IMAGE_REPOSITORY)/$(IMAGE_NAME)$${UNSTRIPPED}$(DOCKER_IMAGE_SUFFIX):$(4) .
103ifneq ($(KIND_LOAD),)
104sleep 1
105kind load docker-image $(IMAGE_REPOSITORY)/$(IMAGE_NAME)$${UNSTRIPPED}$(DOCKER_IMAGE_SUFFIX):$(4)
106else
107ifeq ($(findstring --push,$(DOCKER_FLAGS)),)
108@echo 'Define "DOCKER_FLAGS=--push" to push the build results.'
109else
110$(CONTAINER_ENGINE) buildx imagetools inspect $(IMAGE_REPOSITORY)/$(IMAGE_NAME)$${UNSTRIPPED}$(DOCKER_IMAGE_SUFFIX):$(4)
111@echo '^^^ Images pushed, multi-arch manifest should be above. ^^^'
112endif
113endif
114
115$(1)-unstripped: NOSTRIP=1
116$(1)-unstripped: UNSTRIPPED=-unstripped
117$(1)-unstripped: $(1)
118@echo
119endef
120
121# docker-cilium-image
122$(eval $(call DOCKER_IMAGE_TEMPLATE,docker-cilium-image,images/cilium/Dockerfile,cilium,$(DOCKER_IMAGE_TAG),release))
123
124# dev-docker-image
125$(eval $(call DOCKER_IMAGE_TEMPLATE,dev-docker-image,images/cilium/Dockerfile,cilium-dev,$(DOCKER_IMAGE_TAG),release))
126
127# dev-docker-image-debug
128$(eval $(call DOCKER_IMAGE_TEMPLATE,dev-docker-image-debug,images/cilium/Dockerfile,cilium-dev,$(DOCKER_IMAGE_TAG),debug))
129
130# docker-plugin-image
131$(eval $(call DOCKER_IMAGE_TEMPLATE,docker-plugin-image,images/cilium-docker-plugin/Dockerfile,docker-plugin,$(DOCKER_IMAGE_TAG),release))
132
133# docker-hubble-relay-image
134$(eval $(call DOCKER_IMAGE_TEMPLATE,docker-hubble-relay-image,images/hubble-relay/Dockerfile,hubble-relay,$(DOCKER_IMAGE_TAG),release))
135
136# docker-clustermesh-apiserver-image
137$(eval $(call DOCKER_IMAGE_TEMPLATE,docker-clustermesh-apiserver-image,images/clustermesh-apiserver/Dockerfile,clustermesh-apiserver,$(DOCKER_IMAGE_TAG),release))
138
139# docker-operator-images.
140# We eat the ending of "operator" in to the stem ('%') to allow this pattern
141# to build also 'docker-operator-image', where the stem would be empty otherwise.
142$(eval $(call DOCKER_IMAGE_TEMPLATE,docker-opera%-image,images/operator/Dockerfile,opera%,$(DOCKER_IMAGE_TAG),release))
143$(eval $(call DOCKER_IMAGE_TEMPLATE,dev-docker-opera%-image,images/operator/Dockerfile,opera%,$(DOCKER_IMAGE_TAG),release))
144$(eval $(call DOCKER_IMAGE_TEMPLATE,dev-docker-opera%-image-debug,images/operator/Dockerfile,opera%,$(DOCKER_IMAGE_TAG),debug))
145
146#
147# docker-*-all targets are mainly used from the CI
148#
149docker-images-all: docker-cilium-image docker-plugin-image docker-hubble-relay-image docker-clustermesh-apiserver-image docker-operator-images-all ## Build all Cilium related docker images.
150
151docker-images-all-unstripped: docker-cilium-image-unstripped docker-plugin-image-unstripped docker-hubble-relay-image-unstripped docker-clustermesh-apiserver-image-unstripped docker-operator-images-all-unstripped ## Build all Cilium related unstripped docker images.
152
153docker-operator-images-all: docker-operator-image docker-operator-aws-image docker-operator-azure-image docker-operator-alibabacloud-image docker-operator-generic-image ## Build all variants of cilium-operator images.
154
155docker-operator-images-all-unstripped: docker-operator-image-unstripped docker-operator-aws-image-unstripped docker-operator-azure-image-unstripped docker-operator-alibabacloud-image-unstripped docker-operator-generic-image-unstripped ## Build all variants of unstripped cilium-operator images.
156