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

migrate xpack-metricbeat #38081

Merged
merged 82 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
2b5dd7b
migrate xpack-metricbeat
Feb 21, 2024
cf44101
change pre-command
Feb 21, 2024
e432525
add mage_version
Feb 21, 2024
0783eee
add mage_version
Feb 21, 2024
2a1de26
disable some trigger steps
Feb 21, 2024
7e55863
change command with mage
Feb 21, 2024
cf55e5b
add ASDF_MAGE_VERSION
Feb 21, 2024
def5673
fix arm image name
Feb 21, 2024
85bfec8
fix macos
Feb 21, 2024
5af9510
test arm packaging
Feb 22, 2024
90bec63
test aws/macos
Feb 22, 2024
bd038c2
test macos
Feb 22, 2024
77b29b2
test macos
Feb 22, 2024
5b41d3b
test macos
Feb 22, 2024
2751e38
test macos
Feb 22, 2024
88800c8
test macos
Feb 22, 2024
4805ccb
fix macos declare
Feb 22, 2024
e242568
add export MODULE
Feb 23, 2024
d41b68d
test aws
Feb 23, 2024
5319753
test aws
Feb 23, 2024
32229ce
add withModule
Feb 23, 2024
a09803e
move withModule to pre-command
Feb 23, 2024
271c788
move withModule to pre-command
Feb 23, 2024
cd2282d
move withModule to command
Feb 23, 2024
8eba9dc
fix withModule
Feb 23, 2024
1781fc0
test aws changes
Feb 23, 2024
c12fe5a
test aws changes
Feb 23, 2024
dd1e93e
change the are_changed_only_paths
Feb 24, 2024
d0f433b
revert aws changes and test
Feb 24, 2024
751403b
Merge branch 'main' into migrate-xpack-metricbeat
sharbuz Feb 26, 2024
39caa7b
Merge branch 'main' of github.com:sharbuz/beats into migrate-xpack-me…
Feb 26, 2024
2c4eb33
Merge branch 'main' of github.com:elastic/beats into migrate-xpack-me…
Feb 26, 2024
5696ea2
test with 240min timeout
Feb 26, 2024
b6347ec
Merge branch 'migrate-xpack-metricbeat' of github.com:sharbuz/beats i…
Feb 26, 2024
ba45c92
revert aws changes and test
Feb 26, 2024
eaaa095
full test
Feb 26, 2024
4612e00
change the machine config for aws and go-int test steps
Feb 27, 2024
e709ccf
add aws and terraform
Feb 28, 2024
79c8f95
add docker-compose
Feb 28, 2024
ef0bd84
add withDocker
Feb 29, 2024
873c7d8
add withDocker
Feb 29, 2024
ae65e7f
add cloud scrit and replace commands in the Cloud and Go-int steps
Feb 29, 2024
f8a2a09
add cloud scrit and replace commands in the Cloud and Go-int steps
Feb 29, 2024
35d8957
fix terraformInit
Feb 29, 2024
bf8df0d
test
Mar 4, 2024
0d28c94
test
Mar 5, 2024
724e1c2
test
Mar 5, 2024
44fae9b
changed the logic of cloud and Go int tests
Mar 5, 2024
b488f25
aws test
Mar 5, 2024
2e531f8
aws test without aws changes
Mar 5, 2024
e440521
Jenkins test
Mar 6, 2024
20d2184
module test
Mar 6, 2024
5fba521
debug
Mar 6, 2024
f5a6545
debug
Mar 6, 2024
5e71d65
debug
Mar 6, 2024
2aa3993
debug
Mar 6, 2024
7545802
rolback changes
Mar 6, 2024
929553f
rolback changes
Mar 6, 2024
9303a2a
rolback changes
Mar 6, 2024
700f8c1
test
Mar 7, 2024
d649de3
test
Mar 7, 2024
56d24bf
remove aws changes
Mar 7, 2024
ad0492a
test with hardcoded MODULE=aws
Mar 7, 2024
d532855
test with hardcoded MODULE=aws
Mar 7, 2024
5823883
test with hardcoded MODULE=''
Mar 7, 2024
6b3e9de
test
Mar 7, 2024
bd864ec
test
Mar 7, 2024
6ff685b
test
Mar 7, 2024
9907579
full test
Mar 7, 2024
48e1c08
change images and add cleanup
Mar 7, 2024
fd09867
work with remarks
Mar 11, 2024
0db0696
replace return with echo
Mar 11, 2024
6c59680
revert exporting var
Mar 11, 2024
1ca09df
update method name and description
Mar 11, 2024
b1fdc2c
change cloud_tests
Mar 11, 2024
25547b4
move conditions to the pre-command hook
Mar 11, 2024
c96b847
revert all last changes
Mar 11, 2024
1057a94
test Victor's suggestion
Mar 12, 2024
84e8305
test Victor's suggestion
Mar 12, 2024
141c990
change defineModuleFromTheChangeSet
Mar 12, 2024
3cf8aa0
change defineModuleFromTheChangeSet
Mar 12, 2024
f432822
add true for teardown
Mar 12, 2024
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
30 changes: 29 additions & 1 deletion .buildkite/hooks/pre-command
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@

set -euo pipefail

AWS_SERVICE_ACCOUNT_SECRET_PATH="kv/ci-shared/platform-ingest/aws_account_auth"

retry() {
v1v marked this conversation as resolved.
Show resolved Hide resolved
local retries=$1
shift
local count=0
until "$@"; do
exit=$?
wait=$((2 ** count))
count=$((count + 1))
if [ $count -lt "$retries" ]; then
>&2 echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
sleep $wait
else
>&2 echo "Retry $count/$retries exited $exit, no more retries left."
return $exit
fi
done
return 0
}

if [[ "$BUILDKITE_PIPELINE_SLUG" == "filebeat" || "$BUILDKITE_PIPELINE_SLUG" == "auditbeat" || "$BUILDKITE_PIPELINE_SLUG" == "heartbeat" ]]; then
source .buildkite/env-scripts/env.sh
source .buildkite/env-scripts/util.sh
Expand All @@ -12,10 +33,17 @@ if [[ "$BUILDKITE_PIPELINE_SLUG" == "filebeat" || "$BUILDKITE_PIPELINE_SLUG" ==
fi
fi

if [[ "$BUILDKITE_PIPELINE_SLUG" == "beats-metricbeat" || "$BUILDKITE_PIPELINE_SLUG" == "beats-libbeat" || "$BUILDKITE_PIPELINE_SLUG" == "beats-packetbeat" || "$BUILDKITE_PIPELINE_SLUG" == "beats-winlogbeat" || "$BUILDKITE_PIPELINE_SLUG" == "beats-xpack-libbeat" ]]; then
if [[ "$BUILDKITE_PIPELINE_SLUG" == "beats-metricbeat" || "$BUILDKITE_PIPELINE_SLUG" == "beats-libbeat" || "$BUILDKITE_PIPELINE_SLUG" == "beats-packetbeat" || "$BUILDKITE_PIPELINE_SLUG" == "beats-winlogbeat" || "$BUILDKITE_PIPELINE_SLUG" == "beats-xpack-libbeat" || "$BUILDKITE_PIPELINE_SLUG" == "beats-xpack-metricbeat" ]]; then
source .buildkite/scripts/setenv.sh
if [[ "${BUILDKITE_COMMAND}" =~ ^buildkite-agent ]]; then
echo "Skipped pre-command when running the Upload pipeline"
exit 0
fi
fi

if [[ "$BUILDKITE_PIPELINE_SLUG" == "beats-xpack-metricbeat" && "$BUILDKITE_STEP_KEY" == "extended-cloud-test" ]]; then
BEATS_AWS_SECRET_KEY=$(retry 5 vault kv get -field secret_key ${AWS_SERVICE_ACCOUNT_SECRET_PATH})
export BEATS_AWS_SECRET_KEY
BEATS_AWS_ACCESS_KEY=$(retry 5 vault kv get -field access_key ${AWS_SERVICE_ACCOUNT_SECRET_PATH})
export BEATS_AWS_ACCESS_KEY
fi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After thinking a bit more about defineModuleFromTheChangeSet and how other places could use it. I think MODULE could be set in the pre-command hook.

Suggested change
fi
# Set the MODULE env variable
defineModuleFromTheChangeSet "${BEATS_PROJECT_NAME}"

Therefore my former suggestion for adding defineModuleFromTheChangeSet in .buildkite/scripts/cloud_tests.sh could be deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it here: c96b847
And if we move that right now to the pre-command, it will require moving to pre-command all dependencies, answered here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then, with this approach, the MODULE env variable won't be set in the other stages. Hence, it will take longer to run the tests.

If that's something to be done in phase2 that's totally fine with me. But PRs will be slow to be tested. Unless the defineModuleFromTheChangeSet "${BEATS_PROJECT_NAME}" is also called in all those tests using the withModule: true

24 changes: 24 additions & 0 deletions .buildkite/scripts/cloud_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

# What Terraform Module will run
export MODULE_DIR="x-pack/metricbeat/module/aws"

source .buildkite/scripts/install_tools.sh

set -euo pipefail

trap 'teardown || true; unset_secrets' EXIT

# Set the MODULE env variable if possible
defineModuleFromTheChangeSet "${BEATS_PROJECT_NAME}"

# Prepare the cloud resources using Terraform
startCloudTestEnv "${MODULE_DIR}"

# Run tests
echo "--- Run Cloud Tests for $BEATS_PROJECT_NAME"
pushd "${BEATS_PROJECT_NAME}" > /dev/null

mage build test

popd > /dev/null
171 changes: 158 additions & 13 deletions .buildkite/scripts/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ arch_type="$(uname -m)"
GITHUB_PR_TRIGGER_COMMENT=${GITHUB_PR_TRIGGER_COMMENT:-""}
GITHUB_PR_LABELS=${GITHUB_PR_LABELS:-""}
ONLY_DOCS=${ONLY_DOCS:-"true"}
OSS_MODULE_PATTERN="^[a-z0-9]+beat\\/module\\/([^\\/]+)\\/.*"
XPACK_MODULE_PATTERN="^x-pack\\/[a-z0-9]+beat\\/module\\/([^\\/]+)\\/.*"
[ -z "${run_libbeat+x}" ] && run_libbeat="$(buildkite-agent meta-data get run_libbeat --default "false")"
[ -z "${run_metricbeat+x}" ] && run_metricbeat="$(buildkite-agent meta-data get run_metricbeat --default "false")"
[ -z "${run_packetbeat+x}" ] && run_packetbeat="$(buildkite-agent meta-data get run_packetbeat --default "false")"
Expand All @@ -17,6 +19,9 @@ ONLY_DOCS=${ONLY_DOCS:-"true"}
[ -z "${run_packetbeat_arm_tests+x}" ] && run_packetbeat_arm_tests="$(buildkite-agent meta-data get run_packetbeat_arm_tests --default "false")"
[ -z "${run_metricbeat_macos_tests+x}" ] && run_metricbeat_macos_tests="$(buildkite-agent meta-data get run_metricbeat_macos_tests --default "false")"
[ -z "${run_packetbeat_macos_tests+x}" ] && run_packetbeat_macos_tests="$(buildkite-agent meta-data get run_packetbeat_macos_tests --default "false")"
[ -z "${run_xpack_libbeat_arm_tests+x}" ] && run_xpack_libbeat_arm_tests="$(buildkite-agent meta-data get run_xpack_libbeat_arm_tests --default "false")"
[ -z "${run_xpack_metricbeat_aws_tests+x}" ] && run_xpack_metricbeat_aws_tests="$(buildkite-agent meta-data get run_xpack_metricbeat_aws_tests --default "false")"
[ -z "${run_xpack_metricbeat_macos_tests+x}" ] && run_xpack_metricbeat_macos_tests="$(buildkite-agent meta-data get run_xpack_metricbeat_macos_tests --default "false")"

metricbeat_changeset=(
"^metricbeat/.*"
Expand Down Expand Up @@ -81,6 +86,30 @@ packaging_changeset=(
".go-version"
)

case "${BUILDKITE_PIPELINE_SLUG}" in
"beats-metricbeat")
BEAT_CHANGESET_REFERENCE=${metricbeat_changeset[@]}
;;
"beats-libbeat")
BEAT_CHANGESET_REFERENCE=${libbeat_changeset[@]}
;;
"beats-packetbeat")
BEAT_CHANGESET_REFERENCE=${packetbeat_changeset[@]}
;;
"beats-winlogbeat")
BEAT_CHANGESET_REFERENCE=${winlogbeat_changeset[@]}
;;
"beats-xpack-libbeat")
BEAT_CHANGESET_REFERENCE=${xpack_libbeat_changeset[@]}
;;
"beats-xpack-metricbeat")
BEAT_CHANGESET_REFERENCE=${xpack_metricbeat_changeset[@]}
;;
*)
echo "The changeset for the ${BUILDKITE_PIPELINE_SLUG} pipeline hasn't been defined yet."
;;
esac

check_and_set_beat_vars() {
if [[ -n "$BEATS_PROJECT_NAME" && "$BEATS_PROJECT_NAME" == *"x-pack/"* ]]; then
BEATS_XPACK_PROJECT_NAME=${BEATS_PROJECT_NAME//-/} #remove -
Expand All @@ -89,8 +118,8 @@ check_and_set_beat_vars() {
BEATS_GH_LABEL=${BEATS_XPACK_LABEL_PROJECT_NAME}
TRIGGER_SPECIFIC_BEAT="run_${BEATS_XPACK_PROJECT_NAME}"
TRIGGER_SPECIFIC_ARM_TESTS="run_${BEATS_XPACK_PROJECT_NAME}_arm_tests"
TRIGGER_SPECIFIC_AWS_TESTS="run_${BEATS_XPACK_PROJECT_NAME}_aws_tests"
TRIGGER_SPECIFIC_MACOS_TESTS="run_${BEATS_XPACK_PROJECT_NAME}_macos_tests"
declare -n BEAT_CHANGESET_REFERENCE="${BEATS_XPACK_PROJECT_NAME}_changeset"
sharbuz marked this conversation as resolved.
Show resolved Hide resolved
echo "Beats project name is $BEATS_XPACK_PROJECT_NAME"
mandatory_changeset=(
"${BEAT_CHANGESET_REFERENCE[@]}"
Expand All @@ -101,8 +130,8 @@ check_and_set_beat_vars() {
BEATS_GH_LABEL=${BEATS_PROJECT_NAME}
TRIGGER_SPECIFIC_BEAT="run_${BEATS_PROJECT_NAME}"
TRIGGER_SPECIFIC_ARM_TESTS="run_${BEATS_PROJECT_NAME}_arm_tests"
TRIGGER_SPECIFIC_AWS_TESTS="run_${BEATS_PROJECT_NAME}_aws_tests"
TRIGGER_SPECIFIC_MACOS_TESTS="run_${BEATS_PROJECT_NAME}_macos_tests"
declare -n BEAT_CHANGESET_REFERENCE="${BEATS_PROJECT_NAME}_changeset"
echo "Beats project name is $BEATS_PROJECT_NAME"
mandatory_changeset=(
"${BEAT_CHANGESET_REFERENCE[@]}"
Expand All @@ -113,8 +142,10 @@ check_and_set_beat_vars() {
BEATS_GH_COMMENT="/test ${BEATS_PROJECT_NAME}"
BEATS_GH_MACOS_COMMENT="${BEATS_GH_COMMENT} for macos"
BEATS_GH_ARM_COMMENT="${BEATS_GH_COMMENT} for arm"
BAETS_GH_MACOS_LABEL="macOS"
BAETS_GH_ARM_LABEL="arm"
BEATS_GH_AWS_COMMENT="${BEATS_GH_COMMENT} for aws cloud"
BEATS_GH_MACOS_LABEL="macOS"
BEATS_GH_ARM_LABEL="arm"
BEATS_GH_AWS_LABEL="aws"
}

with_docker_compose() {
Expand All @@ -127,6 +158,19 @@ with_docker_compose() {
docker-compose version
}

with_Terraform() {
echo "Setting up the Terraform environment..."
local path_to_file="${WORKSPACE}/terraform.zip"
create_workspace
check_platform_architeture
retry 5 curl -sSL -o ${path_to_file} "https://releases.hashicorp.com/terraform/${ASDF_TERRAFORM_VERSION}/terraform_${ASDF_TERRAFORM_VERSION}_${platform_type_lowercase}_${go_arch_type}.zip"
unzip -q ${path_to_file} -d ${BIN}/
rm ${path_to_file}
chmod +x ${BIN}/terraform
export PATH="${BIN}:${PATH}"
terraform version
}

create_workspace() {
if [[ ! -d "${BIN}" ]]; then
mkdir -p "${BIN}"
Expand Down Expand Up @@ -168,6 +212,8 @@ with_mage() {
for pkg in "${install_packages[@]}"; do
go install "${pkg}@latest"
done
echo "Download modules to local cache"
retry 3 go mod download
}

with_go() {
Expand Down Expand Up @@ -283,9 +329,15 @@ are_paths_changed() {

are_changed_only_paths() {
local patterns=("${@}")
local changelist=()
local changed_files=$(git diff --name-only HEAD@{1} HEAD)
if [ -z "$changed_files" ] || grep -qE "$(IFS=\|; echo "${patterns[*]}")" <<< "$changed_files"; then
local changed_files=($(git diff --name-only HEAD@{1} HEAD))
local matched_files=()
for pattern in "${patterns[@]}"; do
local matched=($(grep -E "${pattern}" <<< "${changed_files[@]}"))
if [ "${#matched[@]}" -gt 0 ]; then
matched_files+=("${matched[@]}")
fi
done
if [ "${#matched_files[@]}" -eq "${#changed_files[@]}" ] || [ "${#changed_files[@]}" -eq 0 ]; then
return 0
fi
return 1
Expand All @@ -301,7 +353,7 @@ are_conditions_met_mandatory_tests() {
are_conditions_met_arm_tests() {
if are_conditions_met_mandatory_tests; then #from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/Jenkinsfile#L145-L171
if [[ "$BUILDKITE_PIPELINE_SLUG" == "beats-libbeat" || "$BUILDKITE_PIPELINE_SLUG" == "beats-packetbeat" ]]; then
if [[ "${GITHUB_PR_TRIGGER_COMMENT}" == "${BEATS_GH_ARM_COMMENT}" || "${GITHUB_PR_LABELS}" =~ "${BAETS_GH_ARM_LABEL}" || "${!TRIGGER_SPECIFIC_ARM_TESTS}" == "true" ]]; then
if [[ "${GITHUB_PR_TRIGGER_COMMENT}" == "${BEATS_GH_ARM_COMMENT}" || "${GITHUB_PR_LABELS}" =~ ${BEATS_GH_ARM_LABEL} || "${!TRIGGER_SPECIFIC_ARM_TESTS}" == "true" ]]; then
return 0
fi
fi
Expand All @@ -311,8 +363,19 @@ are_conditions_met_arm_tests() {

are_conditions_met_macos_tests() {
if are_conditions_met_mandatory_tests; then #from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/Jenkinsfile#L145-L171
if [[ "$BUILDKITE_PIPELINE_SLUG" == "beats-metricbeat" || "$BUILDKITE_PIPELINE_SLUG" == "beats-packetbeat" ]]; then
if [[ "${GITHUB_PR_TRIGGER_COMMENT}" == "${BEATS_GH_MACOS_COMMENT}" || "${GITHUB_PR_LABELS}" =~ "${BAETS_GH_MACOS_LABEL}" || "${!TRIGGER_SPECIFIC_MACOS_TESTS}" == "true" ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/metricbeat/Jenkinsfile.yml#L3-L12
if [[ "$BUILDKITE_PIPELINE_SLUG" == "beats-metricbeat" || "$BUILDKITE_PIPELINE_SLUG" == "beats-packetbeat" || "$BUILDKITE_PIPELINE_SLUG" == "beats-xpack-metricbeat" ]]; then
if [[ "${GITHUB_PR_TRIGGER_COMMENT}" == "${BEATS_GH_MACOS_COMMENT}" || "${GITHUB_PR_LABELS}" =~ ${BEATS_GH_MACOS_LABEL} || "${!TRIGGER_SPECIFIC_MACOS_TESTS}" == "true" ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/metricbeat/Jenkinsfile.yml#L3-L12
return 0
fi
fi
fi
return 1
}

are_conditions_met_aws_tests() {
if are_conditions_met_mandatory_tests; then #from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/Jenkinsfile#L145-L171
if [[ "$BUILDKITE_PIPELINE_SLUG" == "beats-xpack-metricbeat" ]]; then
if [[ "${GITHUB_PR_TRIGGER_COMMENT}" == "${BEATS_GH_AWS_COMMENT}" || "${GITHUB_PR_LABELS}" =~ ${BEATS_GH_AWS_LABEL} || "${!TRIGGER_SPECIFIC_AWS_TESTS}" == "true" ]]; then # from https://github.com/elastic/beats/blob/c5e79a25d05d5bdfa9da4d187fe89523faa42afc/metricbeat/Jenkinsfile.yml#L3-L12
return 0
fi
fi
Expand All @@ -336,19 +399,101 @@ config_git() {
fi
}

defineModuleFromTheChangeSet() {
# This method gathers the module name, if required, in order to run the ITs only if the changeset affects a specific module.
# For such, it's required to look for changes under the module folder and exclude anything else such as asciidoc and png files.
# This method defines and exports the MODULE variable with a particular module name or '' if changeset doesn't affect a specific module
local project_path=$1
local project_path_transformed=$(echo "$project_path" | sed 's/\//\\\//g')
local project_path_exclussion="((?!^${project_path_transformed}\\/).)*\$"
local exclude=("^(${project_path_exclussion}|((?!\\/module\\/).)*\$|.*\\.asciidoc|.*\\.png)")

if [[ "$project_path" == *"x-pack/"* ]]; then
local pattern=("$XPACK_MODULE_PATTERN")
else
local pattern=("$OSS_MODULE_PATTERN")
fi
local changed_modules=""
local module_dirs=$(find "$project_path/module" -mindepth 1 -maxdepth 1 -type d)
for module_dir in $module_dirs; do
if are_paths_changed $module_dir && ! are_changed_only_paths "${exclude[@]}"; then
if [[ -z "$changed_modules" ]]; then
changed_modules=$(basename "$module_dir")
else
changed_modules+=",$(basename "$module_dir")"
fi
fi
done
if [[ -z "$changed_modules" ]]; then # TODO: remove this condition and uncomment the line below when the issue https://github.com/elastic/ingest-dev/issues/2993 is solved
export MODULE="aws"
else
export MODULE="${changed_modules}" # TODO: remove this line and uncomment the line below when the issue https://github.com/elastic/ingest-dev/issues/2993 is solved
# export MODULE="${changed_modules}" # TODO: uncomment the line when the issue https://github.com/elastic/ingest-dev/issues/2993 is solved
fi
}

terraformInit() {
local dir=$1
echo "Terraform Init on $dir"
pushd "${dir}" > /dev/null
terraform init
popd > /dev/null
}

withAWS() {
# This method gathers the masked AWS credentials from pre-command hook and sets the right AWS variable names.
export AWS_ACCESS_KEY_ID=$BEATS_AWS_ACCESS_KEY
v1v marked this conversation as resolved.
Show resolved Hide resolved
export AWS_SECRET_ACCESS_KEY=$BEATS_AWS_SECRET_KEY
export TEST_TAGS="${TEST_TAGS:+$TEST_TAGS,}aws"
}

startCloudTestEnv() {
local dir=$1
withAWS
echo "--- Run docker-compose services for emulated cloud env"
docker-compose -f .ci/jobs/docker-compose.yml up -d #TODO: move all docker-compose files from the .ci to .buildkite folder before switching to BK
with_Terraform
terraformInit "$dir"
export TF_VAR_BRANCH=$(echo "${BUILDKITE_BRANCH}" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g')
export TF_VAR_BUILD_ID="${BUILDKITE_BUILD_ID}"
export TF_VAR_CREATED_DATE=$(date +%s)
export TF_VAR_ENVIRONMENT="ci"
export TF_VAR_REPO="${REPO}"
pushd "${dir}" > /dev/null
terraform apply -auto-approve
popd > /dev/null
}

teardown() {
# Teardown resources after using them
echo "---Terraform Cleanup"
.ci/scripts/terraform-cleanup.sh "${MODULE_DIR}" #TODO: move all docker-compose files from the .ci to .buildkite folder before switching to BK
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, there is a missing context what's the reason for running this terraform clean up function. A comment can help in the future.
In addition, should this particular script run only if the stage cloud ran for the x-pack/metricbeat? Or will it work regardless of the stages in all the beats in case they call the cleanup function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of these functions will be run only when it's needed, for example: https://github.com/sharbuz/beats/blob/25547b4ef8c75e9c356573b39e0bc25c0bf5e022/.buildkite/scripts/cloud_tests.sh#L7
It will run when the script finishes with EXIT code, which means when it finishes successfully/unsuccessfully

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do understand the trap function.
My main concern is regarding the cleanup function itself. If the pipeline finishes with exit 0, then cleanup will run, but if it fails, will the failure be reported, too? I'm not sure if that's what we want. Post-build steps for tearing down resources should not fail the build but make it unstable or notify the cloud resources manager with a message, IMO.

My team enabled a cloud-reaper mechanism that runs async to delete any leftovers in any of the cloud providers we use. That's how we can avoid having stalled resources and failing builds if the cleanup failed.

Copy link
Contributor Author

@sharbuz sharbuz Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got what you mean, In this case I would suggest using the additional "cleanup" step with Slack notification or pipeline with email notification. @v1v WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To illustrate a bit more, the current implementation in Jenkins does not fail when tearing down the resources with terraform/docker:

  • beats/Jenkinsfile

    Lines 945 to 950 in 3f46222

    dirs?.each { folder ->
    // If it failed then cleanup without failing the build
    sh(label: 'Terraform Cleanup', script: ".ci/scripts/terraform-cleanup.sh ${folder}", returnStatus: true)
    }
    // Cleanup the docker services
    sh(label: 'Docker Compose Cleanup', script: ".ci/scripts/docker-services-cleanup.sh", returnStatus: true)

It uses returnStatus: true that means do nothing, see

returnStatus : boolean (optional)
Normally, a script which exits with a nonzero status code will cause the step to fail with an exception. If this option is checked, the return value of the step will instead be the status code. You may then compare it to zero, for example.

See https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#sh-shell-script

You can get the same:
either you can || true or use your additional cleanup with soft_fail in Buildkite.

WDYT?

Sounds good to me. But let me ask @dliappis about his preference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done: f432822


echo "---Docker Compose Cleanup"
docker-compose -f .ci/jobs/docker-compose.yml down -v #TODO: move all docker-compose files from the .ci to .buildkite folder before switching to BK
v1v marked this conversation as resolved.
Show resolved Hide resolved
}

unset_secrets () {
for var in $(printenv | sed 's;=.*;;' | sort); do
if [[ "$var" == AWS_* || "$var" == BEATS_AWS_* ]]; then
unset "$var"
fi
done
}

if ! are_changed_only_paths "${docs_changeset[@]}" ; then
ONLY_DOCS="false"
export ONLY_DOCS="false"
echo "Changes include files outside the docs_changeset vairiabe. ONLY_DOCS=$ONLY_DOCS."
else
echo "All changes are related to DOCS. ONLY_DOCS=$ONLY_DOCS."
fi

if are_paths_changed "${go_mod_changeset[@]}" ; then
GO_MOD_CHANGES="true"
export GO_MOD_CHANGES="true"
fi

if are_paths_changed "${packaging_changeset[@]}" ; then
PACKAGING_CHANGES="true"
export PACKAGING_CHANGES="true"
fi

check_and_set_beat_vars
4 changes: 2 additions & 2 deletions .buildkite/scripts/generate_metricbeat_pipeline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ if are_conditions_met_packaging; then
image: "${IMAGE_UBUNTU_X86_64}"
machineType: "${GCP_HI_PERF_MACHINE_TYPE}"
env:
PLATFORMS: "+all linux/amd64 linux/arm64 windows/amd64 darwin/amd64 darwin/arm64"
PLATFORMS: "${PACKAGING_PLATFORMS}"

- label: ":linux: Packaging ARM"
key: "packaging-arm"
Expand All @@ -159,7 +159,7 @@ if are_conditions_met_packaging; then
imagePrefix: "${IMAGE_UBUNTU_ARM_64}"
instanceType: "${AWS_ARM_INSTANCE_TYPE}"
env:
PLATFORMS: "linux/arm64"
PLATFORMS: "${PACKAGING_ARM_PLATFORMS}"
PACKAGES: "docker"

YAML
Expand Down
6 changes: 3 additions & 3 deletions .buildkite/scripts/generate_packetbeat_pipeline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ else
exit 0
fi

if are_conditions_met_arm_tests && are_conditions_met_macos_tests; then
if are_conditions_met_arm_tests || are_conditions_met_macos_tests; then
cat >> $pipelineName <<- YAML

- group: "Extended Tests"
Expand Down Expand Up @@ -150,7 +150,7 @@ if are_conditions_met_packaging; then
image: "${IMAGE_UBUNTU_X86_64}"
machineType: "${GCP_HI_PERF_MACHINE_TYPE}"
env:
PLATFORMS: "+all linux/amd64 linux/arm64 windows/amd64 darwin/amd64 darwin/arm64"
PLATFORMS: "${PACKAGING_PLATFORMS}"

- label: ":linux: Packaging ARM"
key: "packaging-arm"
Expand All @@ -160,7 +160,7 @@ if are_conditions_met_packaging; then
imagePrefix: "${IMAGE_UBUNTU_ARM_64}"
instanceType: "${AWS_ARM_INSTANCE_TYPE}"
env:
PLATFORMS: "linux/arm64"
PLATFORMS: "${PACKAGING_ARM_PLATFORMS}"
PACKAGES: "docker"

YAML
Expand Down
2 changes: 1 addition & 1 deletion .buildkite/scripts/generate_winlogbeat_pipeline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ if are_conditions_met_packaging; then
image: "${IMAGE_UBUNTU_X86_64}"
machineType: "${GCP_HI_PERF_MACHINE_TYPE}"
env:
PLATFORMS: "+all linux/amd64 linux/arm64 windows/amd64 darwin/amd64 darwin/arm64"
PLATFORMS: "${PACKAGING_PLATFORMS}"


YAML
Expand Down
Loading
Loading