forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint.go
262 lines (229 loc) · 6.37 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package lint
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterworkflowtemplatepkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/clusterworkflowtemplate"
cronworkflowpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/cronworkflow"
workflowpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/workflow"
workflowtemplatepkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/workflowtemplate"
wf "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/workflow/common"
)
type ServiceClients struct {
WorkflowsClient workflowpkg.WorkflowServiceClient
WorkflowTemplatesClient workflowtemplatepkg.WorkflowTemplateServiceClient
CronWorkflowsClient cronworkflowpkg.CronWorkflowServiceClient
ClusterWorkflowTemplateClient clusterworkflowtemplatepkg.ClusterWorkflowTemplateServiceClient
}
type LintOptions struct {
Files []string
Strict bool
DefaultNamespace string
Formatter Formatter
ServiceClients ServiceClients
}
// LintResult represents the result of linting objects from a single source
type LintResult struct {
File string
Errs []error
Linted bool
}
// LintResults represents the result of linting objects from multiple sources
type LintResults struct {
Results []*LintResult
Success bool
msg string
fmtr Formatter
anythingLinted bool
}
type Formatter interface {
Format(*LintResults) string
}
var (
lintExt = map[string]bool{
".yaml": true,
".yml": true,
".json": true,
}
defaultFormatter = formatterPretty{}
formatters = map[string]Formatter{
"pretty": formatterPretty{},
"simple": formatterSimple{},
}
)
func GetFormatter(fmtr string) (Formatter, error) {
f, exists := formatters[fmtr]
if !exists {
return nil, fmt.Errorf("unknown formatter: %s", fmtr)
}
return f, nil
}
// Lint reads all files, returns linting errors of all of the enitities of the specified kinds.
// Entities of other kinds are ignored.
func Lint(ctx context.Context, opts *LintOptions) (*LintResults, error) {
results := &LintResults{
Results: []*LintResult{},
fmtr: opts.Formatter,
}
for _, file := range opts.Files {
err := filepath.Walk(file, func(path string, info os.FileInfo, err error) error {
var r io.Reader
switch {
case path == "-":
path = "stdin"
r = os.Stdin
case err != nil:
return err
case lintExt[filepath.Ext(path)]:
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
r = f
case info.IsDir():
return nil // skip
default:
log.Warnf("ignoring file with unknown extension: %s", path)
return nil
}
data, err := ioutil.ReadAll(r)
if err != nil {
return err
}
results.Results = append(results.Results, lintData(ctx, path, data, opts))
return nil
})
if err != nil {
return nil, err
}
}
return results.evaluate(), nil
}
func lintData(ctx context.Context, src string, data []byte, opts *LintOptions) *LintResult {
res := &LintResult{
File: src,
Errs: []error{},
}
for i, pr := range common.ParseObjects(data, opts.Strict) {
obj, err := pr.Object, pr.Err
if obj == nil {
continue // could not parse to kubernetes object
}
// we should prefer the object's namespace
namespace := obj.GetNamespace()
if namespace == "" {
namespace = opts.DefaultNamespace
}
objName := ""
switch v := obj.(type) {
case *wfv1.ClusterWorkflowTemplate:
objName = getObjectName(wf.ClusterWorkflowTemplateKind, v, i)
if opts.ServiceClients.ClusterWorkflowTemplateClient == nil {
log.Debugf("ignoring %s, not in lint options", objName)
continue
}
res.Linted = true
if err == nil {
_, err = opts.ServiceClients.ClusterWorkflowTemplateClient.LintClusterWorkflowTemplate(
ctx,
&clusterworkflowtemplatepkg.ClusterWorkflowTemplateLintRequest{Template: v},
)
}
case *wfv1.CronWorkflow:
objName = getObjectName(wf.CronWorkflowKind, v, i)
if opts.ServiceClients.CronWorkflowsClient == nil {
log.Debugf("ignoring %s, not in lint options kinds", objName)
continue
}
res.Linted = true
if err == nil {
_, err = opts.ServiceClients.CronWorkflowsClient.LintCronWorkflow(
ctx,
&cronworkflowpkg.LintCronWorkflowRequest{Namespace: namespace, CronWorkflow: v},
)
}
case *wfv1.Workflow:
objName = getObjectName(wf.WorkflowKind, v, i)
if opts.ServiceClients.WorkflowsClient == nil {
log.Debugf("ignoring %s, not in lint options kinds", objName)
continue
}
res.Linted = true
if err == nil {
_, err = opts.ServiceClients.WorkflowsClient.LintWorkflow(
ctx,
&workflowpkg.WorkflowLintRequest{Namespace: namespace, Workflow: v},
)
}
case *wfv1.WorkflowEventBinding:
// noop
case *wfv1.WorkflowTemplate:
objName = getObjectName(wf.WorkflowTemplateKind, v, i)
if opts.ServiceClients.WorkflowTemplatesClient == nil {
log.Debugf("ignoring %s, not in lint options kinds", objName)
continue
}
res.Linted = true
if err == nil {
_, err = opts.ServiceClients.WorkflowTemplatesClient.LintWorkflowTemplate(
ctx,
&workflowtemplatepkg.WorkflowTemplateLintRequest{Namespace: namespace, Template: v},
)
}
default:
// silently ignore unknown kinds
}
if err != nil {
res.Errs = append(res.Errs, fmt.Errorf("in %s: %w", objName, err))
}
}
return res
}
func (l *LintResults) Msg() string {
return l.msg
}
// evaluate must be called before checking the value of l.Success and l.String()
func (l *LintResults) evaluate() *LintResults {
success := true
l.anythingLinted = false
for _, r := range l.Results {
if !r.Linted {
continue
}
l.anythingLinted = true
if len(r.Errs) == 0 {
continue
}
success = false
}
if !l.anythingLinted {
success = false
}
l.Success = success
var fmtr Formatter = defaultFormatter
if l.fmtr != nil {
fmtr = l.fmtr
}
l.msg = fmtr.Format(l)
return l
}
func getObjectName(kind string, obj metav1.Object, objIndex int) string {
name := ""
switch {
case obj.GetName() != "":
name = obj.GetName()
case obj.GetGenerateName() != "":
name = obj.GetGenerateName()
default:
name = fmt.Sprintf("object #%d", objIndex+1)
}
return fmt.Sprintf(`"%s" (%s)`, name, kind)
}