Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] Support ARM build for fleet server standalone docker image #2832

Merged
merged 8 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .buildkite/scripts/build_push_docker_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 0 additions & 5 deletions .buildkite/scripts/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
10 changes: 7 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
23 changes: 16 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -198,18 +199,25 @@ $(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}" \
--build-arg=DEV="$(DEV)" \
--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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
26 changes: 8 additions & 18 deletions testing/e2e/stand_alone_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"

Expand All @@ -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) {
Expand Down Expand Up @@ -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))
Expand All @@ -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,
Expand Down
Loading