From 08752e5d742a144ffb1ec7c8e07e7a558774fbfc Mon Sep 17 00:00:00 2001 From: Alessio Caiazza Date: Fri, 3 Nov 2017 10:02:30 +0100 Subject: [PATCH] Remove `Clusters::Applications::FetchInstallationStatusService` --- app/models/clusters/cluster.rb | 2 +- .../check_installation_progress_service.rb | 64 +++++++++++++------ .../fetch_installation_status_service.rb | 15 ----- .../clusters/applications/install_service.rb | 2 +- ...luster_wait_for_app_installation_worker.rb | 3 +- lib/gitlab/kubernetes/pod.rb | 12 ++++ 6 files changed, 60 insertions(+), 38 deletions(-) delete mode 100644 app/services/clusters/applications/fetch_installation_status_service.rb create mode 100644 lib/gitlab/kubernetes/pod.rb diff --git a/app/models/clusters/cluster.rb b/app/models/clusters/cluster.rb index 7dae2234998..77b299e46a0 100644 --- a/app/models/clusters/cluster.rb +++ b/app/models/clusters/cluster.rb @@ -52,7 +52,7 @@ module Clusters end def applications - [ + [ application_helm || build_application_helm ] end diff --git a/app/services/clusters/applications/check_installation_progress_service.rb b/app/services/clusters/applications/check_installation_progress_service.rb index 4e8fd9baaf4..7f5a633b749 100644 --- a/app/services/clusters/applications/check_installation_progress_service.rb +++ b/app/services/clusters/applications/check_installation_progress_service.rb @@ -4,26 +4,52 @@ module Clusters def execute return unless app.installing? - FetchInstallationStatusService.new(app).execute do |phase, log| - case phase - when 'Succeeded' - if app.make_installed - FinalizeInstallationService.new(app).execute - else - app.make_errored!("Failed to update app record; #{app.errors}") - end - when 'Failed' - app.make_errored!(log || 'Installation silently failed') - FinalizeInstallationService.new(app).execute - else - if Time.now.utc - app.updated_at.to_time.utc > ClusterWaitForAppInstallationWorker::TIMEOUT - app.make_errored!('App installation timeouted') - else - ClusterWaitForAppInstallationWorker.perform_in( - ClusterWaitForAppInstallationWorker::EAGER_INTERVAL, app.name, app.id) - end - end + case installation_phase + when Gitlab::Kubernetes::Pod::SUCCEEDED + on_succeeded + when Gitlab::Kubernetes::Pod::FAILED + on_failed + else + check_timeout end + rescue KubeException => ke + app.make_errored!("Kubernetes error: #{ke.message}") unless app.errored? + end + + private + + def on_succeeded + if app.make_installed + finalize_installation + else + app.make_errored!("Failed to update app record; #{app.errors}") + end + end + + def on_failed + app.make_errored!(log || 'Installation silently failed') + finalize_installation + end + + def check_timeout + if Time.now.utc - app.updated_at.to_time.utc > ClusterWaitForAppInstallationWorker::TIMEOUT + app.make_errored!('App installation timeouted') + else + ClusterWaitForAppInstallationWorker.perform_in( + ClusterWaitForAppInstallationWorker::INTERVAL, app.name, app.id) + end + end + + def finilize_installation + FinalizeInstallationService.new(app).execute + end + + def installation_phase + helm_api.installation_status(app) + end + + def installation_errors + helm_api.installation_log(app) end end end diff --git a/app/services/clusters/applications/fetch_installation_status_service.rb b/app/services/clusters/applications/fetch_installation_status_service.rb deleted file mode 100644 index 3d082485532..00000000000 --- a/app/services/clusters/applications/fetch_installation_status_service.rb +++ /dev/null @@ -1,15 +0,0 @@ -module Clusters - module Applications - class FetchInstallationStatusService < BaseHelmService - def execute - return unless app.installing? - - phase = helm_api.installation_status(app) - log = helm_api.installation_log(app) if phase == 'Failed' - yield(phase, log) if block_given? - rescue KubeException => ke - app.make_errored!("Kubernetes error: #{ke.message}") unless app.errored? - end - end - end -end diff --git a/app/services/clusters/applications/install_service.rb b/app/services/clusters/applications/install_service.rb index 7fcccb5e78c..5ed0968a98a 100644 --- a/app/services/clusters/applications/install_service.rb +++ b/app/services/clusters/applications/install_service.rb @@ -9,7 +9,7 @@ module Clusters if app.make_installing ClusterWaitForAppInstallationWorker.perform_in( - ClusterWaitForAppInstallationWorker::INITIAL_INTERVAL, app.name, app.id) + ClusterWaitForAppInstallationWorker::INTERVAL, app.name, app.id) else app.make_errored!("Failed to update app record; #{app.errors}") end diff --git a/app/workers/cluster_wait_for_app_installation_worker.rb b/app/workers/cluster_wait_for_app_installation_worker.rb index d5974c467c4..548f34fc6a5 100644 --- a/app/workers/cluster_wait_for_app_installation_worker.rb +++ b/app/workers/cluster_wait_for_app_installation_worker.rb @@ -3,8 +3,7 @@ class ClusterWaitForAppInstallationWorker include ClusterQueue include ClusterApplications - INITIAL_INTERVAL = 30.seconds - EAGER_INTERVAL = 10.seconds + INTERVAL = 30.seconds TIMEOUT = 20.minutes def perform(app_name, app_id) diff --git a/lib/gitlab/kubernetes/pod.rb b/lib/gitlab/kubernetes/pod.rb new file mode 100644 index 00000000000..12a6bfcf816 --- /dev/null +++ b/lib/gitlab/kubernetes/pod.rb @@ -0,0 +1,12 @@ +module Gitlab + module Kubernetes + module Pod + PENDING = 'Pending'.freeze + RUNNING = 'Running'.freeze + SUCCEEDED = 'Succeeded'.freeze + FAILED = 'Failed'.freeze + UNKNOWN = 'Unknown'.freeze + PHASES = [PENDING, RUNNING, SUCCEEDED, FAILED, UNKNONW].freeze + end + end +end