diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-01-25 21:08:56 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-01-25 21:08:56 +0300 |
commit | ff549ec680715e4ea1daf0cee457f29dfe3b6062 (patch) | |
tree | 823fc28718a1278025ee2d88c1368958befec4da /scripts/review_apps | |
parent | ec558ad8ed732ff6f8a89aa3651eb92c27c50deb (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'scripts/review_apps')
-rwxr-xr-x | scripts/review_apps/automated_cleanup.rb | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/scripts/review_apps/automated_cleanup.rb b/scripts/review_apps/automated_cleanup.rb index 07444fbd850..6ab42c5877a 100755 --- a/scripts/review_apps/automated_cleanup.rb +++ b/scripts/review_apps/automated_cleanup.rb @@ -14,6 +14,7 @@ module ReviewApps review_app: 'review/', docs_review_app: 'review-docs/' }.freeze + HELM_ALLOWED_NAMESPACES_REGEX = /^review-(?!apps).+/.freeze IGNORED_HELM_ERRORS = [ 'transport is closing', 'error upgrading connection', @@ -64,19 +65,21 @@ module ReviewApps @kubernetes ||= Tooling::KubernetesClient.new(namespace: review_apps_namespace) end - def perform_gitlab_environment_cleanup!(days_for_stop:, days_for_delete:) - puts "Checking for Review Apps not updated in the last #{days_for_stop} days..." + def perform_gitlab_environment_cleanup!(days_for_delete:) + puts "Checking for Review Apps not updated in the last #{days_for_delete} days..." checked_environments = [] delete_threshold = threshold_time(days: days_for_delete) - stop_threshold = threshold_time(days: days_for_stop) deployments_look_back_threshold = threshold_time(days: days_for_delete * 5) releases_to_delete = [] # Delete environments via deployments gitlab.deployments(project_path, per_page: DEPLOYMENTS_PER_PAGE, sort: 'desc').auto_paginate do |deployment| - break if Time.parse(deployment.created_at) < deployments_look_back_threshold + last_deploy = deployment.created_at + deployed_at = Time.parse(last_deploy) + + break if deployed_at < deployments_look_back_threshold environment = deployment.environment @@ -84,20 +87,13 @@ module ReviewApps next unless environment.name.start_with?(ENVIRONMENT_PREFIX[:review_app]) next if checked_environments.include?(environment.slug) - last_deploy = deployment.created_at - deployed_at = Time.parse(last_deploy) - if deployed_at < delete_threshold deleted_environment = delete_environment(environment, deployment) + if deleted_environment release = Tooling::Helm3Client::Release.new(environment.slug, 1, deployed_at.to_s, nil, nil, environment.slug) releases_to_delete << release end - elsif deployed_at >= stop_threshold - print_release_state(subject: 'Review App', release_name: environment.slug, release_date: last_deploy, action: 'leaving') - else - environment_state = fetch_environment(environment)&.state - stop_environment(environment, deployment) if environment_state && environment_state != 'stopped' end checked_environments << environment.slug @@ -116,18 +112,21 @@ module ReviewApps checked_environments = [] stop_threshold = threshold_time(days: days_for_stop) delete_threshold = threshold_time(days: days_for_delete) + deployments_look_back_threshold = threshold_time(days: days_for_delete * 5) # Delete environments via deployments gitlab.deployments(project_path, per_page: DEPLOYMENTS_PER_PAGE, sort: 'desc').auto_paginate do |deployment| + last_deploy = deployment.created_at + deployed_at = Time.parse(last_deploy) + + break if deployed_at < deployments_look_back_threshold + environment = deployment.environment next unless environment next unless environment.name.start_with?(ENVIRONMENT_PREFIX[:docs_review_app]) next if checked_environments.include?(environment.slug) - last_deploy = deployment.created_at - deployed_at = Time.parse(last_deploy) - if deployed_at < stop_threshold environment_state = fetch_environment(environment)&.state stop_environment(environment, deployment) if environment_state && environment_state != 'stopped' @@ -150,6 +149,7 @@ module ReviewApps helm_releases.each do |release| # Prevents deleting `dns-gitlab-review-app` releases or other unrelated releases + next unless HELM_ALLOWED_NAMESPACES_REGEX.match?(release.namespace) next unless release.name.start_with?('review-') if release.status == 'failed' || release.last_update < threshold @@ -228,7 +228,7 @@ module ReviewApps end def helm_releases - args = ['--all', '--date'] + args = ['--all', '--all-namespaces', '--date'] helm.releases(args: args) end @@ -304,10 +304,20 @@ if $PROGRAM_NAME == __FILE__ automated_cleanup.perform_helm_releases_cleanup!(days: 2) end + puts + + timed('Review Apps cleanup') do + automated_cleanup.perform_gitlab_environment_cleanup!(days_for_delete: 3) + end + + puts + timed('Stale Namespace cleanup') do automated_cleanup.perform_stale_namespace_cleanup!(days: 3) end + puts + timed('Stale PVC cleanup') do automated_cleanup.perform_stale_pvc_cleanup!(days: 30) end |