Skip to content

Commit

Permalink
chore(util): Move job deletion to cobra finalizer to simplify cmd wor…
Browse files Browse the repository at this point in the history
…kflows
  • Loading branch information
gabe565 committed Mar 4, 2024
1 parent 544bccd commit e138eee
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 18 deletions.
3 changes: 0 additions & 3 deletions cmd/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,5 @@ func preRun(cmd *cobra.Command, args []string) (err error) {
}

func run(cmd *cobra.Command, args []string) (err error) {
defer func() {
util.Teardown(cmd, &action.Global)
}()
return action.Run(cmd.Context())
}
3 changes: 0 additions & 3 deletions cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,5 @@ func preRun(cmd *cobra.Command, args []string) error {
}

func run(cmd *cobra.Command, args []string) (err error) {
defer func() {
util.Teardown(cmd, &action.Global)
}()
return action.Run(cmd.Context())
}
4 changes: 0 additions & 4 deletions cmd/port_forward/port_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func preRun(cmd *cobra.Command, args []string) error {
}
action.LocalPort, err = cmd.Flags().GetUint16(consts.ListenPortFlag)
if err != nil {
util.Teardown(cmd, &action.Global)
panic(err)
}
if action.LocalPort == 0 {
Expand All @@ -86,8 +85,5 @@ func preRun(cmd *cobra.Command, args []string) error {
}

func run(cmd *cobra.Command, args []string) (err error) {
defer func() {
util.Teardown(cmd, &action.Global)
}()
return action.Run(cmd.Context())
}
4 changes: 0 additions & 4 deletions cmd/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,5 @@ func preRun(cmd *cobra.Command, args []string) (err error) {
}

func run(cmd *cobra.Command, args []string) (err error) {
defer func() {
util.Teardown(cmd, &action.Global)
}()

return action.Run(cmd.Context())
}
1 change: 0 additions & 1 deletion cmd/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ func run(cmd *cobra.Command, args []string) error {
fmt.Println(prefixErr, "Job creation failed:", err.Error())
os.Exit(1)
}
defer util.Teardown(cmd, &conf)

var buf strings.Builder
listTablesCmd := conf.Dialect.ExecCommand(config.Exec{
Expand Down
4 changes: 3 additions & 1 deletion internal/util/cmd_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,11 @@ func CreateJob(cmd *cobra.Command, conf *config.Global, opts SetupOptions) error
if err := createJob(cmd, conf, opts.Name); err != nil {
return err
}
cobra.OnFinalize(func() {
Teardown(conf)
})

if err := watchJobPod(cmd, conf); err != nil {
Teardown(cmd, conf)
return err
}
}
Expand Down
3 changes: 1 addition & 2 deletions internal/util/cmd_teardown.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (

"github.com/clevyr/kubedb/internal/config"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Teardown(cmd *cobra.Command, conf *config.Global) {
func Teardown(conf *config.Global) {
if conf.Job != nil {
log.WithField("name", conf.Job.ObjectMeta.Name).Info("cleaning up job")
policy := metav1.DeletePropagationForeground
Expand Down

0 comments on commit e138eee

Please sign in to comment.