Skip to content

Commit ffb281a

Browse files
AnesBenmerzougjessesuen
authored andcommitted
Small code cleanup and add tests (argoproj#1562)
1 parent 1cb8345 commit ffb281a

File tree

5 files changed

+228
-79
lines changed

5 files changed

+228
-79
lines changed

cmd/argo/commands/submit.go

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package commands
22

33
import (
4-
"bufio"
5-
"io/ioutil"
64
"log"
7-
"net/http"
85
"os"
96
"strconv"
107

@@ -14,7 +11,6 @@ import (
1411
apimachineryversion "k8s.io/apimachinery/pkg/version"
1512

1613
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
17-
cmdutil "github.com/argoproj/argo/util/cmd"
1814
"github.com/argoproj/argo/workflow/common"
1915
"github.com/argoproj/argo/workflow/util"
2016
)
@@ -81,37 +77,16 @@ func SubmitWorkflows(filePaths []string, submitOpts *util.SubmitOpts, cliOpts *c
8177
cliOpts = &cliSubmitOpts{}
8278
}
8379
defaultWFClient := InitWorkflowClient()
80+
81+
fileContents, err := util.ReadManifest(filePaths...)
82+
if err != nil {
83+
log.Fatal(err)
84+
}
85+
8486
var workflows []wfv1.Workflow
85-
if len(filePaths) == 1 && filePaths[0] == "-" {
86-
reader := bufio.NewReader(os.Stdin)
87-
body, err := ioutil.ReadAll(reader)
88-
if err != nil {
89-
log.Fatal(err)
90-
}
91-
workflows = unmarshalWorkflows(body, cliOpts.strict)
92-
} else {
93-
for _, filePath := range filePaths {
94-
var body []byte
95-
var err error
96-
if cmdutil.IsURL(filePath) {
97-
response, err := http.Get(filePath)
98-
if err != nil {
99-
log.Fatal(err)
100-
}
101-
body, err = ioutil.ReadAll(response.Body)
102-
_ = response.Body.Close()
103-
if err != nil {
104-
log.Fatal(err)
105-
}
106-
} else {
107-
body, err = ioutil.ReadFile(filePath)
108-
if err != nil {
109-
log.Fatal(err)
110-
}
111-
}
112-
wfs := unmarshalWorkflows(body, cliOpts.strict)
113-
workflows = append(workflows, wfs...)
114-
}
87+
for _, body := range fileContents {
88+
wfs := unmarshalWorkflows(body, cliOpts.strict)
89+
workflows = append(workflows, wfs...)
11590
}
11691

11792
if cliOpts.watch {

cmd/argo/commands/template/create.go

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package template
22

33
import (
4-
"bufio"
5-
"io/ioutil"
64
"log"
7-
"net/http"
85
"os"
96

107
"github.com/argoproj/pkg/json"
118
"github.com/spf13/cobra"
129

1310
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
14-
cmdutil "github.com/argoproj/argo/util/cmd"
1511
"github.com/argoproj/argo/workflow/common"
12+
"github.com/argoproj/argo/workflow/util"
1613
"github.com/argoproj/argo/workflow/validate"
1714
)
1815

@@ -47,37 +44,16 @@ func CreateWorkflowTemplates(filePaths []string, cliOpts *cliCreateOpts) {
4744
cliOpts = &cliCreateOpts{}
4845
}
4946
defaultWFTmplClient := InitWorkflowTemplateClient()
47+
48+
fileContents, err := util.ReadManifest(filePaths...)
49+
if err != nil {
50+
log.Fatal(err)
51+
}
52+
5053
var workflowTemplates []wfv1.WorkflowTemplate
51-
if len(filePaths) == 1 && filePaths[0] == "-" {
52-
reader := bufio.NewReader(os.Stdin)
53-
body, err := ioutil.ReadAll(reader)
54-
if err != nil {
55-
log.Fatal(err)
56-
}
57-
workflowTemplates = unmarshalWorkflowTemplates(body, cliOpts.strict)
58-
} else {
59-
for _, filePath := range filePaths {
60-
var body []byte
61-
var err error
62-
if cmdutil.IsURL(filePath) {
63-
response, err := http.Get(filePath)
64-
if err != nil {
65-
log.Fatal(err)
66-
}
67-
body, err = ioutil.ReadAll(response.Body)
68-
_ = response.Body.Close()
69-
if err != nil {
70-
log.Fatal(err)
71-
}
72-
} else {
73-
body, err = ioutil.ReadFile(filePath)
74-
if err != nil {
75-
log.Fatal(err)
76-
}
77-
}
78-
wftmpls := unmarshalWorkflowTemplates(body, cliOpts.strict)
79-
workflowTemplates = append(workflowTemplates, wftmpls...)
80-
}
54+
for _, body := range fileContents {
55+
wftmpls := unmarshalWorkflowTemplates(body, cliOpts.strict)
56+
workflowTemplates = append(workflowTemplates, wftmpls...)
8157
}
8258

8359
if len(workflowTemplates) == 0 {

util/file/fileutil_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/argoproj/argo/util/file"
1212
)
1313

14-
// TestResubmitWorkflowWithOnExit ensures we do not carry over the onExit node even if successful
14+
// TestCompressContentString ensures compressing then decompressing a content string works as expected
1515
func TestCompressContentString(t *testing.T) {
1616
content := "{\"pod-limits-rrdm8-591645159\":{\"id\":\"pod-limits-rrdm8-591645159\",\"name\":\"pod-limits-rrdm8[0]." +
1717
"run-pod(0:0)\",\"displayName\":\"run-pod(0:0)\",\"type\":\"Pod\",\"templateName\":\"run-pod\",\"phase\":" +

workflow/util/util.go

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package util
22

33
import (
4+
"bufio"
45
"encoding/json"
56
"fmt"
67
"io/ioutil"
78
"math/rand"
89
"net/http"
10+
"os"
911
"regexp"
1012
"strings"
1113
"time"
@@ -197,12 +199,7 @@ func SubmitWorkflow(wfIf v1alpha1.WorkflowInterface, wfClientset wfclientset.Int
197199
var body []byte
198200
var err error
199201
if cmdutil.IsURL(opts.ParameterFile) {
200-
response, err := http.Get(opts.ParameterFile)
201-
if err != nil {
202-
return nil, errors.InternalWrapError(err)
203-
}
204-
body, err = ioutil.ReadAll(response.Body)
205-
_ = response.Body.Close()
202+
body, err = ReadFromUrl(opts.ParameterFile)
206203
if err != nil {
207204
return nil, errors.InternalWrapError(err)
208205
}
@@ -258,14 +255,14 @@ func SubmitWorkflow(wfIf v1alpha1.WorkflowInterface, wfClientset wfclientset.Int
258255
return nil, err
259256
}
260257

261-
if opts.ServerDryRun {
258+
if opts.DryRun {
259+
return wf, nil
260+
} else if opts.ServerDryRun {
262261
wf, err := CreateServerDryRun(wf, wfClientset)
263262
if err != nil {
264263
return nil, err
265264
}
266265
return wf, err
267-
} else if opts.DryRun {
268-
return wf, nil
269266
} else {
270267
return wfIf.Create(wf)
271268
}
@@ -535,6 +532,7 @@ func IsWorkflowSuspended(wf *wfv1.Workflow) bool {
535532
return false
536533
}
537534

535+
// IsWorkflowTerminated returns whether or not a workflow is considered terminated
538536
func IsWorkflowTerminated(wf *wfv1.Workflow) bool {
539537
if wf.Spec.ActiveDeadlineSeconds != nil && *wf.Spec.ActiveDeadlineSeconds == 0 {
540538
return true
@@ -583,3 +581,68 @@ func DecompressWorkflow(wf *wfv1.Workflow) error {
583581
}
584582
return nil
585583
}
584+
585+
// Reads from stdin
586+
func ReadFromStdin() ([]byte, error) {
587+
reader := bufio.NewReader(os.Stdin)
588+
body, err := ioutil.ReadAll(reader)
589+
if err != nil {
590+
return []byte{}, err
591+
}
592+
return body, err
593+
}
594+
595+
// Reads the content of a url
596+
func ReadFromUrl(url string) ([]byte, error) {
597+
response, err := http.Get(url)
598+
if err != nil {
599+
return nil, err
600+
}
601+
body, err := ioutil.ReadAll(response.Body)
602+
_ = response.Body.Close()
603+
if err != nil {
604+
return nil, err
605+
}
606+
return body, err
607+
}
608+
609+
// ReadFromFilePathsOrUrls reads the content of a single or a list of file paths and/or urls
610+
func ReadFromFilePathsOrUrls(filePathsOrUrls ...string) ([][]byte, error) {
611+
var fileContents [][]byte
612+
var body []byte
613+
var err error
614+
for _, filePathOrUrl := range filePathsOrUrls {
615+
if cmdutil.IsURL(filePathOrUrl) {
616+
body, err = ReadFromUrl(filePathOrUrl)
617+
if err != nil {
618+
return [][]byte{}, err
619+
}
620+
} else {
621+
body, err = ioutil.ReadFile(filePathOrUrl)
622+
if err != nil {
623+
return [][]byte{}, err
624+
}
625+
}
626+
fileContents = append(fileContents, body)
627+
}
628+
return fileContents, err
629+
}
630+
631+
// ReadManifest reads from stdin, a single file/url, or a list of files and/or urls
632+
func ReadManifest(manifestPaths ...string) ([][]byte, error) {
633+
var manifestContents [][]byte
634+
var err error
635+
if len(manifestPaths) == 1 && manifestPaths[0] == "-" {
636+
body, err := ReadFromStdin()
637+
if err != nil {
638+
return [][]byte{}, err
639+
}
640+
manifestContents = append(manifestContents, body)
641+
} else {
642+
manifestContents, err = ReadFromFilePathsOrUrls(manifestPaths...)
643+
if err != nil {
644+
return [][]byte{}, err
645+
}
646+
}
647+
return manifestContents, err
648+
}

0 commit comments

Comments
 (0)