-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit.go
152 lines (141 loc) · 4.66 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
package commands
import (
"bufio"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/argoproj/pkg/json"
"github.com/spf13/cobra"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
cmdutil "github.com/argoproj/argo/util/cmd"
"github.com/argoproj/argo/workflow/common"
"github.com/argoproj/argo/workflow/util"
)
// cliSubmitOpts holds submition 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().StringVarP(&submitOpts.ParameterFile, "parameter-file", "f", "", "pass a file containing all input parameters")
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().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")
return command
}
func SubmitWorkflows(filePaths []string, submitOpts *util.SubmitOpts, cliOpts *cliSubmitOpts) {
if submitOpts == nil {
submitOpts = &util.SubmitOpts{}
}
if cliOpts == nil {
cliOpts = &cliSubmitOpts{}
}
InitWorkflowClient()
var workflows []wfv1.Workflow
if len(filePaths) == 1 && filePaths[0] == "-" {
reader := bufio.NewReader(os.Stdin)
body, err := ioutil.ReadAll(reader)
if err != nil {
log.Fatal(err)
}
workflows = unmarshalWorkflows(body, cliOpts.strict)
} else {
for _, filePath := range filePaths {
var body []byte
var err error
if cmdutil.IsURL(filePath) {
response, err := http.Get(filePath)
if err != nil {
log.Fatal(err)
}
body, err = ioutil.ReadAll(response.Body)
_ = response.Body.Close()
if err != nil {
log.Fatal(err)
}
} else {
body, err = ioutil.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}
}
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")
}
}
var workflowNames []string
for _, wf := range workflows {
wf.Spec.Priority = cliOpts.priority
created, err := util.SubmitWorkflow(wfClient, &wf, submitOpts)
if err != nil {
log.Fatalf("Failed to submit workflow: %v", err)
}
printWorkflow(created, cliOpts.output)
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 []json.JSONOpt
if strict {
jsonOpts = append(jsonOpts, json.DisallowUnknownFields)
}
err := json.Unmarshal(wfBytes, &wf, jsonOpts...)
if err == nil {
return []wfv1.Workflow{wf}
}
yamlWfs, err := common.SplitYAMLFile(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])
}
}