From 7d96557fb0fc73087ea3e0db5214c7a64a513260 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Mon, 4 Mar 2024 13:01:27 -0600 Subject: [PATCH] chore(util): Change job func params from cobra.Command to context.Context --- cmd/dump/dump.go | 2 +- cmd/exec/exec.go | 2 +- cmd/restore/restore.go | 2 +- cmd/status/status.go | 2 +- internal/util/cmd_setup.go | 14 +++++++------- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/dump/dump.go b/cmd/dump/dump.go index 5a1e879d..ea5615e0 100644 --- a/cmd/dump/dump.go +++ b/cmd/dump/dump.go @@ -117,7 +117,7 @@ func preRun(cmd *cobra.Command, args []string) (err error) { if err := util.DefaultSetup(cmd, &action.Global, setupOptions); err != nil { return err } - if err := util.CreateJob(cmd, &action.Global, setupOptions); err != nil { + if err := util.CreateJob(cmd.Context(), &action.Global, setupOptions); err != nil { return err } diff --git a/cmd/exec/exec.go b/cmd/exec/exec.go index 2ba7433d..948e6310 100644 --- a/cmd/exec/exec.go +++ b/cmd/exec/exec.go @@ -40,7 +40,7 @@ func preRun(cmd *cobra.Command, args []string) error { if err := util.DefaultSetup(cmd, &action.Global, setupOptions); err != nil { return err } - if err := util.CreateJob(cmd, &action.Global, setupOptions); err != nil { + if err := util.CreateJob(cmd.Context(), &action.Global, setupOptions); err != nil { return err } diff --git a/cmd/restore/restore.go b/cmd/restore/restore.go index 14663eff..21c5e3f2 100644 --- a/cmd/restore/restore.go +++ b/cmd/restore/restore.go @@ -123,7 +123,7 @@ func preRun(cmd *cobra.Command, args []string) (err error) { } } - if err := util.CreateJob(cmd, &action.Global, setupOptions); err != nil { + if err := util.CreateJob(cmd.Context(), &action.Global, setupOptions); err != nil { return err } diff --git a/cmd/status/status.go b/cmd/status/status.go index 3ea074f3..d8a157a8 100644 --- a/cmd/status/status.go +++ b/cmd/status/status.go @@ -97,7 +97,7 @@ func run(cmd *cobra.Command, args []string) error { os.Exit(1) } - if err := util.CreateJob(cmd, &conf, util.SetupOptions{Name: "status"}); err == nil { + if err := util.CreateJob(cmd.Context(), &conf, util.SetupOptions{Name: "status"}); err == nil { fmt.Println(prefixOk, "Jobs can be created") } else { fmt.Println(prefixErr, "Job creation failed:", err.Error()) diff --git a/internal/util/cmd_setup.go b/internal/util/cmd_setup.go index 5579727a..64aa03f5 100644 --- a/internal/util/cmd_setup.go +++ b/internal/util/cmd_setup.go @@ -224,23 +224,23 @@ func DefaultSetup(cmd *cobra.Command, conf *config.Global, opts SetupOptions) (e return group.Wait() } -func CreateJob(cmd *cobra.Command, conf *config.Global, opts SetupOptions) error { +func CreateJob(ctx context.Context, conf *config.Global, opts SetupOptions) error { if !viper.GetBool(consts.NoJobKey) { - if err := createJob(cmd, conf, opts.Name); err != nil { + if err := createJob(ctx, conf, opts.Name); err != nil { return err } cobra.OnFinalize(func() { Teardown(conf) }) - if err := watchJobPod(cmd, conf); err != nil { + if err := watchJobPod(ctx, conf); err != nil { return err } } return nil } -func createJob(cmd *cobra.Command, conf *config.Global, actionName string) error { +func createJob(ctx context.Context, conf *config.Global, actionName string) error { image := conf.DbPod.Spec.Containers[0].Image name := "kubedb-" @@ -332,7 +332,7 @@ func createJob(cmd *cobra.Command, conf *config.Global, actionName string) error log.WithField("namespace", conf.Namespace).Info("creating job") - ctx, cancel := context.WithTimeout(cmd.Context(), time.Minute) + ctx, cancel := context.WithTimeout(ctx, time.Minute) defer cancel() var err error @@ -343,13 +343,13 @@ func createJob(cmd *cobra.Command, conf *config.Global, actionName string) error return nil } -func watchJobPod(cmd *cobra.Command, conf *config.Global) error { +func watchJobPod(ctx context.Context, conf *config.Global) error { log.WithFields(log.Fields{ "namespace": conf.Namespace, "name": "job.batch/" + conf.Job.ObjectMeta.Name, }).Info("waiting for job...") - ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Minute) + ctx, cancel := context.WithTimeout(ctx, 5*time.Minute) defer cancel() watch, err := conf.Client.Pods().Watch(ctx, metav1.ListOptions{