Commit 26a7650b authored by Igor Drozdov's avatar Igor Drozdov

Merge branch '30678-the-docs-review-app-environments-are-not-being-cleaned-up' into 'master'

ci: Delete stopped environments not updated since specific threshold

See merge request gitlab-org/gitlab!70817
parents e51a3eae daa23387
...@@ -8,6 +8,10 @@ class AutomatedCleanup ...@@ -8,6 +8,10 @@ class AutomatedCleanup
attr_reader :project_path, :gitlab_token attr_reader :project_path, :gitlab_token
DEPLOYMENTS_PER_PAGE = 100 DEPLOYMENTS_PER_PAGE = 100
ENVIRONMENT_PREFIX = {
review_app: 'review/',
docs_review_app: 'review-docs/'
}.freeze
IGNORED_HELM_ERRORS = [ IGNORED_HELM_ERRORS = [
'transport is closing', 'transport is closing',
'error upgrading connection', 'error upgrading connection',
...@@ -62,13 +66,14 @@ class AutomatedCleanup ...@@ -62,13 +66,14 @@ class AutomatedCleanup
releases_to_delete = [] releases_to_delete = []
# Delete environments via deployments
gitlab.deployments(project_path, per_page: DEPLOYMENTS_PER_PAGE, sort: 'desc').auto_paginate do |deployment| 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 break if Time.parse(deployment.created_at) < deployments_look_back_threshold
environment = deployment.environment environment = deployment.environment
next unless environment next unless environment
next unless environment.name.start_with?('review/') next unless environment.name.start_with?(ENVIRONMENT_PREFIX[:review_app])
next if checked_environments.include?(environment.slug) next if checked_environments.include?(environment.slug)
last_deploy = deployment.created_at last_deploy = deployment.created_at
...@@ -92,6 +97,10 @@ class AutomatedCleanup ...@@ -92,6 +97,10 @@ class AutomatedCleanup
checked_environments << environment.slug checked_environments << environment.slug
end end
delete_stopped_environments(environment_type: :review_app, checked_environments: checked_environments, last_updated_threshold: delete_threshold) do |environment|
releases_to_delete << Tooling::Helm3Client::Release.new(environment.slug, 1, environment.updated_at, nil, nil, review_apps_namespace)
end
delete_helm_releases(releases_to_delete) delete_helm_releases(releases_to_delete)
end end
...@@ -102,14 +111,12 @@ class AutomatedCleanup ...@@ -102,14 +111,12 @@ class AutomatedCleanup
stop_threshold = threshold_time(days: days_for_stop) stop_threshold = threshold_time(days: days_for_stop)
delete_threshold = threshold_time(days: days_for_delete) delete_threshold = threshold_time(days: days_for_delete)
max_delete_count = 1000 # Delete environments via deployments
delete_count = 0
gitlab.deployments(project_path, per_page: DEPLOYMENTS_PER_PAGE, sort: 'desc').auto_paginate do |deployment| gitlab.deployments(project_path, per_page: DEPLOYMENTS_PER_PAGE, sort: 'desc').auto_paginate do |deployment|
environment = deployment.environment environment = deployment.environment
next unless environment next unless environment
next unless environment.name.start_with?('review-docs/') next unless environment.name.start_with?(ENVIRONMENT_PREFIX[:docs_review_app])
next if checked_environments.include?(environment.slug) next if checked_environments.include?(environment.slug)
last_deploy = deployment.created_at last_deploy = deployment.created_at
...@@ -120,15 +127,12 @@ class AutomatedCleanup ...@@ -120,15 +127,12 @@ class AutomatedCleanup
stop_environment(environment, deployment) if environment_state && environment_state != 'stopped' stop_environment(environment, deployment) if environment_state && environment_state != 'stopped'
end end
if deployed_at < delete_threshold delete_environment(environment, deployment) if deployed_at < delete_threshold
delete_environment(environment, deployment)
delete_count += 1
break if delete_count > max_delete_count
end
checked_environments << environment.slug checked_environments << environment.slug
end end
delete_stopped_environments(environment_type: :docs_review_app, checked_environments: checked_environments, last_updated_threshold: delete_threshold)
end end
def perform_helm_releases_cleanup!(days:) def perform_helm_releases_cleanup!(days:)
...@@ -171,8 +175,9 @@ class AutomatedCleanup ...@@ -171,8 +175,9 @@ class AutomatedCleanup
nil nil
end end
def delete_environment(environment, deployment) def delete_environment(environment, deployment = nil)
print_release_state(subject: 'Review app', release_name: environment.slug, release_date: deployment.created_at, action: 'deleting') release_date = deployment ? deployment.created_at : environment.updated_at
print_release_state(subject: 'Review app', release_name: environment.slug, release_date: release_date, action: 'deleting')
gitlab.delete_environment(project_path, environment.id) gitlab.delete_environment(project_path, environment.id)
rescue Gitlab::Error::Forbidden rescue Gitlab::Error::Forbidden
...@@ -187,6 +192,24 @@ class AutomatedCleanup ...@@ -187,6 +192,24 @@ class AutomatedCleanup
puts "Review app '#{environment.name}' / '#{environment.slug}' (##{environment.id}) is forbidden: skipping it" puts "Review app '#{environment.name}' / '#{environment.slug}' (##{environment.id}) is forbidden: skipping it"
end end
def delete_stopped_environments(environment_type:, checked_environments:, last_updated_threshold:)
gitlab.environments(project_path, per_page: DEPLOYMENTS_PER_PAGE, sort: 'desc', states: 'stopped', search: ENVIRONMENT_PREFIX[environment_type]).auto_paginate do |environment|
next if skip_environment?(environment: environment, checked_environments: checked_environments, last_updated_threshold: delete_threshold, environment_type: environment_type)
yield environment if delete_environment(environment)
checked_environments << environment.slug
end
end
def skip_environment?(environment:, checked_environments:, last_updated_threshold:, environment_type:)
return true unless environment.name.start_with?(ENVIRONMENT_PREFIX[environment_type])
return true if checked_environments.include?(environment.slug)
return true if Time.parse(environment.updated_at) > last_updated_threshold
false
end
def helm_releases def helm_releases
args = ['--all', '--date'] args = ['--all', '--date']
......
...@@ -19,7 +19,7 @@ module Tooling ...@@ -19,7 +19,7 @@ module Tooling
end end
def last_update def last_update
@last_update ||= Time.parse(self[:last_update]) @last_update ||= self[:last_update] ? Time.parse(self[:last_update]) : nil
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment