Makefile 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # Copyright 2016 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. # Build the etcd image
  15. #
  16. # Usage:
  17. # [BUNDLED_ETCD_VERSIONS=3.0.17 3.1.12 3.2.24 3.3.17 3.4.3] [REGISTRY=k8s.gcr.io] [ARCH=amd64] [BASEIMAGE=busybox] make (build|push)
  18. #
  19. # The image contains different etcd versions to simplify
  20. # upgrades. Thus be careful when removing any versions from here.
  21. #
  22. # NOTE: The etcd upgrade rules are that you can upgrade only 1 minor
  23. # version at a time, and patch release don't matter.
  24. #
  25. # Except from etcd-$(version) and etcdctl-$(version) binaries, we also
  26. # need etcd and etcdctl binaries for backward compatibility reasons.
  27. # That binary will be set to the last version from $(BUNDLED_ETCD_VERSIONS).
  28. BUNDLED_ETCD_VERSIONS?=3.0.17 3.1.12 3.2.24 3.3.17 3.4.3
  29. # LATEST_ETCD_VERSION identifies the most recent etcd version available.
  30. LATEST_ETCD_VERSION?=3.4.3
  31. # REVISION provides a version number fo this image and all it's bundled
  32. # artifacts. It should start at zero for each LATEST_ETCD_VERSION and increment
  33. # for each revision of this image at that etcd version.
  34. REVISION?=0
  35. # IMAGE_TAG Uniquely identifies k8s.gcr.io/etcd docker image with a tag of the form "<etcd-version>-<revision>".
  36. IMAGE_TAG=$(LATEST_ETCD_VERSION)-$(REVISION)
  37. ARCH?=amd64
  38. ALL_ARCH = amd64 arm arm64 ppc64le s390x
  39. # Image should be pulled from k8s.gcr.io, which will auto-detect
  40. # region (us, eu, asia, ...) and pull from the closest.
  41. REGISTRY?=k8s.gcr.io
  42. # Images should be pushed to staging-k8s.gcr.io.
  43. PUSH_REGISTRY?=staging-k8s.gcr.io
  44. MANIFEST_IMAGE := $(PUSH_REGISTRY)/etcd
  45. # Install binaries matching base distro permissions
  46. BIN_INSTALL := install -m 0555
  47. # Hosts running SELinux need :z added to volume mounts
  48. SELINUX_ENABLED := $(shell cat /sys/fs/selinux/enforce 2> /dev/null || echo 0)
  49. ifeq ($(SELINUX_ENABLED),1)
  50. DOCKER_VOL_OPTS?=:z
  51. endif
  52. # This option is for running docker manifest command
  53. export DOCKER_CLI_EXPERIMENTAL := enabled
  54. # golang version should match the golang version from https://github.com/coreos/etcd/releases for the current ETCD_VERSION.
  55. GOLANG_VERSION?=1.12.9
  56. GOARM?=7
  57. TEMP_DIR:=$(shell mktemp -d)
  58. ifeq ($(ARCH),amd64)
  59. BASEIMAGE?=k8s.gcr.io/debian-base:v1.0.0
  60. endif
  61. ifeq ($(ARCH),arm)
  62. BASEIMAGE?=k8s.gcr.io/debian-base-arm:v1.0.0
  63. endif
  64. ifeq ($(ARCH),arm64)
  65. BASEIMAGE?=k8s.gcr.io/debian-base-arm64:v1.0.0
  66. endif
  67. ifeq ($(ARCH),ppc64le)
  68. BASEIMAGE?=k8s.gcr.io/debian-base-ppc64le:v1.0.0
  69. endif
  70. ifeq ($(ARCH),s390x)
  71. BASEIMAGE?=k8s.gcr.io/debian-base-s390x:v1.0.0
  72. endif
  73. build:
  74. # Explicitly copy files to the temp directory
  75. $(BIN_INSTALL) migrate-if-needed.sh $(TEMP_DIR)
  76. install Dockerfile $(TEMP_DIR)
  77. # Compile migrate
  78. migrate_tmp_dir=$(shell mktemp -d); \
  79. docker run --interactive -v $(shell pwd)/../../../:/go/src/k8s.io/kubernetes$(DOCKER_VOL_OPTS) -v $${migrate_tmp_dir}:/build$(DOCKER_VOL_OPTS) -e GOARCH=$(ARCH) golang:$(GOLANG_VERSION) \
  80. /bin/bash -c "CGO_ENABLED=0 go build -o /build/migrate k8s.io/kubernetes/cluster/images/etcd/migrate"; \
  81. $(BIN_INSTALL) $${migrate_tmp_dir}/migrate $(TEMP_DIR)
  82. ifeq ($(ARCH),amd64)
  83. # Do not compile if we should make an image for amd64, use the official etcd binaries instead
  84. # For each release create a tmp dir 'etcd_release_tmp_dir' and unpack the release tar there.
  85. for version in $(BUNDLED_ETCD_VERSIONS); do \
  86. etcd_release_tmp_dir=$(shell mktemp -d); \
  87. curl -sSL --retry 5 https://github.com/coreos/etcd/releases/download/v$$version/etcd-v$$version-linux-amd64.tar.gz | tar -xz -C $$etcd_release_tmp_dir --strip-components=1; \
  88. $(BIN_INSTALL) $$etcd_release_tmp_dir/etcd $$etcd_release_tmp_dir/etcdctl $(TEMP_DIR)/; \
  89. $(BIN_INSTALL) $(TEMP_DIR)/etcd $(TEMP_DIR)/etcd-$$version; \
  90. $(BIN_INSTALL) $(TEMP_DIR)/etcdctl $(TEMP_DIR)/etcdctl-$$version; \
  91. done
  92. else
  93. # Download etcd in a golang container and cross-compile it statically
  94. # For each release create a tmp dir 'etcd_release_tmp_dir' and unpack the release tar there.
  95. arch_prefix=""
  96. ifeq ($(ARCH),arm)
  97. arch_prefix="GOARM=$(GOARM)"
  98. endif
  99. # use '/go/src/go.etcd.io/etcd' to build etcd 3.4 and later.
  100. for version in $(BUNDLED_ETCD_VERSIONS); do \
  101. etcd_release_tmp_dir=$(shell mktemp -d); \
  102. etcd_build_dir="/go/src/github.com/coreos/etcd"; \
  103. if [ $$(echo $$version | cut -d. -f2) -gt 3 ]; then \
  104. etcd_build_dir="/go/src/go.etcd.io/etcd"; \
  105. fi; \
  106. docker run --interactive -v $${etcd_release_tmp_dir}:/etcdbin golang:$(GOLANG_VERSION)$(DOCKER_VOL_OPTS) /bin/bash -c \
  107. "git clone https://github.com/coreos/etcd $$etcd_build_dir \
  108. && cd $$etcd_build_dir \
  109. && git checkout v$${version} \
  110. && $(arch_prefix) GOARCH=$(ARCH) ./build \
  111. && cp -f bin/$(ARCH)/etcd* bin/etcd* /etcdbin; echo 'done'"; \
  112. $(BIN_INSTALL) $$etcd_release_tmp_dir/etcd $$etcd_release_tmp_dir/etcdctl $(TEMP_DIR)/; \
  113. $(BIN_INSTALL) $(TEMP_DIR)/etcd $(TEMP_DIR)/etcd-$$version; \
  114. $(BIN_INSTALL) $(TEMP_DIR)/etcdctl $(TEMP_DIR)/etcdctl-$$version; \
  115. done
  116. # Add this ENV variable in order to workaround an unsupported arch blocker
  117. # The multiarch feature is in an limited and experimental state right now, and etcd should work fine on arm64
  118. # On arm (which is 32-bit), it can't handle >1GB data in-memory, but it is very unlikely someone tinkering with their limited arm devices would reach such a high usage
  119. # ppc64le is still quite untested, but compiles and is probably in the process of being validated by IBM.
  120. cd $(TEMP_DIR) && echo "ENV ETCD_UNSUPPORTED_ARCH=$(ARCH)" >> Dockerfile
  121. endif
  122. # Replace BASEIMAGE with the real base image
  123. cd $(TEMP_DIR) && sed -i.bak 's|BASEIMAGE|$(BASEIMAGE)|g' Dockerfile
  124. # And build the image
  125. docker build --pull -t $(REGISTRY)/etcd-$(ARCH):$(IMAGE_TAG) $(TEMP_DIR)
  126. push: build
  127. docker tag $(REGISTRY)/etcd-$(ARCH):$(IMAGE_TAG) $(MANIFEST_IMAGE)-$(ARCH):$(IMAGE_TAG)
  128. docker push $(MANIFEST_IMAGE)-$(ARCH):$(IMAGE_TAG)
  129. sub-build-%:
  130. $(MAKE) ARCH=$* build
  131. all-build: $(addprefix sub-build-,$(ALL_ARCH))
  132. sub-push-image-%:
  133. $(MAKE) ARCH=$* push
  134. all-push-images: $(addprefix sub-push-image-,$(ALL_ARCH))
  135. all-push: all-push-images push-manifest
  136. push-manifest:
  137. docker manifest create --amend $(MANIFEST_IMAGE):$(IMAGE_TAG) $(shell echo $(ALL_ARCH) | sed -e "s~[^ ]*~$(MANIFEST_IMAGE)\-&:$(IMAGE_TAG)~g")
  138. @for arch in $(ALL_ARCH); do docker manifest annotate --arch $${arch} ${MANIFEST_IMAGE}:${IMAGE_TAG} ${MANIFEST_IMAGE}-$${arch}:${IMAGE_TAG}; done
  139. docker manifest push --purge ${MANIFEST_IMAGE}:${IMAGE_TAG}
  140. unit-test:
  141. docker run --interactive -v $(shell pwd)/../../../:/go/src/k8s.io/kubernetes$(DOCKER_VOL_OPTS) -e GOARCH=$(ARCH) golang:$(GOLANG_VERSION) \
  142. /bin/bash -c "CGO_ENABLED=0 go test -v k8s.io/kubernetes/cluster/images/etcd/migrate"
  143. # Integration tests require both a golang build environment and all the etcd binaries from a `k8s.gcr.io/etcd` image (`/usr/local/bin/etcd-<version>`, ...).
  144. # Since the `k8s.gcr.io/etcd` image is for runtime only and does not have a build golang environment, we create a new docker image to run integration tests
  145. # with.
  146. build-integration-test-image: build
  147. cp -r $(TEMP_DIR) $(TEMP_DIR)_integration_test
  148. cp Dockerfile $(TEMP_DIR)_integration_test/Dockerfile
  149. cd $(TEMP_DIR)_integration_test && sed -i.bak 's|BASEIMAGE|golang:$(GOLANG_VERSION)|g' Dockerfile
  150. docker build --pull -t etcd-integration-test $(TEMP_DIR)_integration_test
  151. integration-test:
  152. docker run --interactive -v $(shell pwd)/../../../:/go/src/k8s.io/kubernetes$(DOCKER_VOL_OPTS) -e GOARCH=$(ARCH) etcd-integration-test \
  153. /bin/bash -c "CGO_ENABLED=0 go test -tags=integration k8s.io/kubernetes/cluster/images/etcd/migrate -args -v 10 -logtostderr true"
  154. integration-build-test: build-integration-test-image integration-test
  155. test: unit-test integration-build-test
  156. all: all-build test
  157. .PHONY: build push push-manifest all-push all-push-images all-build unit-test build-integration-test-image integration-test integration-build-test test