Commit 95ee0f55 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch 'fix-clusters-service-account-cleanup-errors' into 'master'

Reduce noise in cluster cleanup workers

See merge request gitlab-org/gitlab!74117
parents 4f38c758 f3e541c0
...@@ -307,7 +307,7 @@ module Clusters ...@@ -307,7 +307,7 @@ module Clusters
end end
def kubeclient def kubeclient
platform_kubernetes.kubeclient if kubernetes? platform_kubernetes&.kubeclient if kubernetes?
end end
def elastic_stack_adapter def elastic_stack_adapter
......
...@@ -35,9 +35,11 @@ module Clusters ...@@ -35,9 +35,11 @@ module Clusters
end end
def kubeclient_delete_namespace(kubernetes_namespace) def kubeclient_delete_namespace(kubernetes_namespace)
cluster.kubeclient.delete_namespace(kubernetes_namespace.namespace) cluster.kubeclient&.delete_namespace(kubernetes_namespace.namespace)
rescue Kubeclient::ResourceNotFoundError rescue Kubeclient::ResourceNotFoundError
# no-op: nothing to delete # 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
end end
end end
end end
......
...@@ -16,11 +16,14 @@ module Clusters ...@@ -16,11 +16,14 @@ module Clusters
def delete_gitlab_service_account def delete_gitlab_service_account
log_event(:deleting_gitlab_service_account) log_event(:deleting_gitlab_service_account)
cluster.kubeclient.delete_service_account( cluster.kubeclient&.delete_service_account(
::Clusters::Kubernetes::GITLAB_SERVICE_ACCOUNT_NAME, ::Clusters::Kubernetes::GITLAB_SERVICE_ACCOUNT_NAME,
::Clusters::Kubernetes::GITLAB_SERVICE_ACCOUNT_NAMESPACE ::Clusters::Kubernetes::GITLAB_SERVICE_ACCOUNT_NAMESPACE
) )
rescue Kubeclient::ResourceNotFoundError rescue Kubeclient::ResourceNotFoundError
# 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
end end
end end
end end
......
...@@ -58,6 +58,19 @@ RSpec.describe Clusters::Cleanup::ProjectNamespaceService do ...@@ -58,6 +58,19 @@ RSpec.describe Clusters::Cleanup::ProjectNamespaceService do
subject subject
end end
context 'when cluster.kubeclient is nil' do
let(:kubeclient_instance_double) { nil }
it 'schedules ::ServiceAccountWorker' do
expect(Clusters::Cleanup::ServiceAccountWorker).to receive(:perform_async).with(cluster.id)
subject
end
it 'deletes namespaces from database' do
expect { subject }.to change { cluster.kubernetes_namespaces.exists? }.from(true).to(false)
end
end
end end
context 'when cluster has no namespaces' do context 'when cluster has no namespaces' do
......
...@@ -44,5 +44,13 @@ RSpec.describe Clusters::Cleanup::ServiceAccountService do ...@@ -44,5 +44,13 @@ RSpec.describe Clusters::Cleanup::ServiceAccountService do
it 'deletes cluster' do it 'deletes cluster' do
expect { subject }.to change { Clusters::Cluster.where(id: cluster.id).exists? }.from(true).to(false) expect { subject }.to change { Clusters::Cluster.where(id: cluster.id).exists? }.from(true).to(false)
end end
context 'when cluster.kubeclient is nil' do
let(:kubeclient_instance_double) { nil }
it 'deletes cluster' do
expect { subject }.to change { Clusters::Cluster.where(id: cluster.id).exists? }.from(true).to(false)
end
end
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