From 538258fa632718585ea25b1f3a45f42970099e8a Mon Sep 17 00:00:00 2001 From: Eugen Sumin Date: Mon, 2 Oct 2023 11:32:06 +0200 Subject: [PATCH 1/2] Rename PodRunner's RunEx to Run --- pkg/function/backup_data_stats.go | 2 +- pkg/function/checkRepository.go | 2 +- pkg/function/copy_volume_data.go | 2 +- pkg/function/delete_data.go | 2 +- .../delete_data_using_kopia_server.go | 2 +- pkg/function/describe_backups.go | 2 +- pkg/function/kube_task.go | 2 +- pkg/function/prepare_data.go | 2 +- pkg/function/restore_data.go | 2 +- .../restore_data_using_kopia_server.go | 2 +- pkg/kube/pod_runner.go | 22 +++++-------------- pkg/kube/pod_runner_test.go | 4 ++-- 12 files changed, 17 insertions(+), 29 deletions(-) diff --git a/pkg/function/backup_data_stats.go b/pkg/function/backup_data_stats.go index 3f23fe4f84..f3f544d5a9 100644 --- a/pkg/function/backup_data_stats.go +++ b/pkg/function/backup_data_stats.go @@ -72,7 +72,7 @@ func backupDataStats(ctx context.Context, cli kubernetes.Interface, tp param.Tem } pr := kube.NewPodRunner(cli, options) podFunc := backupDataStatsPodFunc(tp, encryptionKey, backupArtifactPrefix, backupID, mode) - return pr.RunEx(ctx, podFunc) + return pr.Run(ctx, podFunc) } func backupDataStatsPodFunc( diff --git a/pkg/function/checkRepository.go b/pkg/function/checkRepository.go index 88056b56e4..7911ee5311 100644 --- a/pkg/function/checkRepository.go +++ b/pkg/function/checkRepository.go @@ -55,7 +55,7 @@ func CheckRepository(ctx context.Context, cli kubernetes.Interface, tp param.Tem } pr := kube.NewPodRunner(cli, options) podFunc := CheckRepositoryPodFunc(cli, tp, encryptionKey, targetPaths) - return pr.RunEx(ctx, podFunc) + return pr.Run(ctx, podFunc) } func CheckRepositoryPodFunc( diff --git a/pkg/function/copy_volume_data.go b/pkg/function/copy_volume_data.go index f96ebd2913..f0423e0898 100644 --- a/pkg/function/copy_volume_data.go +++ b/pkg/function/copy_volume_data.go @@ -81,7 +81,7 @@ func copyVolumeData(ctx context.Context, cli kubernetes.Interface, tp param.Temp } pr := kube.NewPodRunner(cli, options) podFunc := copyVolumeDataPodFunc(cli, tp, mountPoint, targetPath, encryptionKey) - return pr.RunEx(ctx, podFunc) + return pr.Run(ctx, podFunc) } func copyVolumeDataPodFunc( diff --git a/pkg/function/delete_data.go b/pkg/function/delete_data.go index 4de0951e87..53173197fd 100644 --- a/pkg/function/delete_data.go +++ b/pkg/function/delete_data.go @@ -93,7 +93,7 @@ func deleteData( } pr := kube.NewPodRunner(cli, options) podFunc := deleteDataPodFunc(tp, reclaimSpace, encryptionKey, targetPaths, deleteTags, deleteIdentifiers) - return pr.RunEx(ctx, podFunc) + return pr.Run(ctx, podFunc) } //nolint:gocognit diff --git a/pkg/function/delete_data_using_kopia_server.go b/pkg/function/delete_data_using_kopia_server.go index f23c068815..d72946f2cb 100644 --- a/pkg/function/delete_data_using_kopia_server.go +++ b/pkg/function/delete_data_using_kopia_server.go @@ -149,7 +149,7 @@ func deleteDataFromServer( username, userPassphrase, ) - return pr.RunEx(ctx, podFunc) + return pr.Run(ctx, podFunc) } func deleteDataFromServerPodFunc( diff --git a/pkg/function/describe_backups.go b/pkg/function/describe_backups.go index fb9f512a13..dc736354e0 100644 --- a/pkg/function/describe_backups.go +++ b/pkg/function/describe_backups.go @@ -74,7 +74,7 @@ func describeBackups(ctx context.Context, cli kubernetes.Interface, tp param.Tem } pr := kube.NewPodRunner(cli, options) podFunc := describeBackupsPodFunc(cli, tp, encryptionKey, targetPaths) - return pr.RunEx(ctx, podFunc) + return pr.Run(ctx, podFunc) } func describeBackupsPodFunc( diff --git a/pkg/function/kube_task.go b/pkg/function/kube_task.go index b30dcb512e..8ec2825b39 100644 --- a/pkg/function/kube_task.go +++ b/pkg/function/kube_task.go @@ -62,7 +62,7 @@ func kubeTask(ctx context.Context, cli kubernetes.Interface, namespace, image st pr := kube.NewPodRunner(cli, options) podFunc := kubeTaskPodFunc() - return pr.RunEx(ctx, podFunc) + return pr.Run(ctx, podFunc) } func kubeTaskPodFunc() func(ctx context.Context, pc kube.PodController) (map[string]interface{}, error) { diff --git a/pkg/function/prepare_data.go b/pkg/function/prepare_data.go index 37c5544b20..e62d2c51d7 100644 --- a/pkg/function/prepare_data.go +++ b/pkg/function/prepare_data.go @@ -97,7 +97,7 @@ func prepareData(ctx context.Context, cli kubernetes.Interface, namespace, servi } pr := kube.NewPodRunner(cli, options) podFunc := prepareDataPodFunc(cli) - return pr.RunEx(ctx, podFunc) + return pr.Run(ctx, podFunc) } func prepareDataPodFunc(cli kubernetes.Interface) func(ctx context.Context, pc kube.PodController) (map[string]interface{}, error) { diff --git a/pkg/function/restore_data.go b/pkg/function/restore_data.go index ad54550c1f..2b69a5a9ea 100644 --- a/pkg/function/restore_data.go +++ b/pkg/function/restore_data.go @@ -126,7 +126,7 @@ func restoreData(ctx context.Context, cli kubernetes.Interface, tp param.Templat } pr := kube.NewPodRunner(cli, options) podFunc := restoreDataPodFunc(tp, encryptionKey, backupArtifactPrefix, restorePath, backupTag, backupID) - return pr.RunEx(ctx, podFunc) + return pr.Run(ctx, podFunc) } func restoreDataPodFunc( diff --git a/pkg/function/restore_data_using_kopia_server.go b/pkg/function/restore_data_using_kopia_server.go index 0d327fd5a0..a5ccc924c3 100644 --- a/pkg/function/restore_data_using_kopia_server.go +++ b/pkg/function/restore_data_using_kopia_server.go @@ -194,7 +194,7 @@ func restoreDataFromServer( userPassphrase, sparseRestore, ) - return pr.RunEx(ctx, podFunc) + return pr.Run(ctx, podFunc) } func restoreDataFromServerPodFunc( diff --git a/pkg/kube/pod_runner.go b/pkg/kube/pod_runner.go index 64b864b0f7..c9aea53da7 100644 --- a/pkg/kube/pod_runner.go +++ b/pkg/kube/pod_runner.go @@ -18,7 +18,6 @@ import ( "context" "github.com/pkg/errors" - v1 "k8s.io/api/core/v1" "k8s.io/client-go/kubernetes" "github.com/kanisterio/kanister/pkg/consts" @@ -28,16 +27,12 @@ import ( // PodRunner allows us to start / stop pod, write file to pod and execute command within it type PodRunner interface { - Run(ctx context.Context, fn func(context.Context, *v1.Pod) (map[string]interface{}, error)) (map[string]interface{}, error) - // RunEx utilizes the PodController interface and forwards it to the functor, simplifying the manipulation with - // particular pod from the functor. - // TODO: Since significant number of functions are currently using PodRunner, we'll keep Run for now. - // However, once all these functions have been refactored to use PodController, - // Run should be removed and RunEx has to be renamed to Run. - RunEx(ctx context.Context, fn func(context.Context, PodController) (map[string]interface{}, error)) (map[string]interface{}, error) + // Run creates pod using the PodController interface and forwards it to the functor. + // Pod will be deleted as soon as functor exits. + Run(ctx context.Context, fn func(context.Context, PodController) (map[string]interface{}, error)) (map[string]interface{}, error) } -// PodRunner specifies Kubernetes Client and PodOptions needed for creating Pod +// podRunner implements PodRunner interface type podRunner struct { pc PodController } @@ -60,14 +55,7 @@ func NewPodRunnerWithPodController(pc PodController) PodRunner { } // Run will create a new Pod based on PodRunner contents and execute the given function -func (p *podRunner) Run(ctx context.Context, fn func(context.Context, *v1.Pod) (map[string]interface{}, error)) (map[string]interface{}, error) { - return p.RunEx(ctx, func(innerCtx context.Context, pc PodController) (map[string]interface{}, error) { - return fn(innerCtx, pc.Pod()) - }) -} - -// RunEx will create a new Pod based on PodRunner contents and execute the given function -func (p *podRunner) RunEx(ctx context.Context, fn func(context.Context, PodController) (map[string]interface{}, error)) (map[string]interface{}, error) { +func (p *podRunner) Run(ctx context.Context, fn func(context.Context, PodController) (map[string]interface{}, error)) (map[string]interface{}, error) { ctx, cancel := context.WithCancel(ctx) defer cancel() diff --git a/pkg/kube/pod_runner_test.go b/pkg/kube/pod_runner_test.go index 900d7df852..779d58c080 100644 --- a/pkg/kube/pod_runner_test.go +++ b/pkg/kube/pod_runner_test.go @@ -109,8 +109,8 @@ func (s *PodRunnerTestSuite) TestPodRunnerForSuccessCase(c *C) { cancel() } -func makePodRunnerTestFunc(ch chan struct{}) func(ctx context.Context, pod *v1.Pod) (map[string]interface{}, error) { - return func(ctx context.Context, pod *v1.Pod) (map[string]interface{}, error) { +func makePodRunnerTestFunc(ch chan struct{}) func(ctx context.Context, pc PodController) (map[string]interface{}, error) { + return func(ctx context.Context, pc PodController) (map[string]interface{}, error) { <-ch return nil, nil } From f959a63a64ca2d33d6857a3032fde20f772797f0 Mon Sep 17 00:00:00 2001 From: Eugene Sumin <95425330+e-sumin@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:37:41 +0200 Subject: [PATCH 2/2] Improve readability Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com> --- pkg/kube/pod_runner.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/kube/pod_runner.go b/pkg/kube/pod_runner.go index c9aea53da7..2d0c2268dd 100644 --- a/pkg/kube/pod_runner.go +++ b/pkg/kube/pod_runner.go @@ -55,7 +55,10 @@ func NewPodRunnerWithPodController(pc PodController) PodRunner { } // Run will create a new Pod based on PodRunner contents and execute the given function -func (p *podRunner) Run(ctx context.Context, fn func(context.Context, PodController) (map[string]interface{}, error)) (map[string]interface{}, error) { +func (p *podRunner) Run( + ctx context.Context, + fn func(context.Context, PodController) (map[string]interface{}, error), +) (map[string]interface{}, error) { ctx, cancel := context.WithCancel(ctx) defer cancel()