forked from argoproj/argo-workflows
/
terminate.go
109 lines (85 loc) · 2.89 KB
/
terminate.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package commands
import (
"fmt"
"os"
"github.com/argoproj/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client"
workflowpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/workflow"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)
type terminateOption struct {
namespace string
labels string
fields string
dryRun bool
}
func (t *terminateOption) isList() bool {
if t.labels != "" || t.fields != "" {
return true
}
return false
}
func (t *terminateOption) convertToWorkflows(names []string) wfv1.Workflows {
var wfs wfv1.Workflows
for _, n := range names {
wfs = append(wfs, wfv1.Workflow{
ObjectMeta: metav1.ObjectMeta{Name: n, Namespace: t.namespace},
})
}
return wfs
}
func NewTerminateCommand() *cobra.Command {
t := &terminateOption{}
command := &cobra.Command{
Use: "terminate WORKFLOW WORKFLOW2...",
Short: "terminate zero or more workflows immediately",
Example: `# Terminate a workflow:
argo terminate my-wf
# Terminate the latest workflow:
argo terminate @latest
# Terminate multiple workflows by label selector
argo terminate -l workflows.argoproj.io/test=true
# Terminate multiple workflows by field selector
argo terminate --field-selector metadata.namespace=argo
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 && !t.isList() {
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}
ctx, apiClient := client.NewAPIClient(cmd.Context())
serviceClient := apiClient.NewWorkflowServiceClient()
t.namespace = client.Namespace()
var workflows wfv1.Workflows
if t.isList() {
listed, err := listWorkflows(ctx, serviceClient, listFlags{
namespace: t.namespace,
fields: t.fields,
labels: t.labels,
})
errors.CheckError(err)
workflows = append(workflows, listed...)
} else {
workflows = t.convertToWorkflows(args)
}
for _, w := range workflows {
if t.dryRun {
fmt.Printf("workflow %s terminated (dry-run)\n", w.Name)
continue
}
wf, err := serviceClient.TerminateWorkflow(ctx, &workflowpkg.WorkflowTerminateRequest{
Name: w.Name,
Namespace: w.Namespace,
})
errors.CheckError(err)
fmt.Printf("workflow %s terminated\n", wf.Name)
}
},
}
command.Flags().StringVarP(&t.labels, "selector", "l", "", "Selector (label query) to filter on, not including uninitialized ones, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
command.Flags().StringVar(&t.fields, "field-selector", "", "Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selectorkey1=value1,key2=value2). The server only supports a limited number of field queries per type.")
command.Flags().BoolVar(&t.dryRun, "dry-run", false, "Do not terminate the workflow, only print what would happen")
return command
}