debian-mirror-gitlab/app/services/clusters/cleanup/project_namespace_service.rb

49 lines
1.7 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
module Clusters
module Cleanup
2021-09-04 01:27:46 +05:30
class ProjectNamespaceService < ::Clusters::Cleanup::BaseService
2020-01-01 13:55:28 +05:30
KUBERNETES_NAMESPACE_BATCH_SIZE = 100
def execute
delete_project_namespaces_in_batches
# Keep calling the worker untill all namespaces are deleted
if cluster.kubernetes_namespaces.exists?
return schedule_next_execution(Clusters::Cleanup::ProjectNamespaceWorker)
end
cluster.continue_cleanup!
end
private
def delete_project_namespaces_in_batches
kubernetes_namespaces_batch = cluster.kubernetes_namespaces.first(KUBERNETES_NAMESPACE_BATCH_SIZE)
kubernetes_namespaces_batch.each do |kubernetes_namespace|
log_event(:deleting_project_namespace, namespace: kubernetes_namespace.namespace)
begin
kubeclient_delete_namespace(kubernetes_namespace)
2022-01-26 12:08:38 +05:30
rescue Kubeclient::HttpError => e
# unauthorized, forbidden: GitLab's access has been revoked
# certificate verify failed: Cluster is probably gone forever
raise unless e.message =~ /unauthorized|forbidden|certificate verify failed/i
2020-01-01 13:55:28 +05:30
end
kubernetes_namespace.destroy!
end
end
def kubeclient_delete_namespace(kubernetes_namespace)
2021-12-11 22:18:48 +05:30
cluster.kubeclient&.delete_namespace(kubernetes_namespace.namespace)
2020-01-01 13:55:28 +05:30
rescue Kubeclient::ResourceNotFoundError
2021-12-11 22:18:48 +05:30
# The resources have already been deleted, possibly on a previous attempt that timed out
rescue Gitlab::UrlBlocker::BlockedUrlError
# User gave an invalid cluster from the start, or deleted the endpoint before this job ran
2020-01-01 13:55:28 +05:30
end
end
end
end