forked from argoproj/argo-workflows
/
submit.go
195 lines (175 loc) · 6.37 KB
/
submit.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package commands
import (
"log"
"os"
"github.com/argoproj/pkg/errors"
argoJson "github.com/argoproj/pkg/json"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/argoproj/argo/cmd/argo/commands/client"
workflowpkg "github.com/argoproj/argo/pkg/apiclient/workflow"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo/workflow/common"
"github.com/argoproj/argo/workflow/util"
)
// cliSubmitOpts holds submission options specific to CLI submission (e.g. controlling output)
type cliSubmitOpts struct {
output string // --output
wait bool // --wait
watch bool // --watch
strict bool // --strict
priority *int32 // --priority
}
func NewSubmitCommand() *cobra.Command {
var (
submitOpts util.SubmitOpts
cliSubmitOpts cliSubmitOpts
priority int32
)
var command = &cobra.Command{
Use: "submit FILE1 FILE2...",
Short: "submit a workflow",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}
if cmd.Flag("priority").Changed {
cliSubmitOpts.priority = &priority
}
SubmitWorkflows(args, &submitOpts, &cliSubmitOpts)
},
}
command.Flags().StringVar(&submitOpts.Name, "name", "", "override metadata.name")
command.Flags().StringVar(&submitOpts.GenerateName, "generate-name", "", "override metadata.generateName")
command.Flags().StringVar(&submitOpts.Entrypoint, "entrypoint", "", "override entrypoint")
command.Flags().StringArrayVarP(&submitOpts.Parameters, "parameter", "p", []string{}, "pass an input parameter")
command.Flags().StringVar(&submitOpts.ServiceAccount, "serviceaccount", "", "run all pods in the workflow using specified serviceaccount")
command.Flags().StringVar(&submitOpts.InstanceID, "instanceid", "", "submit with a specific controller's instance id label")
command.Flags().BoolVar(&submitOpts.DryRun, "dry-run", false, "modify the workflow on the client-side without creating it")
command.Flags().BoolVar(&submitOpts.ServerDryRun, "server-dry-run", false, "send request to server with dry-run flag which will modify the workflow without creating it")
command.Flags().StringVarP(&cliSubmitOpts.output, "output", "o", "", "Output format. One of: name|json|yaml|wide")
command.Flags().BoolVarP(&cliSubmitOpts.wait, "wait", "w", false, "wait for the workflow to complete")
command.Flags().BoolVar(&cliSubmitOpts.watch, "watch", false, "watch the workflow until it completes")
command.Flags().BoolVar(&cliSubmitOpts.strict, "strict", true, "perform strict workflow validation")
command.Flags().Int32Var(&priority, "priority", 0, "workflow priority")
command.Flags().StringVarP(&submitOpts.ParameterFile, "parameter-file", "f", "", "pass a file containing all input parameters")
command.Flags().StringVarP(&submitOpts.Labels, "labels", "l", "", "Comma separated labels to apply to the workflow. Will override previous values.")
// Only complete files with appropriate extension.
err := command.Flags().SetAnnotation("parameter-file", cobra.BashCompFilenameExt, []string{"json", "yaml", "yml"})
if err != nil {
log.Fatal(err)
}
return command
}
func SubmitWorkflows(filePaths []string, submitOpts *util.SubmitOpts, cliOpts *cliSubmitOpts) {
if submitOpts == nil {
submitOpts = &util.SubmitOpts{}
}
if cliOpts == nil {
cliOpts = &cliSubmitOpts{}
}
ctx, apiClient := client.NewAPIClient()
serviceClient := apiClient.NewWorkflowServiceClient()
namespace := client.Namespace()
fileContents, err := util.ReadManifest(filePaths...)
if err != nil {
log.Fatal(err)
}
var workflows []wfv1.Workflow
for _, body := range fileContents {
wfs := unmarshalWorkflows(body, cliOpts.strict)
workflows = append(workflows, wfs...)
}
if cliOpts.watch {
if len(workflows) > 1 {
log.Fatalf("Cannot watch more than one workflow")
}
if cliOpts.wait {
log.Fatalf("--wait cannot be combined with --watch")
}
if submitOpts.DryRun {
log.Fatalf("--watch cannot be combined with --dry-run")
}
if submitOpts.ServerDryRun {
log.Fatalf("--watch cannot be combined with --server-dry-run")
}
}
if cliOpts.wait {
if submitOpts.DryRun {
log.Fatalf("--wait cannot be combined with --dry-run")
}
if submitOpts.ServerDryRun {
log.Fatalf("--wait cannot be combined with --server-dry-run")
}
}
if submitOpts.DryRun {
if cliOpts.output == "" {
log.Fatalf("--dry-run should have an output option")
}
if submitOpts.ServerDryRun {
log.Fatalf("--dry-run cannot be combined with --server-dry-run")
}
}
if submitOpts.ServerDryRun {
if cliOpts.output == "" {
log.Fatalf("--server-dry-run should have an output option")
}
}
if len(workflows) == 0 {
log.Println("No Workflow found in given files")
os.Exit(1)
}
var workflowNames []string
for _, wf := range workflows {
if wf.Namespace == "" {
// This is here to avoid passing an empty namespace when using --server-dry-run
wf.Namespace = namespace
}
err := util.ApplySubmitOpts(&wf, submitOpts)
errors.CheckError(err)
wf.Spec.Priority = cliOpts.priority
options := &metav1.CreateOptions{}
if submitOpts.DryRun {
options.DryRun = []string{"All"}
}
created, err := serviceClient.CreateWorkflow(ctx, &workflowpkg.WorkflowCreateRequest{
Namespace: wf.Namespace,
Workflow: &wf,
InstanceID: submitOpts.InstanceID,
ServerDryRun: submitOpts.ServerDryRun,
CreateOptions: options,
})
if err != nil {
log.Fatalf("Failed to submit workflow: %v", err)
}
printWorkflow(created, cliOpts.output, DefaultStatus)
workflowNames = append(workflowNames, created.Name)
}
waitOrWatch(workflowNames, *cliOpts)
}
// unmarshalWorkflows unmarshals the input bytes as either json or yaml
func unmarshalWorkflows(wfBytes []byte, strict bool) []wfv1.Workflow {
var wf wfv1.Workflow
var jsonOpts []argoJson.JSONOpt
if strict {
jsonOpts = append(jsonOpts, argoJson.DisallowUnknownFields)
}
err := argoJson.Unmarshal(wfBytes, &wf, jsonOpts...)
if err == nil {
return []wfv1.Workflow{wf}
}
yamlWfs, err := common.SplitWorkflowYAMLFile(wfBytes, strict)
if err == nil {
return yamlWfs
}
log.Fatalf("Failed to parse workflow: %v", err)
return nil
}
func waitOrWatch(workflowNames []string, cliSubmitOpts cliSubmitOpts) {
if cliSubmitOpts.wait {
WaitWorkflows(workflowNames, false, !(cliSubmitOpts.output == "" || cliSubmitOpts.output == "wide"))
} else if cliSubmitOpts.watch {
watchWorkflow(workflowNames[0])
}
}