Skip to content

Commit

Permalink
build: setup buildx
Browse files Browse the repository at this point in the history
Signed-off-by: Morlay <[email protected]>
  • Loading branch information
morlay committed Dec 1, 2020
1 parent 5c3b53b commit 66a4599
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 32 deletions.
45 changes: 32 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ JAEGER_IMPORT_PATH=github.com/jaegertracing/jaeger
STORAGE_PKGS = ./plugin/storage/integration/...
OTEL_COLLECTOR_DIR = ./cmd/opentelemetry

include docker/Makefile

# all .go files that are not auto-generated and should be auto-formatted and linted.
ALL_SRC := $(shell find . -name '*.go' \
-not -name 'doc.go' \
Expand Down Expand Up @@ -69,6 +67,18 @@ COLORIZE=$(SED) ''/PASS/s//$(COLOR_PASS)/'' | $(SED) ''/FAIL/s//$(COLOR_FAIL)/''
DOCKER_NAMESPACE?=jaegertracing
DOCKER_TAG?=latest

# list for multi-arch image publishing
TARGET_ARCHS ?= amd64 arm64

BUILDX_PUSH ?= false
DOCKER_BUILD ?= docker build --build-arg=TARGETARCH=$(GOARCH)
ifeq ($(BUILDX_PUSH),true)
DOCKER_BUILD = DOCKER_CLI_EXPERIMENTAL="enabled" docker buildx build --builder=jaeger-builder --push $(foreach arch,${TARGET_ARCHS},--platform=linux/${arch})
endif

# make sure variables could pass to includes
include docker/Makefile

MOCKERY=mockery

.DEFAULT_GOAL := test-and-lint
Expand Down Expand Up @@ -220,7 +230,7 @@ build-anonymizer:
.PHONY: docker-hotrod
docker-hotrod:
GOOS=linux $(MAKE) build-examples
docker build -t $(DOCKER_NAMESPACE)/example-hotrod:${DOCKER_TAG} ./examples/hotrod --build-arg TARGETARCH=$(GOARCH)
$(DOCKER_BUILD) -t $(DOCKER_NAMESPACE)/example-hotrod:${DOCKER_TAG} ./examples/hotrod

.PHONY: run-all-in-one
run-all-in-one: build-ui
Expand Down Expand Up @@ -289,6 +299,12 @@ docker: build-binaries-linux docker-images-only
build-binaries-linux:
GOOS=linux GOARCH=amd64 $(MAKE) build-platform-binaries

.PHONY: build-binaries-linux-archs
build-binaries-linux-archs:
for arch in $(TARGET_ARCHS) ; do \
GOOS=linux GOARCH=$${arch} $(MAKE) build-platform-binaries; \
done

.PHONY: build-binaries-windows
build-binaries-windows:
GOOS=windows GOARCH=amd64 $(MAKE) build-platform-binaries
Expand Down Expand Up @@ -332,13 +348,13 @@ build-all-platforms: build-binaries-linux build-binaries-windows build-binaries-

.PHONY: docker-images-cassandra
docker-images-cassandra:
docker build -t $(DOCKER_NAMESPACE)/jaeger-cassandra-schema:${DOCKER_TAG} plugin/storage/cassandra/
$(DOCKER_BUILD) -t $(DOCKER_NAMESPACE)/jaeger-cassandra-schema:${DOCKER_TAG} plugin/storage/cassandra/
@echo "Finished building jaeger-cassandra-schema =============="

.PHONY: docker-images-elastic
docker-images-elastic:
docker build -t $(DOCKER_NAMESPACE)/jaeger-es-index-cleaner:${DOCKER_TAG} plugin/storage/es
docker build -t $(DOCKER_NAMESPACE)/jaeger-es-rollover:${DOCKER_TAG} plugin/storage/es -f plugin/storage/es/Dockerfile.rollover
$(DOCKER_BUILD) -t $(DOCKER_NAMESPACE)/jaeger-es-index-cleaner:${DOCKER_TAG} plugin/storage/es
$(DOCKER_BUILD) -t $(DOCKER_NAMESPACE)/jaeger-es-rollover:${DOCKER_TAG} plugin/storage/es -f plugin/storage/es/Dockerfile.rollover
@echo "Finished building jaeger-es-indices-clean =============="

docker-images-jaeger-backend: TARGET = release
Expand All @@ -348,21 +364,20 @@ docker-images-jaeger-backend-debug: SUFFIX = -debug
.PHONY: docker-images-jaeger-backend docker-images-jaeger-backend-debug
docker-images-jaeger-backend docker-images-jaeger-backend-debug: create-baseimg create-debugimg
for component in agent collector query ingester ; do \
docker build --target $(TARGET) \
$(DOCKER_BUILD) --target $(TARGET) \
--tag $(DOCKER_NAMESPACE)/jaeger-$$component$(SUFFIX):${DOCKER_TAG} \
--build-arg base_image=$(BASE_IMAGE) \
--build-arg debug_image=$(DEBUG_IMAGE) \
--build-arg TARGETARCH=$(GOARCH) \
cmd/$$component ; \
echo "Finished building $$component ==============" ; \
done
docker build -t $(DOCKER_NAMESPACE)/jaeger-opentelemetry-collector:${DOCKER_TAG} -f ${OTEL_COLLECTOR_DIR}/cmd/collector/Dockerfile cmd/opentelemetry/cmd/collector --build-arg TARGETARCH=$(GOARCH)
docker build -t $(DOCKER_NAMESPACE)/jaeger-opentelemetry-agent:${DOCKER_TAG} -f ${OTEL_COLLECTOR_DIR}/cmd/agent/Dockerfile cmd/opentelemetry/cmd/agent --build-arg TARGETARCH=$(GOARCH)
docker build -t $(DOCKER_NAMESPACE)/jaeger-opentelemetry-ingester:${DOCKER_TAG} -f ${OTEL_COLLECTOR_DIR}/cmd/ingester/Dockerfile cmd/opentelemetry/cmd/ingester --build-arg TARGETARCH=$(GOARCH)
$(DOCKER_BUILD) -t $(DOCKER_NAMESPACE)/jaeger-opentelemetry-collector:${DOCKER_TAG} -f ${OTEL_COLLECTOR_DIR}/cmd/collector/Dockerfile cmd/opentelemetry/cmd/collector
$(DOCKER_BUILD) -t $(DOCKER_NAMESPACE)/jaeger-opentelemetry-agent:${DOCKER_TAG} -f ${OTEL_COLLECTOR_DIR}/cmd/agent/Dockerfile cmd/opentelemetry/cmd/agent
$(DOCKER_BUILD) -t $(DOCKER_NAMESPACE)/jaeger-opentelemetry-ingester:${DOCKER_TAG} -f ${OTEL_COLLECTOR_DIR}/cmd/ingester/Dockerfile cmd/opentelemetry/cmd/ingester

.PHONY: docker-images-tracegen
docker-images-tracegen:
docker build -t $(DOCKER_NAMESPACE)/jaeger-tracegen:${DOCKER_TAG} cmd/tracegen/ --build-arg TARGETARCH=$(GOARCH)
$(DOCKER_BUILD) -t $(DOCKER_NAMESPACE)/jaeger-tracegen:${DOCKER_TAG} cmd/tracegen/
@echo "Finished building jaeger-tracegen =============="

.PHONY: docker-images-anonymizer
Expand Down Expand Up @@ -390,6 +405,10 @@ docker-push:
docker push $(DOCKER_NAMESPACE)/jaeger-$$component ; \
done

.PHONY: dockerx-push
dockerx-push: setup-buildx-builder
BUILDX_PUSH=true $(MAKE) docker-images-only

.PHONY: build-crossdock-linux
build-crossdock-linux:
GOOS=linux $(GOBUILD) -o ./crossdock/crossdock-linux ./crossdock/main.go
Expand All @@ -406,7 +425,7 @@ build-crossdock-ui-placeholder:

.PHONY: build-crossdock
build-crossdock: build-crossdock-ui-placeholder build-binaries-linux build-crossdock-linux docker-images-cassandra docker-images-jaeger-backend
docker build -t $(DOCKER_NAMESPACE)/test-driver:${DOCKER_TAG} crossdock/
$(DOCKER_BUILD) -t $(DOCKER_NAMESPACE)/test-driver:${DOCKER_TAG} crossdock/
@echo "Finished building test-driver ==============" ; \

.PHONY: build-and-run-crossdock
Expand Down
4 changes: 2 additions & 2 deletions cmd/agent/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ ARG base_image
ARG debug_image

FROM $base_image AS release
ARG TARGETARCH=amd64
ARG TARGETARCH
ARG USER_UID=10001
COPY agent-linux-$TARGETARCH /go/bin/agent-linux
EXPOSE 5775/udp 6831/udp 6832/udp 5778/tcp
ENTRYPOINT ["/go/bin/agent-linux"]
USER ${USER_UID}

FROM $debug_image AS debug
ARG TARGETARCH=amd64
ARG TARGETARCH
ARG USER_UID=10001
COPY agent-debug-linux-$TARGETARCH /go/bin/agent-linux
EXPOSE 5775/udp 6831/udp 6832/udp 5778/tcp 12345/tcp
Expand Down
4 changes: 2 additions & 2 deletions cmd/all-in-one/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ARG base_image
ARG debug_image

FROM $base_image AS release
ARG TARGETARCH=amd64
ARG TARGETARCH

# Agent zipkin.thrift compact
EXPOSE 5775/udp
Expand Down Expand Up @@ -33,7 +33,7 @@ ENTRYPOINT ["/go/bin/all-in-one-linux"]
CMD ["--sampling.strategies-file=/etc/jaeger/sampling_strategies.json"]

FROM $debug_image AS debug
ARG TARGETARCH=amd64
ARG TARGETARCH

# Agent zipkin.thrift compact
EXPOSE 5775/udp
Expand Down
4 changes: 2 additions & 2 deletions cmd/collector/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ ARG base_image
ARG debug_image

FROM $base_image AS release
ARG TARGETARCH=amd64
ARG TARGETARCH
COPY collector-linux-$TARGETARCH /go/bin/collector-linux
EXPOSE 14250/tcp
ENTRYPOINT ["/go/bin/collector-linux"]

FROM $debug_image AS debug
ARG TARGETARCH=amd64
ARG TARGETARCH
COPY collector-debug-linux-$TARGETARCH /go/bin/collector-linux
EXPOSE 12345/tcp 14250/tcp
ENTRYPOINT ["/go/bin/dlv", "exec", "/go/bin/collector-linux", "--headless", "--listen=:12345", "--api-version=2", "--accept-multiclient", "--log", "--"]
4 changes: 2 additions & 2 deletions cmd/ingester/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ ARG base_image
ARG debug_image

FROM $base_image AS release
ARG TARGETARCH=amd64
ARG TARGETARCH
COPY ingester-linux-$TARGETARCH /go/bin/ingester-linux
EXPOSE 14270/tcp 14271/tcp
ENTRYPOINT ["/go/bin/ingester-linux"]

FROM $debug_image AS debug
ARG TARGETARCH=amd64
ARG TARGETARCH
COPY ingester-debug-linux-$TARGETARCH /go/bin/ingester-linux
EXPOSE 12345/tcp 14270/tcp 14271/tcp
ENTRYPOINT ["/go/bin/dlv", "exec", "/go/bin/ingester-linux", "--headless", "--listen=:12345", "--api-version=2", "--accept-multiclient", "--log", "--"]
2 changes: 1 addition & 1 deletion cmd/opentelemetry/cmd/agent/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM alpine:latest as certs
RUN apk add --update --no-cache ca-certificates

FROM scratch
ARG TARGETARCH=amd64
ARG TARGETARCH

COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt

Expand Down
2 changes: 1 addition & 1 deletion cmd/opentelemetry/cmd/all-in-one/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM alpine:latest as certs
RUN apk add --update --no-cache ca-certificates

FROM scratch
ARG TARGETARCH=amd64
ARG TARGETARCH

COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt

Expand Down
2 changes: 1 addition & 1 deletion cmd/opentelemetry/cmd/collector/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM alpine:latest as certs
RUN apk add --update --no-cache ca-certificates

FROM scratch
ARG TARGETARCH=amd64
ARG TARGETARCH
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt

COPY opentelemetry-collector-linux-$TARGETARCH /go/bin/opentelemetry-collector-linux
Expand Down
2 changes: 1 addition & 1 deletion cmd/opentelemetry/cmd/ingester/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM alpine:latest as certs
RUN apk add --update --no-cache ca-certificates

FROM scratch
ARG TARGETARCH=amd64
ARG TARGETARCH

COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt

Expand Down
4 changes: 2 additions & 2 deletions cmd/query/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ ARG base_image
ARG debug_image

FROM $base_image AS release
ARG TARGETARCH=amd64
ARG TARGETARCH
COPY query-linux-$TARGETARCH /go/bin/query-linux
EXPOSE 16686/tcp
ENTRYPOINT ["/go/bin/query-linux"]

FROM $debug_image AS debug
ARG TARGETARCH=amd64
ARG TARGETARCH
COPY query-debug-linux-$TARGETARCH /go/bin/query-linux
EXPOSE 12345/tcp 16686/tcp
ENTRYPOINT ["/go/bin/dlv", "exec", "/go/bin/query-linux", "--headless", "--listen=:12345", "--api-version=2", "--accept-multiclient", "--log", "--"]
2 changes: 1 addition & 1 deletion cmd/tracegen/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM scratch
ARG TARGETARCH=amd64
ARG TARGETARCH

COPY tracegen-linux-$TARGETARCH /go/bin/tracegen-linux
ENTRYPOINT ["/go/bin/tracegen-linux"]
14 changes: 10 additions & 4 deletions docker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ ROOT_IMAGE ?= alpine:3.12
CERT_IMAGE := alpine:3.12
GOLANG_IMAGE := golang:1.15-alpine

BASE_IMAGE := localhost/baseimg:$(VERSION)-$(shell echo $(ROOT_IMAGE) | tr : -)
DEBUG_IMAGE := localhost/debugimg:$(VERSION)-$(shell echo $(GOLANG_IMAGE) | tr : -)
BASE_IMAGE := $(DOCKER_NAMESPACE)/baseimg:$(VERSION)-$(shell echo $(ROOT_IMAGE) | tr : -)
DEBUG_IMAGE := $(DOCKER_NAMESPACE)/debugimg:$(VERSION)-$(shell echo $(GOLANG_IMAGE) | tr : -)

create-baseimg-debugimg: create-baseimg create-debugimg

dockerx-baseimg-debugimg: setup-buildx-builder
BUILDX_PUSH=true $(MAKE) create-baseimg-debugimg

create-baseimg:
docker build -t $(BASE_IMAGE) \
$(DOCKER_BUILD) -t $(BASE_IMAGE) \
--build-arg root_image=$(ROOT_IMAGE) \
--build-arg cert_image=$(CERT_IMAGE) \
docker/base

create-debugimg:
docker build -t $(DEBUG_IMAGE) \
$(DOCKER_BUILD) -t $(DEBUG_IMAGE) \
--build-arg golang_image=$(GOLANG_IMAGE) \
docker/debug

setup-buildx-builder:
bash ./scripts/setup-buildx-builder.sh
71 changes: 71 additions & 0 deletions scripts/setup-buildx-builder.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash

set -e


REQUIRED_BUILDX_VERSION="v0.4.2";
BUILDER_NAME=jaeger-builder

# Ensure "docker buildx" is available and enabled. For more details, see: https://github.com/docker/buildx/blob/master/README.md

# This does a few things:
# 1. Makes sure docker is in PATH
# 2. Downloads and installs buildx if no version of buildx is installed yet
# 3. Makes sure any installed buildx is a required version or newer
# 4. Makes sure the user has enabled buildx (either by default or by setting DOCKER_CLI_EXPERIMENTAL env var to 'enabled')
# Thus, this target will only ever succeed if a required (or newer) version of 'docker buildx' is available and enabled.

if ! which docker > /dev/null 2>&1; then echo "'docker' is not in your PATH."; exit 1; fi

if ! DOCKER_CLI_EXPERIMENTAL="enabled" docker buildx version > /dev/null 2>&1 ; then
buildx_download_url="https://github.com/docker/buildx/releases/download/${REQUIRED_BUILDX_VERSION}/buildx-${REQUIRED_BUILDX_VERSION}.$(go env GOOS)-$(go env GOARCH)";

echo "You do not have 'docker buildx' installed. Will now download from [${buildx_download_url}] and install it to [${HOME}/.docker/cli-plugins].";

mkdir -p ${HOME}/.docker/cli-plugins;
curl -L --output ${HOME}/.docker/cli-plugins/docker-buildx "${buildx_download_url}";
chmod a+x ${HOME}/.docker/cli-plugins/docker-buildx;

installed_version="$(DOCKER_CLI_EXPERIMENTAL="enabled" docker buildx version || echo "unknown")";

if docker buildx version > /dev/null 2>&1; then
echo "'docker buildx' has been installed and is enabled [version=${installed_version}]";
else
echo "An attempt to install 'docker buildx' has been made but it either failed or is not enabled by default. [version=${installed_version}]";
echo "Set DOCKER_CLI_EXPERIMENTAL=enabled to enable it.";
exit 1;
fi
fi;

current_buildx_version="$(DOCKER_CLI_EXPERIMENTAL=enabled docker buildx version 2>/dev/null | sed -E 's/.*v([0-9]+\.[0-9]+\.[0-9]+).*/\1/g')";
is_valid_buildx_version="$(if [ "$(printf ${REQUIRED_BUILDX_VERSION}\\n${current_buildx_version} | sort -V | head -n1)" == "${REQUIRED_BUILDX_VERSION}" ]; then echo "true"; else echo "false"; fi)";

if [ "${is_valid_buildx_version}" == "true" ]; then
echo "A valid version of 'docker buildx' is available: ${current_buildx_version}";
else
echo "You have an older version of 'docker buildx' that is not compatible. Please upgrade to at least v${REQUIRED_BUILDX_VERSION}";
exit 1;
fi;

if docker buildx version > /dev/null 2>&1; then
echo "'docker buildx' is enabled";
else
echo "'docker buildx' is not enabled. Set DOCKER_CLI_EXPERIMENTAL=enabled if you want to use it.";
exit 1;
fi

# Ensure a local builder for multi-arch build. For more details, see: https://github.com/docker/buildx/blob/master/README.md#building-multi-platform-images
if ! docker buildx inspect ${BUILDER_NAME} > /dev/null 2>&1; then
echo "The buildx builder instance named '${BUILDER_NAME}' does not exist. Creating one now.";
if ! docker buildx create --name=${BUILDER_NAME} --driver-opt=image=moby/buildkit:v0.8.0-rc3; then
echo "Failed to create the buildx builder '${BUILDER_NAME}'";
exit 1;
fi
fi;

if [[ $(uname -s) == "Linux" ]]; then
echo "Ensuring QEMU is set up for this Linux host";
if ! docker run --privileged --rm tonistiigi/binfmt --install all; then
echo "Failed to ensure QEMU is set up. This build will be allowed to continue, but it may fail at a later step.";
fi
fi

0 comments on commit 66a4599

Please sign in to comment.