This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
delete.go
87 lines (73 loc) · 2.58 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cmd
import (
"fmt"
"github.com/lyft/flytepropeller/pkg/controller"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type DeleteOpts struct {
*RootOptions
force bool
allCompleted bool
chunkSize int64
limit int64
}
func NewDeleteCommand(opts *RootOptions) *cobra.Command {
deleteOpts := &DeleteOpts{
RootOptions: opts,
}
// deleteCmd represents the delete command
deleteCmd := &cobra.Command{
Use: "delete <opts> [workflow-name]",
Short: "delete a workflow",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
name := args[0]
return deleteOpts.deleteWorkflow(name)
}
return deleteOpts.deleteCompletedWorkflows()
},
}
deleteCmd.Flags().BoolVarP(&deleteOpts.force, "force", "f", false, "Enable force deletion to remove finalizers from a workflow.")
deleteCmd.Flags().BoolVarP(&deleteOpts.allCompleted, "all-completed", "a", false, "Delete all the workflows that have completed. Cannot be used with --force.")
deleteCmd.Flags().Int64VarP(&deleteOpts.chunkSize, "chunk-size", "c", 100, "When using all-completed, provide a chunk size to retrieve at once from the server.")
deleteCmd.Flags().Int64VarP(&deleteOpts.limit, "limit", "l", -1, "Only iterate over max limit records.")
return deleteCmd
}
func (d *DeleteOpts) deleteCompletedWorkflows() error {
if d.force && d.allCompleted {
return fmt.Errorf("cannot delete multiple workflows with --force")
}
if !d.allCompleted {
return fmt.Errorf("all completed | workflow name is required")
}
t, err := d.GetTimeoutSeconds()
if err != nil {
return err
}
p := v1.DeletePropagationBackground
return d.flyteClient.FlyteworkflowV1alpha1().FlyteWorkflows(d.ConfigOverrides.Context.Namespace).DeleteCollection(
&v1.DeleteOptions{PropagationPolicy: &p}, v1.ListOptions{
TimeoutSeconds: &t,
LabelSelector: v1.FormatLabelSelector(controller.CompletedWorkflowsLabelSelector()),
},
)
}
func (d *DeleteOpts) deleteWorkflow(name string) error {
p := v1.DeletePropagationBackground
if err := d.flyteClient.FlyteworkflowV1alpha1().FlyteWorkflows(d.ConfigOverrides.Context.Namespace).Delete(name, &v1.DeleteOptions{PropagationPolicy: &p}); err != nil {
return err
}
if d.force {
w, err := d.flyteClient.FlyteworkflowV1alpha1().FlyteWorkflows(d.ConfigOverrides.Context.Namespace).Get(name, v1.GetOptions{})
if err != nil {
return err
}
w.SetFinalizers([]string{})
if _, err := d.flyteClient.FlyteworkflowV1alpha1().FlyteWorkflows(d.ConfigOverrides.Context.Namespace).Update(w); err != nil {
return err
}
}
return nil
}