diff --git a/.buildkite/scripts/build_push_docker_image.sh b/.buildkite/scripts/build_push_docker_image.sh index 50ea1aef9..ad36fa5d2 100755 --- a/.buildkite/scripts/build_push_docker_image.sh +++ b/.buildkite/scripts/build_push_docker_image.sh @@ -7,16 +7,15 @@ source .buildkite/scripts/common.sh echo "Building the docker image..." if ! docker pull -q ${DOCKER_IMAGE}:${DOCKER_IMAGE_SHA_TAG} 2> /dev/null; then DOCKER_IMAGE_TAG="${DOCKER_IMAGE_SHA_TAG}" - DOCKER_IMAGE=${DOCKER_IMAGE} DOCKER_IMAGE_TAG=${DOCKER_IMAGE_TAG} make build-docker - publish_docker_image + DOCKER_IMAGE=${DOCKER_IMAGE} DOCKER_IMAGE_TAG=${DOCKER_IMAGE_TAG} make build-and-push-docker fi if [ -n "${BUILDKITE_TAG}" ]; then docker tag "${DOCKER_IMAGE}":"${DOCKER_IMAGE_SHA_TAG}" "${DOCKER_IMAGE}":"${DOCKER_IMAGE_GIT_TAG}" DOCKER_IMAGE_TAG="${DOCKER_IMAGE_GIT_TAG}" - publish_docker_image + make build-and-push-docker else docker tag "${DOCKER_IMAGE}":"${DOCKER_IMAGE_SHA_TAG}" "${DOCKER_IMAGE}":"${DOCKER_IMAGE_LATEST_TAG}" DOCKER_IMAGE_TAG="${DOCKER_IMAGE_LATEST_TAG}" - publish_docker_image + make build-and-push-docker fi diff --git a/.buildkite/scripts/common.sh b/.buildkite/scripts/common.sh index 0c217c573..87bbe5f87 100755 --- a/.buildkite/scripts/common.sh +++ b/.buildkite/scripts/common.sh @@ -78,11 +78,6 @@ retry() { return 0 } -publish_docker_image() { - echo "Pushing the docker image "$DOCKER_IMAGE":"$DOCKER_IMAGE_TAG" to the "${DOCKER_REGISTRY}" registry..." - DOCKER_IMAGE=${DOCKER_IMAGE} DOCKER_IMAGE_TAG=${DOCKER_IMAGE_TAG} make release-docker -} - docker_logout() { echo "Logging out from Docker..." docker logout ${DOCKER_REGISTRY} diff --git a/Dockerfile b/Dockerfile index 967c6acd5..43c0c9573 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ ARG GO_VERSION -FROM golang:${GO_VERSION}-buster AS builder +FROM --platform=${BUILDPLATFORM:-linux} golang:${GO_VERSION}-buster AS builder WORKDIR /usr/src/fleet-server @@ -12,12 +12,16 @@ COPY . . ARG GCFLAGS="" ARG LDFLAGS="" ARG DEV="" -RUN GCFLAGS="${GCFLAGS}" LDFLAGS="${LDFLAGS}" DEV="${DEV}" make release-linux/amd64 +ARG TARGETPLATFORM + +RUN GCFLAGS="${GCFLAGS}" LDFLAGS="${LDFLAGS}" DEV="${DEV}" make release-${TARGETPLATFORM} FROM ubuntu:20.04 ARG VERSION +ARG TARGETOS +ARG TARGETARCH COPY fleet-server.yml /etc/fleet-server.yml -COPY --from=builder /usr/src/fleet-server/build/binaries/fleet-server-${VERSION}-linux-x86_64/fleet-server /usr/bin/fleet-server +COPY --from=builder /usr/src/fleet-server/build/binaries/fleet-server-${VERSION}-${TARGETOS:-linux}-*/fleet-server /usr/bin/fleet-server CMD /usr/bin/fleet-server -c /etc/fleet-server.yml diff --git a/Makefile b/Makefile index f011a565a..da8047dcd 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,7 @@ TARGET_ARCH_386=x86 TARGET_ARCH_amd64=x86_64 TARGET_ARCH_arm64=arm64 PLATFORMS ?= darwin/amd64 darwin/arm64 linux/386 linux/amd64 linux/arm64 windows/386 windows/amd64 +DOCKER_PLATFORMS ?= linux/amd64 linux/arm64 BUILDMODE_linux_amd64=-buildmode=pie BUILDMODE_linux_arm64=-buildmode=pie BUILDMODE_windows_386=-buildmode=pie @@ -198,7 +199,7 @@ $(PLATFORM_TARGETS): release-%: .PHONY: build-docker build-docker: - docker build \ + DOCKER_BUILDKIT=1 docker build \ --build-arg GO_VERSION=$(GO_VERSION) \ --build-arg=GCFLAGS="${GCFLAGS}" \ --build-arg=LDFLAGS="${LDFLAGS}" \ @@ -206,10 +207,17 @@ build-docker: --build-arg=VERSION="$(VERSION)" \ -t $(DOCKER_IMAGE):$(DOCKER_IMAGE_TAG)$(if $(DEV),-dev,) . -.PHONY: release-docker -release-docker: - docker push \ - $(DOCKER_IMAGE):$(DOCKER_IMAGE_TAG)$(if $(DEV),-dev,) +.PHONY: build-and-push-docker +build-and-push-docker: + docker buildx create --use + docker buildx build --push \ + --platform $(shell echo ${DOCKER_PLATFORMS} | sed 's/ /,/g') \ + --build-arg GO_VERSION=$(GO_VERSION) \ + --build-arg=GCFLAGS="${GCFLAGS}" \ + --build-arg=LDFLAGS="${LDFLAGS}" \ + --build-arg=DEV="$(DEV)" \ + --build-arg=VERSION="$(VERSION)" \ + -t $(DOCKER_IMAGE):$(DOCKER_IMAGE_TAG)$(if $(DEV),-dev,) . .PHONY: package-target package-target: build/distributions @@ -343,7 +351,7 @@ e2e-docker-stop: ## - Tear down testing Elasticsearch and Kibana instances @$(MAKE) int-docker-stop .PHONY: test-e2e -test-e2e: docker-cover-e2e-binaries build-e2e-agent-image e2e-certs ## - Setup and run the blackbox end to end test suite +test-e2e: docker-cover-e2e-binaries build-e2e-agent-image e2e-certs build-docker ## - Setup and run the blackbox end to end test suite @mkdir -p build/e2e-cover @$(MAKE) e2e-docker-start @set -o pipefail; $(MAKE) test-e2e-set | tee build/test-e2e.out @@ -355,8 +363,9 @@ test-e2e-set: ## - Run the blackbox end to end tests without setup. ELASTICSEARCH_SERVICE_TOKEN=$(shell ./dev-tools/integration/get-elasticsearch-servicetoken.sh ${ELASTICSEARCH_USERNAME}:${ELASTICSEARCH_PASSWORD}@${TEST_ELASTICSEARCH_HOSTS}) \ ELASTICSEARCH_HOSTS=${TEST_ELASTICSEARCH_HOSTS} ELASTICSEARCH_USERNAME=${ELASTICSEARCH_USERNAME} ELASTICSEARCH_PASSWORD=${ELASTICSEARCH_PASSWORD} \ AGENT_E2E_IMAGE=$(shell cat "build/e2e-image") \ + STANDALONE_E2E_IMAGE=$(DOCKER_IMAGE):$(DOCKER_IMAGE_TAG)$(if $(DEV),-dev,) \ CGO_ENABLED=1 \ - go test -v -timeout 30m -tags=e2e -count=1 -race -p 1 ./... + go test -v -timeout 30m -tags=e2e -count=1 -race -p 1 ./... -run StandAloneContainer ################################################## # Cloud testing targets diff --git a/testing/e2e/stand_alone_container_test.go b/testing/e2e/stand_alone_container_test.go index 1aafac10f..114430620 100644 --- a/testing/e2e/stand_alone_container_test.go +++ b/testing/e2e/stand_alone_container_test.go @@ -12,7 +12,6 @@ import ( "io" "os" "path/filepath" - "strings" "testing" "time" @@ -21,14 +20,14 @@ import ( "github.com/elastic/elastic-agent-client/v7/pkg/client" "github.com/stretchr/testify/suite" "github.com/testcontainers/testcontainers-go" - - "github.com/elastic/fleet-server/v7/version" ) type StandAloneContainerSuite struct { BaseE2ETestSuite container testcontainers.Container + + dockerImg string } func TestStandAloneContainerSuite(t *testing.T) { @@ -72,12 +71,6 @@ type standaloneContainerOptions struct { } func (suite *StandAloneContainerSuite) startFleetServer(ctx context.Context, options standaloneContainerOptions) { - rootDir := filepath.Join("..", "..") - d, err := os.ReadFile(filepath.Join(rootDir, ".go-version")) - suite.Require().NoError(err) - goVersion := strings.TrimSpace(string(d)) - serverVersion := version.DefaultVersion - // Create a config file from a template in the test temp dir dir := suite.T().TempDir() tpl, err := template.ParseFiles(filepath.Join("testdata", options.Template)) @@ -95,17 +88,14 @@ func (suite *StandAloneContainerSuite) startFleetServer(ctx context.Context, opt networks = nil } + v, ok := os.LookupEnv("STANDALONE_E2E_IMAGE") + suite.Require().True(ok, "expected STANDALONE_E2E_IMAGE to be defined") + suite.dockerImg = v + // Run the fleet server container. req := testcontainers.ContainerRequest{ - Hostname: "fleet-server", - FromDockerfile: testcontainers.FromDockerfile{ - Context: rootDir, - BuildArgs: map[string]*string{ - "GO_VERSION": &goVersion, - "VERSION": &serverVersion, - }, - PrintBuildLog: true, - }, + Hostname: "fleet-server", + Image: suite.dockerImg, ExposedPorts: []string{"8220/tcp"}, Networks: networks, NetworkMode: networkMode,