-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
lint.go
138 lines (129 loc) · 3.41 KB
/
lint.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
package commands
import (
"context"
"fmt"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"github.com/argoproj/argo/cmd/argo/commands/client"
"github.com/argoproj/argo/pkg/apiclient/workflow"
"github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
cmdutil "github.com/argoproj/argo/util/cmd"
"github.com/argoproj/argo/workflow/validate"
)
func NewLintCommand() *cobra.Command {
var (
strict bool
serverHost string
)
var command = &cobra.Command{
Use: "lint (DIRECTORY | FILE1 FILE2 FILE3...)",
Short: "validate a file or directory of workflow manifests",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}
var err error
wftmplGetter := &LazyWorkflowTemplateGetter{}
validateDir := cmdutil.MustIsDir(args[0])
if client.ArgoServer != "" {
conn := client.GetClientConn()
defer conn.Close()
err = ServerSideLint(args[0], conn, strict)
if err != nil {
return
}
} else {
if validateDir {
if len(args) > 1 {
fmt.Printf("Validation of a single directory supported")
os.Exit(1)
}
fmt.Printf("Verifying all workflow manifests in directory: %s\n", args[0])
err = validate.LintWorkflowDir(wftmplGetter, args[0], strict)
if err != nil {
return
}
} else {
yamlFiles := make([]string, 0)
for _, filePath := range args {
if cmdutil.MustIsDir(filePath) {
fmt.Printf("Validate against a list of files or a single directory, not both")
os.Exit(1)
}
yamlFiles = append(yamlFiles, filePath)
}
for _, yamlFile := range yamlFiles {
err = validate.LintWorkflowFile(wftmplGetter, yamlFile, strict)
if err != nil {
break
}
}
}
}
if err != nil {
log.Fatal(err)
}
fmt.Printf("Workflow manifests validated\n")
},
}
command.Flags().BoolVar(&strict, "strict", true, "perform strict workflow validatation")
command.Flags().StringVar(&serverHost, "server", "", "API Server host and port")
return command
}
func ServerSideLint(arg string, conn *grpc.ClientConn, strict bool) error {
validateDir := cmdutil.MustIsDir(arg)
grpcClient, ctx := GetWFApiServerGRPCClient(conn)
ns, _, _ := client.Config.Namespace()
var wfs []v1alpha1.Workflow
var err error
if validateDir {
walkFunc := func(path string, info os.FileInfo, err error) error {
if info == nil || info.IsDir() {
return nil
}
fileExt := filepath.Ext(info.Name())
switch fileExt {
case ".yaml", ".yml", ".json":
default:
return nil
}
wfs, err1 := validate.ParseWfFromFile(path, strict)
if err1 != nil {
return err1
}
for _, wf := range wfs {
err = ServerLintValidation(ctx, grpcClient, wf, ns)
if err != nil {
log.Errorf("Validation Error in %s :%v", path, err)
}
}
return err
}
return filepath.Walk(arg, walkFunc)
} else {
wfs, err = validate.ParseWfFromFile(arg, strict)
}
if err != nil {
log.Error(err)
return err
}
for _, wf := range wfs {
err = ServerLintValidation(ctx, grpcClient, wf, ns)
if err != nil {
log.Error(err)
}
}
return err
}
func ServerLintValidation(ctx context.Context, client workflow.WorkflowServiceClient, wf v1alpha1.Workflow, ns string) error {
wfReq := workflow.WorkflowLintRequest{
Namespace: ns,
Workflow: &wf,
}
_, err := client.LintWorkflow(ctx, &wfReq)
return err
}