diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-19 06:06:07 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-19 06:06:07 +0300 |
commit | 7f3bff1556594dcdc1beca40d083ba7263965e21 (patch) | |
tree | ab18d957d9bc7b2888c6e9fa9b281a7c1cb8927b /app/services/clusters | |
parent | 8d0aed5e4a6ae59232cfa5ca168fa1b87073520d (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/clusters')
-rw-r--r-- | app/services/clusters/cleanup/app_service.rb | 33 | ||||
-rw-r--r-- | app/services/clusters/cleanup/base_service.rb | 43 | ||||
-rw-r--r-- | app/services/clusters/cleanup/project_namespace_service.rb | 44 | ||||
-rw-r--r-- | app/services/clusters/cleanup/service_account_service.rb | 27 |
4 files changed, 147 insertions, 0 deletions
diff --git a/app/services/clusters/cleanup/app_service.rb b/app/services/clusters/cleanup/app_service.rb new file mode 100644 index 00000000000..a7e29c78ea0 --- /dev/null +++ b/app/services/clusters/cleanup/app_service.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Clusters + module Cleanup + class AppService < Clusters::Cleanup::BaseService + def execute + persisted_applications = @cluster.persisted_applications + + persisted_applications.each do |app| + next unless app.available? + next unless app.can_uninstall? + + log_event(:uninstalling_app, application: app.class.application_name) + uninstall_app_async(app) + end + + # Keep calling the worker untill all dependencies are uninstalled + return schedule_next_execution(Clusters::Cleanup::AppWorker) if persisted_applications.any? + + log_event(:schedule_remove_project_namespaces) + cluster.continue_cleanup! + end + + private + + def uninstall_app_async(application) + application.make_scheduled! + + Clusters::Applications::UninstallWorker.perform_async(application.name, application.id) + end + end + end +end diff --git a/app/services/clusters/cleanup/base_service.rb b/app/services/clusters/cleanup/base_service.rb new file mode 100644 index 00000000000..f99e54cfc40 --- /dev/null +++ b/app/services/clusters/cleanup/base_service.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module Clusters + module Cleanup + class BaseService + DEFAULT_EXECUTION_INTERVAL = 1.minute + + def initialize(cluster, execution_count = 0) + @cluster = cluster + @execution_count = execution_count + end + + private + + attr_reader :cluster + + def logger + @logger ||= Gitlab::Kubernetes::Logger.build + end + + def log_event(event, extra_data = {}) + meta = { + service: self.class.name, + cluster_id: cluster.id, + execution_count: @execution_count, + event: event + } + + logger.info(meta.merge(extra_data)) + end + + def schedule_next_execution(worker_class) + log_event(:scheduling_execution, next_execution: @execution_count + 1) + worker_class.perform_in(execution_interval, cluster.id, @execution_count + 1) + end + + # Override this method to customize the execution interval + def execution_interval + DEFAULT_EXECUTION_INTERVAL + end + end + end +end diff --git a/app/services/clusters/cleanup/project_namespace_service.rb b/app/services/clusters/cleanup/project_namespace_service.rb new file mode 100644 index 00000000000..7621be565ff --- /dev/null +++ b/app/services/clusters/cleanup/project_namespace_service.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +module Clusters + module Cleanup + class ProjectNamespaceService < BaseService + 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) + rescue Kubeclient::HttpError + next + end + + kubernetes_namespace.destroy! + end + end + + def kubeclient_delete_namespace(kubernetes_namespace) + cluster.kubeclient.delete_namespace(kubernetes_namespace.namespace) + rescue Kubeclient::ResourceNotFoundError + # no-op: nothing to delete + end + end + end +end diff --git a/app/services/clusters/cleanup/service_account_service.rb b/app/services/clusters/cleanup/service_account_service.rb new file mode 100644 index 00000000000..d60bd76d388 --- /dev/null +++ b/app/services/clusters/cleanup/service_account_service.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module Clusters + module Cleanup + class ServiceAccountService < BaseService + def execute + delete_gitlab_service_account + + log_event(:destroying_cluster) + + cluster.destroy! + end + + private + + def delete_gitlab_service_account + log_event(:deleting_gitlab_service_account) + + cluster.kubeclient.delete_service_account( + ::Clusters::Kubernetes::GITLAB_SERVICE_ACCOUNT_NAME, + ::Clusters::Kubernetes::GITLAB_SERVICE_ACCOUNT_NAMESPACE + ) + rescue Kubeclient::ResourceNotFoundError + end + end + end +end |