From cefad21a5fc9ff544105adc838ec3ccf5304eb7c Mon Sep 17 00:00:00 2001 From: Matthew Arnold Date: Wed, 22 Jan 2025 09:33:58 -0500 Subject: [PATCH 1/2] Clean up some unused active source connections. Signed-off-by: Matthew Arnold --- pkg/controller/plan/controller.go | 4 ++++ pkg/controller/plan/migration.go | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/pkg/controller/plan/controller.go b/pkg/controller/plan/controller.go index 661ba6196..abe4b24ac 100644 --- a/pkg/controller/plan/controller.go +++ b/pkg/controller/plan/controller.go @@ -270,6 +270,7 @@ func (r *Reconciler) setPopulatorDataSourceLabels(plan *api.Plan) { r.Log.Error(err, "Couldn't construct plan context when trying to set populator labels.") } else { runner := Migration{Context: ctx} + defer runner.logout() runner.SetPopulatorDataSourceLabels() planCopy := plan.DeepCopy() if plan.Annotations == nil { @@ -301,6 +302,7 @@ func (r *Reconciler) archive(plan *api.Plan) { r.Log.Error(err, "Couldn't construct plan context while archiving plan.") } else { runner := Migration{Context: ctx} + defer runner.logout() runner.Archive() } // Regardless of whether or not we can clean up, mark the plan archived. @@ -386,6 +388,7 @@ func (r *Reconciler) execute(plan *api.Plan) (reQ time.Duration, err error) { // // Cancel. runner := Migration{Context: ctx} + defer runner.logout() err = runner.Cancel() if err != nil { return @@ -425,6 +428,7 @@ func (r *Reconciler) execute(plan *api.Plan) (reQ time.Duration, err error) { // Run the migration. snapshot.BeginStagingConditions() runner = Migration{Context: ctx} + defer runner.logout() reQ, err = runner.Run() if err != nil { return diff --git a/pkg/controller/plan/migration.go b/pkg/controller/plan/migration.go index 69e835aa7..b3d2bbfff 100644 --- a/pkg/controller/plan/migration.go +++ b/pkg/controller/plan/migration.go @@ -278,6 +278,12 @@ func (r *Migration) init() (err error) { return } +func (r *Migration) logout() { + if r.provider != nil { + r.provider.Close() + } +} + // Begin the migration. func (r *Migration) begin() (err error) { snapshot := r.Plan.Status.Migration.ActiveSnapshot() From 51c952ce1b5d6e636c8fc84858c5bc1b6d16afbd Mon Sep 17 00:00:00 2001 From: Matthew Arnold Date: Wed, 22 Jan 2025 12:09:39 -0500 Subject: [PATCH 2/2] Move session cleanup down a level. Log out in migration instead of controller, much better results. Signed-off-by: Matthew Arnold --- pkg/controller/plan/controller.go | 4 ---- pkg/controller/plan/migration.go | 21 +++++++++++++++------ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/pkg/controller/plan/controller.go b/pkg/controller/plan/controller.go index abe4b24ac..661ba6196 100644 --- a/pkg/controller/plan/controller.go +++ b/pkg/controller/plan/controller.go @@ -270,7 +270,6 @@ func (r *Reconciler) setPopulatorDataSourceLabels(plan *api.Plan) { r.Log.Error(err, "Couldn't construct plan context when trying to set populator labels.") } else { runner := Migration{Context: ctx} - defer runner.logout() runner.SetPopulatorDataSourceLabels() planCopy := plan.DeepCopy() if plan.Annotations == nil { @@ -302,7 +301,6 @@ func (r *Reconciler) archive(plan *api.Plan) { r.Log.Error(err, "Couldn't construct plan context while archiving plan.") } else { runner := Migration{Context: ctx} - defer runner.logout() runner.Archive() } // Regardless of whether or not we can clean up, mark the plan archived. @@ -388,7 +386,6 @@ func (r *Reconciler) execute(plan *api.Plan) (reQ time.Duration, err error) { // // Cancel. runner := Migration{Context: ctx} - defer runner.logout() err = runner.Cancel() if err != nil { return @@ -428,7 +425,6 @@ func (r *Reconciler) execute(plan *api.Plan) (reQ time.Duration, err error) { // Run the migration. snapshot.BeginStagingConditions() runner = Migration{Context: ctx} - defer runner.logout() reQ, err = runner.Run() if err != nil { return diff --git a/pkg/controller/plan/migration.go b/pkg/controller/plan/migration.go index b3d2bbfff..6007472eb 100644 --- a/pkg/controller/plan/migration.go +++ b/pkg/controller/plan/migration.go @@ -278,12 +278,6 @@ func (r *Migration) init() (err error) { return } -func (r *Migration) logout() { - if r.provider != nil { - r.provider.Close() - } -} - // Begin the migration. func (r *Migration) begin() (err error) { snapshot := r.Plan.Status.Migration.ActiveSnapshot() @@ -379,6 +373,11 @@ func (r *Migration) begin() (err error) { // Archive the plan. // Best effort to remove any retained migration resources. func (r *Migration) Archive() { + defer func() { + if r.provider != nil { + r.provider.Close() + } + }() if err := r.init(); err != nil { r.Log.Error(err, "Archive initialization failed.") return @@ -410,6 +409,11 @@ func (r *Migration) Archive() { } func (r *Migration) SetPopulatorDataSourceLabels() { + defer func() { + if r.provider != nil { + r.provider.Close() + } + }() err := r.init() if err != nil { r.Log.Error(err, "Setting Populator Data Source labels failed.") @@ -437,6 +441,11 @@ func (r *Migration) SetPopulatorDataSourceLabels() { // Cancel the migration. // Delete resources associated with VMs that have been marked canceled. func (r *Migration) Cancel() error { + defer func() { + if r.provider != nil { + r.provider.Close() + } + }() if err := r.init(); err != nil { return liberr.Wrap(err) }