Skip to content

[CI] Allow to run each cloud cleanup step independently in DRY_RUN false mode #2581

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

Merged
merged 9 commits into from
May 8, 2025
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
10 changes: 9 additions & 1 deletion .buildkite/pipeline.cloud-cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ steps:
- label: "False"
value: "false"
default: "true"
- select: "DRY_RUN (Deprecated step)"
key: "DRY_RUN_DEPRECATED"
options:
- label: "True"
value: "true"
- label: "False"
value: "false"
default: "true"
if: "build.source == 'ui'"

- wait: ~
Expand All @@ -42,7 +50,7 @@ steps:
command: ".buildkite/scripts/cloud-cleanup.sh"
env:
RESOURCE_RETENTION_PERIOD: "24 hours"
DRY_RUN: "${DRY_RUN:-true}"
DRY_RUN: "${DRY_RUN_DEPRECATED:-true}"
agents:
provider: "gcp" # this step requires docker

Expand Down
21 changes: 19 additions & 2 deletions .buildkite/scripts/cloud-cleanup-oidc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,38 @@ DRY_RUN="$(buildkite-agent meta-data get DRY_RUN --default "${DRY_RUN:-"true"}")
resources_to_delete=0

COMMAND="validate"
redshift_message=""
if [[ "${DRY_RUN}" != "true" ]]; then
# TODO: to be changed to "destroy --confirm" once it can be tested
# that filters work as expected
COMMAND="plan"
redshift_message=" - stale redshift clusters will be deleted"
else
COMMAND="plan"
fi

buildkite-agent annotate \
"[${BUILDKITE_STEP_KEY}] Running DRY_RUN (${DRY_RUN}) using cloud-reaper command \"${COMMAND}\"${redshift_message}" \
--context "ctx-cloud-reaper-info" \
--style "info"

any_resources_to_delete() {
local file=$1
local number=0
# First three lines are like:
# ⇒ Loading configuration...
# ✓ Succeeded to load configuration
# Scanning resources... ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00

# FIXME:: When running with "destroy --confirm" there could be more lines.
# In the case, there is nothing to delete, there is one more line:
# ⇒ Nothing to destroy !
# but there are no examples when resources are deleted to add the required logic
if [[ "${DRY_RUN}" == false ]] ; then
if tail -n 1 ${file} | grep -q "Nothing to destroy" ; then
return 1
fi
fi
number=$(tail -n +4 "${file}" | wc -l)
if [ "${number}" -eq 0 ]; then
return 1
Expand Down Expand Up @@ -86,7 +103,7 @@ cloud_reaper_aws() {
echo "--- Configuring ephemeral user"
create_aws_ephemeral_user

echo "Validating configuration"
echo "--- Validating configuration"
docker run --rm -v "$(pwd)/.buildkite/configs/cleanup.aws.yml":/etc/cloud-reaper/config.yml \
-e AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID_EPHEMERAL" \
-e AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY_EPHEMERAL" \
Expand All @@ -98,7 +115,7 @@ cloud_reaper_aws() {
--config /etc/cloud-reaper/config.yml \
validate

echo "Scanning resources"
echo "--- Scanning resources"
docker run --rm -v "$(pwd)/.buildkite/configs/cleanup.aws.yml":/etc/cloud-reaper/config.yml \
-e AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID_EPHEMERAL" \
-e AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY_EPHEMERAL" \
Expand Down
23 changes: 20 additions & 3 deletions .buildkite/scripts/cloud-cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,43 @@ export DELETE_RESOURCES_BEFORE_DATE

CLOUD_REAPER_IMAGE="${DOCKER_REGISTRY}/observability-ci/cloud-reaper:0.3.0"

DRY_RUN="$(buildkite-agent meta-data get DRY_RUN --default "${DRY_RUN:-"true"}")"
DRY_RUN="$(buildkite-agent meta-data get DRY_RUN_DEPRECATED --default "${DRY_RUN:-"true"}")"

resources_to_delete=0

COMMAND="validate"
redshift_message=""
if [[ "${DRY_RUN}" != "true" ]]; then
# TODO: to be changed to "destroy --confirm" once it can be tested
# that filters work as expected
COMMAND="plan"
redshift_message=" - stale redshift clusters will be deleted"
else
COMMAND="plan"
fi

buildkite-agent annotate \
"[${BUILDKITE_STEP_KEY}] Running DRY_RUN (${DRY_RUN}) using cloud-reaper command \"${COMMAND}\"${redshift_message}" \
--context "ctx-cloud-reaper-info-deprecated" \
--style "info"

any_resources_to_delete() {
local file=$1
local number=0
# First three lines are like:
# ⇒ Loading configuration...
# ✓ Succeeded to load configuration
# Scanning resources... ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00

# FIXME:: When running with "destroy --confirm" command there could be more lines.
# In the case, there is nothing to delete, there is one more line:
# ⇒ Nothing to destroy !
# but there are no examples when resources are deleted to add the required logic
if [[ "${DRY_RUN}" == false ]] ; then
if tail -n 1 ${file} | grep -q "Nothing to destroy" ; then
return 1
fi
fi
number=$(tail -n +4 "${file}" | wc -l)
if [ "${number}" -eq 0 ]; then
return 1
Expand All @@ -53,7 +70,7 @@ any_resources_to_delete() {
}

cloud_reaper_aws() {
echo "Validating configuration"
echo "--- Validating configuration"
docker run --rm -v "$(pwd)/.buildkite/configs/cleanup.aws.yml":/etc/cloud-reaper/config.yml \
-e AWS_SECRET_ACCESS_KEY="${ELASTIC_PACKAGE_AWS_SECRET_KEY}" \
-e AWS_ACCESS_KEY_ID="${ELASTIC_PACKAGE_AWS_ACCESS_KEY}" \
Expand All @@ -64,7 +81,7 @@ cloud_reaper_aws() {
--config /etc/cloud-reaper/config.yml \
validate

echo "Scanning resources"
echo "--- Scanning resources"
docker run --rm -v "$(pwd)/.buildkite/configs/cleanup.aws.yml":/etc/cloud-reaper/config.yml \
-e AWS_SECRET_ACCESS_KEY="${ELASTIC_PACKAGE_AWS_SECRET_KEY}" \
-e AWS_ACCESS_KEY_ID="${ELASTIC_PACKAGE_AWS_ACCESS_KEY}" \
Expand Down