Skip to content

Commit 1a5ec7f

Browse files
[8.18](backport #5392) fix: package fleet-server using golang-crossbuild (#5427)
* fix: package fleet-server using golang-crossbuild (#5392) * fix: package fleet-server using golang-crossbuild and introduce packaging steps on every PR * feat: save artifacts during packaging for DRA * fix: disable CGO and buildmode pie * doc: add changelog fragment (cherry picked from commit 7da3f69) # Conflicts: # .buildkite/pipeline.package.mbp.yml # .buildkite/pipeline.yml # .buildkite/scripts/package.sh # Dockerfile.fips # magefile.go * fix: adjust changes for make * fix: fleet-server-package-mbp CI step dependencies (#5446) (cherry picked from commit 5aa10ef) --------- Co-authored-by: Panos Koutsovasilis <[email protected]>
1 parent a03b315 commit 1a5ec7f

File tree

7 files changed

+108
-53
lines changed

7 files changed

+108
-53
lines changed

.buildkite/pipeline.package.mbp.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ steps:
2828
provider: "gcp"
2929
image: "${IMAGE_UBUNTU_X86_64}"
3030
machineType: "c2-standard-16"
31+
artifact_paths:
32+
- build/distributions/**
3133
plugins:
3234
- *oidc_plugin
3335

@@ -38,6 +40,8 @@ steps:
3840
provider: "gcp"
3941
image: "${IMAGE_UBUNTU_X86_64}"
4042
machineType: "c2-standard-16"
43+
artifact_paths:
44+
- build/distributions/**
4145
plugins:
4246
- *oidc_plugin
4347

@@ -49,6 +53,8 @@ steps:
4953
provider: "aws"
5054
imagePrefix: "${IMAGE_UBUNTU_ARM_64}"
5155
instanceType: "t4g.2xlarge"
56+
artifact_paths:
57+
- build/distributions/**
5258
plugins:
5359
- *oidc_plugin
5460

@@ -59,6 +65,8 @@ steps:
5965
provider: "aws"
6066
imagePrefix: "${IMAGE_UBUNTU_ARM_64}"
6167
instanceType: "t4g.2xlarge"
68+
artifact_paths:
69+
- build/distributions/**
6270
plugins:
6371
- *oidc_plugin
6472

.buildkite/pipeline.yml

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ env:
99
DOCKER_IMAGE_GIT_TAG: "${BUILDKITE_BRANCH}" # needs to rename for rollback
1010
GO_AGENT_IMAGE: "golang:${GO_VERSION}"
1111
TERRAFORM_VERSION: "1.6.4"
12+
IMAGE_UBUNTU_X86_64: "family/core-ubuntu-2204"
13+
IMAGE_UBUNTU_ARM_64: "core-ubuntu-2204-aarch64"
14+
15+
# This section is used to define the plugins that will be used in the pipeline.
16+
# See https://buildkite.com/docs/pipelines/integrations/plugins/using#using-yaml-anchors-with-plugins
17+
common:
18+
# See https://github.com/elastic/oblt-infra/blob/main/conf/resources/repos/fleet-server/01-gcp-buildkite-oidc.tf
19+
# This plugin authenticates to Google Cloud using the OIDC token.
20+
- oidc_plugin: &oidc_plugin
21+
elastic/oblt-google-auth#v1.2.0:
22+
lifetime: 10800 # seconds
23+
project-id: "elastic-observability-ci"
24+
project-number: "911195782929"
1225

1326
steps:
1427
- group: "Check and build"
@@ -22,20 +35,33 @@ steps:
2235
cpu: "8"
2336
memory: "4G"
2437

25-
- label: ":building_construction: Run local build"
26-
key: make-local
27-
command: "make local"
38+
- label: "Package x86_64"
39+
key: "package-x86-64-pr"
40+
env:
41+
PLATFORMS: "linux/amd64 darwin/amd64 windows/amd64"
42+
command: ".buildkite/scripts/release_test.sh"
43+
artifact_paths:
44+
- build/distributions/**
2845
agents:
29-
image: "${GO_AGENT_IMAGE}"
30-
cpu: "8"
31-
memory: "4G"
46+
provider: "gcp"
47+
image: "${IMAGE_UBUNTU_X86_64}"
48+
machineType: "c2-standard-16"
49+
plugins:
50+
- *oidc_plugin
3251

33-
- label: ":building_construction: Run local build: MacOS"
34-
key: make-local-macos
35-
command: ".buildkite/scripts/local_build.sh"
52+
- label: "Package aarch64"
53+
key: "package-arm64-pr"
54+
env:
55+
PLATFORMS: "linux/arm64 darwin/arm64"
56+
command: ".buildkite/scripts/release_test.sh"
57+
artifact_paths:
58+
- build/distributions/**
3659
agents:
37-
provider: orka
38-
imagePrefix: generic-13-ventura-arm
60+
provider: "aws"
61+
imagePrefix: "${IMAGE_UBUNTU_ARM_64}"
62+
instanceType: "t4g.2xlarge"
63+
plugins:
64+
- *oidc_plugin
3965

4066
- group: "Performance test"
4167
key: "performance-test"
@@ -187,22 +213,6 @@ steps:
187213
depends_on:
188214
- step: "publish"
189215

190-
- label: ":gcloud: Release test"
191-
key: "release-test"
192-
command: ".buildkite/scripts/release_test.sh"
193-
agents:
194-
provider: "gcp"
195-
depends_on:
196-
- step: "tests"
197-
allow_failure: false
198-
plugins:
199-
# See https://github.com/elastic/oblt-infra/blob/main/conf/resources/repos/fleet-server/01-gcp-buildkite-oidc.tf
200-
# This plugin authenticates to Google Cloud using the OIDC token.
201-
- elastic/oblt-google-auth#v1.2.0:
202-
lifetime: 10800 # seconds
203-
project-id: "elastic-observability-ci"
204-
project-number: "911195782929"
205-
206216
- label: ":jenkins: Release - Package Registry Distribution"
207217
key: "release-package-registry"
208218
trigger: "package-registry-release-package-registry-distribution"
@@ -221,5 +231,7 @@ steps:
221231
build:
222232
branch: "${BUILDKITE_BRANCH}"
223233
depends_on:
224-
- step: "release-test"
234+
- step: "package-x86-64-pr"
235+
allow_failure: false
236+
- step: "package-arm64-pr"
225237
allow_failure: false

.buildkite/scripts/package.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ with_mage
2727
case "${TYPE}" in
2828
"snapshot")
2929
export SNAPSHOT=true
30-
make release
30+
make docker-release
3131
;;
3232
"staging")
33-
make release
33+
make docker-release
3434
;;
3535
*)
3636
echo "The option is unsupported yet"

.buildkite/scripts/test-release.sh

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,26 @@
33
set -euo pipefail
44

55
FLEET_SERVER_VERSION=${1:?"Fleet Server version is needed"}
6+
PLATFORMS=${PLATFORMS:-"darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 windows/amd64"}
67

7-
PLATFORM_FILES=(darwin-aarch64.tar.gz darwin-x86_64.tar.gz linux-arm64.tar.gz linux-x86_64.tar.gz windows-x86_64.zip)
8-
if [ "$FIPS" = "true" ] ; then
9-
PLATFORM_FILES=(linux-arm64-fips.tar.gz linux-x86_64-fips.tar.gz)
10-
fi
8+
PLATFORM_FILES=()
9+
for p in $PLATFORMS; do
10+
os="${p%%/*}"
11+
arch="${p##*/}"
12+
13+
case "$os/$arch" in
14+
darwin/arm64) arch="aarch64" ;;
15+
*/amd64) arch="x86_64" ;;
16+
esac
17+
18+
case "$os" in
19+
windows) ext="zip" ;;
20+
*) ext="tar.gz" ;;
21+
esac
22+
23+
file="${os}-${arch}.${ext}"
24+
PLATFORM_FILES+=("$file")
25+
done
1126

1227
#make release
1328

@@ -23,17 +38,17 @@ for PLATFORM_FILE in "${PLATFORM_FILES[@]}"
2338
do
2439
file="${FILE_PREFIX}${PLATFORM_FILE}"
2540
if [ ! -f "${file}" ]; then
26-
echo -e "${RED}!! ${PLATFORM_FILE}: The file was not created.${NO_COLOR}"
41+
echo -e "${RED}!! ${file}: The file was not created.${NO_COLOR}"
2742
exit 1
2843
else
29-
echo -e "- ${PLATFORM_FILE} ${GREEN}OK${NO_COLOR}"
44+
echo -e "- ${file} ${GREEN}OK${NO_COLOR}"
3045
fi
3146

3247
fileSha512="${file}.sha512"
3348
if [ ! -f "${fileSha512}" ]; then
3449
echo -e "${RED}!! ${fileSha512}: The file was not created.${NO_COLOR}"
3550
exit 1
3651
else
37-
echo -e "- ${PLATFORM_FILE}.sha512 ${GREEN}OK${NO_COLOR}"
52+
echo -e "- ${file}.sha512 ${GREEN}OK${NO_COLOR}"
3853
fi
3954
done

Dockerfile.fips

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,22 @@ RUN go mod download && go mod verify
1313
ENTRYPOINT [ "make" ]
1414
CMD [ "release" ]
1515

16+
<<<<<<< HEAD
1617
FROM base AS builder
18+
=======
19+
# FIPS docker image defined below
20+
# Does not use base as the lowest layer so we don't have to deal with user/ownership issues when building the image.
21+
ARG GO_VERSION
22+
# Suffix should be main-debian11-fips or base-arm-debian11-fips
23+
ARG SUFFIX
24+
FROM docker.elastic.co/beats-dev/golang-crossbuild:${GO_VERSION}-${SUFFIX} AS builder
25+
26+
WORKDIR /fleet-server
27+
ENV PATH="$PATH:/go/bin"
28+
COPY go.mod go.sum ./
29+
RUN go mod download && go mod verify
30+
RUN go install github.com/magefile/mage # uses version in go.mod
31+
>>>>>>> 7da3f69 (fix: package fleet-server using golang-crossbuild (#5392))
1732

1833
COPY . .
1934

Makefile

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ DEFAULT_VERSION=$(shell awk '/const DefaultVersion/{print $$NF}' version/version
1212
TARGET_ARCH_amd64=x86_64
1313
TARGET_ARCH_arm64=arm64
1414
PLATFORMS ?= darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 windows/amd64
15-
BUILDMODE_linux_amd64=-buildmode=pie
16-
BUILDMODE_linux_arm64=-buildmode=pie
17-
BUILDMODE_windows_amd64=-buildmode=pie
18-
BUILDMODE_darwin_amd64=-buildmode=pie
19-
BUILDMODE_darwin_arm64=-buildmode=pie
2015

2116
CROSSBUILD_SUFFIX=main-debian11
2217
CROSSBUILD_ARM_SUFFIX=base-arm-debian9
@@ -65,6 +60,10 @@ ifeq "${FIPS}" "true"
6560
DOCKER_IMAGE_TAG:=${DOCKER_IMAGE_TAG}-fips
6661
endif
6762
endif
63+
CGO_ENABLED?=0
64+
ifeq "${FIPS}" "true"
65+
CGO_ENABLED=1
66+
endif
6867
DOCKER_IMAGE?=docker.elastic.co/fleet-server/fleet-server
6968

7069
PLATFORM_TARGETS=$(addprefix release-, $(PLATFORMS))
@@ -138,7 +137,7 @@ list-platforms: ## - Show the possible PLATFORMS
138137
.PHONY: local
139138
local: ## - Build local binary for local environment (bin/fleet-server)
140139
@printf "${CMD_COLOR_ON} Build binaries using local go installation\n${CMD_COLOR_OFF}"
141-
${GOFIPSEXPERIMENT} go build -tags=${GOBUILDTAGS} -gcflags="${GCFLAGS}" -ldflags="${LDFLAGS}" -o ./bin/fleet-server .
140+
CGO_ENABLED=${CGO_ENABLED} ${GOFIPSEXPERIMENT} go build -tags=${GOBUILDTAGS} -gcflags="${GCFLAGS}" -ldflags="${LDFLAGS}" -o ./bin/fleet-server .
142141
@printf "${CMD_COLOR_ON} Binaries in ./bin/\n${CMD_COLOR_OFF}"
143142

144143
.PHONY: $(COVER_TARGETS)
@@ -147,8 +146,7 @@ $(COVER_TARGETS): cover-%: ## - Build a binary with the -cover flag for integrat
147146
$(eval $@_OS := $(firstword $(subst /, ,$(lastword $(subst cover-, ,$@)))))
148147
$(eval $@_GO_ARCH := $(lastword $(subst /, ,$(lastword $(subst cover-, ,$@)))))
149148
$(eval $@_ARCH := $(TARGET_ARCH_$($@_GO_ARCH)))
150-
$(eval $@_BUILDMODE:= $(BUILDMODE_$($@_OS)_$($@_GO_ARCH)))
151-
GOOS=$($@_OS) GOARCH=$($@_GO_ARCH) ${GOFIPSEXPERIMENT} go build -tags=${GOBUILDTAGS} -cover -coverpkg=./... -gcflags="${GCFLAGS}" -ldflags="${LDFLAGS}" $($@_BUILDMODE) -o build/cover/fleet-server-$(VERSION)-$($@_OS)-$($@_ARCH)$(FIPSSUFFIX)/fleet-server$(if $(filter windows,$($@_OS)),.exe,) .
149+
CGO_ENABLED=${CGO_ENABLED} GOOS=$($@_OS) GOARCH=$($@_GO_ARCH) ${GOFIPSEXPERIMENT} go build -tags=${GOBUILDTAGS} -cover -coverpkg=./... -gcflags="${GCFLAGS}" -ldflags="${LDFLAGS}" -o build/cover/fleet-server-$(VERSION)-$($@_OS)-$($@_ARCH)$(FIPSSUFFIX)/fleet-server$(if $(filter windows,$($@_OS)),.exe,) .
152150

153151
.PHONY: clean
154152
clean: ## - Clean up build artifacts
@@ -223,11 +221,11 @@ test-release: ## - Check that all release binaries are created
223221

224222
.PHONY: test-unit
225223
test-unit: prepare-test-context ## - Run unit tests only
226-
set -o pipefail; go test ${GO_TEST_FLAG} -tags=$(GOBUILDTAGS) -v -race -coverprofile=build/coverage-${OS_NAME}.out ./... | tee build/test-unit-${OS_NAME}.out
224+
set -o pipefail; CGO_ENABLED=1 go test ${GO_TEST_FLAG} -tags=$(GOBUILDTAGS) -v -race -coverprofile=build/coverage-${OS_NAME}.out ./... | tee build/test-unit-${OS_NAME}.out
227225

228226
.PHONY: benchmark
229227
benchmark: prepare-test-context install-benchstat ## - Run benchmark tests only
230-
set -o pipefail; go test -bench=$(BENCHMARK_FILTER) -tags=$(GOBUILDTAGS) -run=$(BENCHMARK_FILTER) $(BENCHMARK_ARGS) $(BENCHMARK_PACKAGE) | tee "build/$(BENCH_BASE)"
228+
set -o pipefail; CGO_ENABLED=1 go test -bench=$(BENCHMARK_FILTER) -tags=$(GOBUILDTAGS) -run=$(BENCHMARK_FILTER) $(BENCHMARK_ARGS) $(BENCHMARK_PACKAGE) | tee "build/$(BENCH_BASE)"
231229

232230
.PHONY: install-benchstat
233231
install-benchstat: ## - Install the benchstat package
@@ -262,8 +260,7 @@ $(PLATFORM_TARGETS): release-%:
262260
$(eval $@_OS := $(firstword $(subst /, ,$(lastword $(subst release-, ,$@)))))
263261
$(eval $@_GO_ARCH := $(lastword $(subst /, ,$(lastword $(subst release-, ,$@)))))
264262
$(eval $@_ARCH := $(TARGET_ARCH_$($@_GO_ARCH)))
265-
$(eval $@_BUILDMODE:= $(BUILDMODE_$($@_OS)_$($@_GO_ARCH)))
266-
GOOS=$($@_OS) GOARCH=$($@_GO_ARCH) ${GOFIPSEXPERIMENT} go build -tags=${GOBUILDTAGS} -gcflags="${GCFLAGS}" -ldflags="${LDFLAGS}" $($@_BUILDMODE) -o build/binaries/fleet-server-$(VERSION)-$($@_OS)-$($@_ARCH)$(FIPSSUFFIX)/fleet-server .
263+
CGO_ENABLED=${CGO_ENABLED} GOOS=$($@_OS) GOARCH=$($@_GO_ARCH) ${GOFIPSEXPERIMENT} go build -tags=${GOBUILDTAGS} -gcflags="${GCFLAGS}" -ldflags="${LDFLAGS}" -o build/binaries/fleet-server-$(VERSION)-$($@_OS)-$($@_ARCH)$(FIPSSUFFIX)/fleet-server .
267264
@$(MAKE) OS=$($@_OS) ARCH=$($@_ARCH) package-target
268265

269266
.PHONY: build-docker
@@ -314,6 +311,8 @@ endif
314311
build-releaser: ## - Build a Docker image to run make package including all build tools
315312
ifeq ($(shell uname -p),arm)
316313
$(eval SUFFIX := ${CROSSBUILD_ARM_SUFFIX})
314+
else ifeq ($(shell uname -p),aarch64)
315+
$(eval SUFFIX := ${CROSSBUILD_ARM_SUFFIX})
317316
else
318317
$(eval SUFFIX := ${CROSSBUILD_SUFFIX})
319318
endif
@@ -325,16 +324,16 @@ endif
325324

326325
.PHONY: docker-release
327326
docker-release: build-releaser ## - Builds a release for all platforms in a dockerised environment
328-
docker run --rm -u $(shell id -u):$(shell id -g) --env=GOCACHE=/go/cache --volume $(PWD):/go/src/github.com/elastic/fleet-server $(BUILDER_IMAGE) release
327+
docker run --rm -u $(shell id -u):$(shell id -g) --env=GOCACHE=/go/cache --env='PLATFORMS=${PLATFORMS}' --volume $(PWD):/go/src/github.com/elastic/fleet-server $(BUILDER_IMAGE) release
329328

330329
.PHONY: docker-cover-e2e-binaries
331330
docker-cover-e2e-binaries: build-releaser
332331
ifeq "${FIPS}" "true"
333332
## non-linux is currently unsupported for FIPS
334-
docker run --rm -u $(shell id -u):$(shell id -g) --env=GOCACHE=/go/cache --volume $(PWD):/go/src/github.com/elastic/fleet-server -e SNAPSHOT=true -e DEV=$(DEV) -e FIPS=$(FIPS) $(BUILDER_IMAGE) cover-linux/$(shell go env GOARCH)
333+
docker run --rm -u $(shell id -u):$(shell id -g) --env=GOCACHE=/go/cache --env='PLATFORMS=${PLATFORMS}' --volume $(PWD):/go/src/github.com/elastic/fleet-server -e SNAPSHOT=true -e DEV=$(DEV) -e FIPS=$(FIPS) $(BUILDER_IMAGE) cover-linux/$(shell go env GOARCH)
335334
else
336335
## Build for local architecture and for linux/$ARCH for docker images.
337-
docker run --rm -u $(shell id -u):$(shell id -g) --env=GOCACHE=/go/cache --volume $(PWD):/go/src/github.com/elastic/fleet-server -e SNAPSHOT=true -e DEV=$(DEV) -e FIPS=$(FIPS) $(BUILDER_IMAGE) cover-linux/$(shell go env GOARCH) cover-$(shell go env GOOS)/$(shell go env GOARCH)
336+
docker run --rm -u $(shell id -u):$(shell id -g) --env=GOCACHE=/go/cache --env='PLATFORMS=${PLATFORMS}' --volume $(PWD):/go/src/github.com/elastic/fleet-server -e SNAPSHOT=true -e DEV=$(DEV) -e FIPS=$(FIPS) $(BUILDER_IMAGE) cover-linux/$(shell go env GOARCH) cover-$(shell go env GOOS)/$(shell go env GOARCH)
338337
endif
339338

340339
.PHONY: release
@@ -404,6 +403,7 @@ test-int-set: ## - Run integration tests without setup
404403
REMOTE_ELASTICSEARCH_SERVICE_TOKEN=$(shell ./dev-tools/integration/get-elasticsearch-servicetoken.sh https://${ELASTICSEARCH_USERNAME}:${ELASTICSEARCH_PASSWORD}@${TEST_REMOTE_ELASTICSEARCH_HOST} "fleet-server-remote") \
405404
REMOTE_ELASTICSEARCH_CA_CRT_BASE64="$(shell COMPOSE_PROJECT_NAME=integration docker compose -f ./dev-tools/e2e/docker-compose.yml --env-file ./dev-tools/integration/.env exec elasticsearch-remote /bin/bash -c "cat /usr/share/elasticsearch/config/certs/ca/ca.crt" | base64)" \
406405
ELASTICSEARCH_HOSTS=${TEST_ELASTICSEARCH_HOSTS} ELASTICSEARCH_USERNAME=${ELASTICSEARCH_USERNAME} ELASTICSEARCH_PASSWORD=${ELASTICSEARCH_PASSWORD} \
406+
CGO_ENABLED=1 \
407407
go test -v -tags=integration -count=1 -race -p 1 ./...
408408

409409
##################################################
@@ -465,4 +465,4 @@ test-cloude2e: prepare-test-context ## - Run cloude2e tests with full setup (sl
465465
test-cloude2e-set: ## Run cloude2e test
466466
$(eval FLEET_SERVER_URL := $(shell make --no-print-directory -C ${CLOUD_TESTING_BASE} cloud-get-fleet-url))
467467
make -C ${CLOUD_TESTING_BASE} cloud-get-fleet-url
468-
FLEET_SERVER_URL="${FLEET_SERVER_URL}" go test -v -tags=cloude2e -count=1 -race -p 1 ./testing/cloude2e
468+
FLEET_SERVER_URL="${FLEET_SERVER_URL}" CGO_ENABLED=1 go test -v -tags=cloude2e -count=1 -race -p 1 ./testing/cloude2e
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: bug-fix
2+
summary: Build fleet-server as fully static binary to restore OS matrix compatibility
3+
component: fleet-server
4+
pr: https://github.com/elastic/fleet-server/pull/5392
5+
issue: https://github.com/elastic/fleet-server/issues/5262

0 commit comments

Comments
 (0)