diff options
author | João Cunha <j.a.cunha@gmail.com> | 2019-07-03 17:28:00 +0100 |
---|---|---|
committer | João Cunha <j.a.cunha@gmail.com> | 2019-07-03 18:29:42 +0100 |
commit | 7314097ba8b5d8326fda1c0efcd7db7689fa34cc (patch) | |
tree | 5bb2710046a50cedde02ae64fbe849e9b452d164 | |
parent | e9e14e87b52db4c970c177e52ab481f934fcc081 (diff) | |
download | gitlab-ce-7314097ba8b5d8326fda1c0efcd7db7689fa34cc.tar.gz |
Cancel builds before uninstalling with Helm and remove runner
- Use the make_uninstalling transition to cancel builds related to
the Clusters::Applications::Runner. If we uninstall with Helm before
doing this, the pod that is running the build might get stuck and never
be done.
- Remove associated Ci::Runner after Clusters::Applications::Runner is
destroyed.
-rw-r--r-- | app/models/clusters/applications/runner.rb | 12 | ||||
-rw-r--r-- | app/models/clusters/concerns/application_core.rb | 5 | ||||
-rw-r--r-- | app/models/clusters/concerns/application_status.rb | 4 | ||||
-rw-r--r-- | spec/models/clusters/applications/runner_spec.rb | 31 |
4 files changed, 45 insertions, 7 deletions
diff --git a/app/models/clusters/applications/runner.rb b/app/models/clusters/applications/runner.rb index 599a100471c..4bdd4867885 100644 --- a/app/models/clusters/applications/runner.rb +++ b/app/models/clusters/applications/runner.rb @@ -12,13 +12,11 @@ module Clusters include ::Clusters::Concerns::ApplicationVersion include ::Clusters::Concerns::ApplicationData - belongs_to :runner, class_name: 'Ci::Runner', foreign_key: :runner_id + belongs_to :runner, class_name: 'Ci::Runner', foreign_key: :runner_id, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent delegate :project, :group, to: :cluster default_value_for :version, VERSION - after_destroy :destroy_ci_runner - def chart "#{name}/gitlab-runner" end @@ -42,12 +40,12 @@ module Clusters ) end - private - - def destroy_ci_runner - runner.destroy + def prepare_uninstall + runner.builds.each(&:cancel) end + private + def ensure_runner runner || create_and_assign_runner end diff --git a/app/models/clusters/concerns/application_core.rb b/app/models/clusters/concerns/application_core.rb index 4514498b84b..bc58755c970 100644 --- a/app/models/clusters/concerns/application_core.rb +++ b/app/models/clusters/concerns/application_core.rb @@ -46,6 +46,11 @@ module Clusters command.version = version end end + + def prepare_uninstall + # Override if your application needs any action before + # being uninstalled by Helm + end end end end diff --git a/app/models/clusters/concerns/application_status.rb b/app/models/clusters/concerns/application_status.rb index f842e6aaf81..e83a7967c40 100644 --- a/app/models/clusters/concerns/application_status.rb +++ b/app/models/clusters/concerns/application_status.rb @@ -83,6 +83,10 @@ module Clusters # therefore we need to reflect that in the database. application.cluster.application_helm.update!(version: Gitlab::Kubernetes::Helm::HELM_VERSION) end + + after_transition any => [:uninstalling] do |application, _| + application.prepare_uninstall + end end end diff --git a/spec/models/clusters/applications/runner_spec.rb b/spec/models/clusters/applications/runner_spec.rb index b99f2e13675..0a32c2acd6c 100644 --- a/spec/models/clusters/applications/runner_spec.rb +++ b/spec/models/clusters/applications/runner_spec.rb @@ -156,4 +156,35 @@ describe Clusters::Applications::Runner do end end end + + describe '#make_installing!' do + subject { create(:clusters_applications_runner, :scheduled, runner: ci_runner) } + + let(:other_runner) { create(:ci_runner) } + let(:build) { create(:ci_build, :running, runner: subject.runner) } + let(:other_runner_build) { create(:ci_build, :running, runner: other_runner, project: build.project) } + + before do + expect(build.runner).not_to eq(other_runner_build.runner) + expect(build.project).to eq(other_runner_build.project) + + subject.make_uninstalling! + end + + it 'cancels builds of associated runner' do + expect(build.reload).to be_canceled + end + + it 'does not cancel other runner builds' do + expect(other_runner_build.reload).not_to be_canceled + end + end + + describe '#destroy!' do + it 'destroys its runner' do + application_runner = create(:clusters_applications_runner, :scheduled, runner: ci_runner) + + expect { application_runner.destroy! }.to change { Ci::Runner.count }.by(-1) + end + end end |