forked from jenkins-x-plugins/jx-pipeline
/
fmt.go
323 lines (293 loc) · 8.27 KB
/
fmt.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package fmt
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x/jx-helpers/v3/pkg/cmdrunner"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/helper"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient/cli"
"github.com/jenkins-x/jx-helpers/v3/pkg/options"
"github.com/jenkins-x/jx-helpers/v3/pkg/yamls"
"github.com/pkg/errors"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
v1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"
"github.com/spf13/cobra"
gojenkins "github.com/jenkins-x/golang-jenkins"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/templates"
)
// StopPipelineOptions contains the command line options
type Options struct {
options.BaseOptions
Dir string
Namespace string
GitCloneURL string
GitClonePullRequestURL string
CatalogSHA string
GitClient gitclient.Interface
CommandRunner cmdrunner.CommandRunner
Jobs map[string]gojenkins.Job
}
const (
pipelineCatalogGitURL = "https://github.com/jenkins-x/jx3-pipeline-catalog"
)
var (
cmdLong = templates.LongDesc(`
Formats the local pipeline files
* removes any unnecessary parameters
* converts any shell commands to use 'script:' notation
`)
cmdExample = templates.Examples(`
# Formats the local pipeline files
jx pipeline fmt
`)
removeStepNames = map[string]bool{
"git-clone": true,
"git-merge": true,
"git-setup": true,
"setup-builder-home": true,
}
shellBinaries = map[string]bool{
"/bin/ash": true,
"/bin/bash": true,
"/bin/sh": true,
"/busybox/sh": true,
}
)
// NewCmdPipelineFormat creates the command
func NewCmdPipelineFormat() (*cobra.Command, *Options) {
o := &Options{}
cmd := &cobra.Command{
Use: "fmt",
Short: "Formats the local pipeline files",
Long: cmdLong,
Example: cmdExample,
Aliases: []string{"kill"},
Run: func(cmd *cobra.Command, args []string) {
err := o.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&o.Dir, "dir", "d", ".lighthouse", "The directory to look for the tekton YAML files")
cmd.Flags().StringVarP(&o.CatalogSHA, "sha", "", "", "The git commit SHA of the pipeline catalog repository "+pipelineCatalogGitURL+". If not specified we clone git and find it")
return cmd, o
}
// Validate validate options
func (o *Options) Validate() error {
if o.GitClient == nil {
if o.CommandRunner == nil {
o.CommandRunner = cmdrunner.QuietCommandRunner
}
o.GitClient = cli.NewCLIClient("", o.CommandRunner)
}
if o.CatalogSHA == "" {
dir, err := gitclient.CloneToDir(o.GitClient, pipelineCatalogGitURL, "")
if err != nil {
return errors.Wrapf(err, "failed to clone %s", pipelineCatalogGitURL)
}
o.CatalogSHA, err = gitclient.GetLatestCommitSha(o.GitClient, dir)
if err != nil {
return errors.Wrapf(err, "failed to get latest commit sha from clone of %s in dir %s", pipelineCatalogGitURL, dir)
}
}
if o.GitCloneURL == "" {
o.GitCloneURL = "https://raw.githubusercontent.com/jenkins-x/jx3-pipeline-catalog/" + o.CatalogSHA + "/tasks/git-clone/git-clone.yaml"
}
if o.GitClonePullRequestURL == "" {
o.GitClonePullRequestURL = "https://raw.githubusercontent.com/jenkins-x/jx3-pipeline-catalog/" + o.CatalogSHA + "/tasks/git-clone/git-clone-pr.yaml"
}
return nil
}
// Run implements this command
func (o *Options) Run() error {
err := o.Validate()
if err != nil {
return errors.Wrapf(err, "failed to validate options")
}
dir := o.Dir
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info == nil || info.IsDir() {
return nil
}
if !strings.HasSuffix(path, ".yaml") {
return nil
}
return o.processFile(path)
})
if err != nil {
return errors.Wrapf(err, "failed to process YAML files in dir %s", dir)
}
return nil
}
func (o *Options) processFile(path string) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return errors.Wrapf(err, "failed to load file %s", path)
}
kindPrefix := "kind:"
kind := ""
lines := strings.Split(string(data), "\n")
for _, line := range lines {
if !strings.HasPrefix(line, kindPrefix) {
continue
}
k := strings.TrimSpace(line[len(kindPrefix):])
if k != "" {
kind = k
break
}
}
message := "processing file %s" + path
switch kind {
case "Pipeline":
pipeline := &v1beta1.Pipeline{}
err := yaml.Unmarshal(data, pipeline)
if err != nil {
return errors.Wrapf(err, "failed to unmarshal Pipeline YAML %s", message)
}
return o.processPipeline(pipeline, path)
case "PipelineRun":
prs := &v1beta1.PipelineRun{}
err := yaml.Unmarshal(data, prs)
if err != nil {
return errors.Wrapf(err, "failed to unmarshal PipelineRun YAML %s", message)
}
return o.processPipelineRun(prs, path)
case "Task":
task := &v1beta1.Task{}
err := yaml.Unmarshal(data, task)
if err != nil {
return errors.Wrapf(err, "failed to unmarshal Task YAML %s", message)
}
return o.processTask(task, path)
default:
return nil
}
}
func (o *Options) processPipelineRun(prs *v1beta1.PipelineRun, path string) error {
ps := &prs.Spec
_, name := filepath.Split(path)
prs.Name = strings.TrimSuffix(name, ".yaml")
prs.Labels = nil
if prs.Annotations == nil {
prs.Annotations = map[string]string{}
}
gitCloneURL := o.GitCloneURL
if prs.Name != "release" {
gitCloneURL = o.GitClonePullRequestURL
}
prs.Annotations["lighthouse.jenkins-x.io/prependStepsURL"] = gitCloneURL
if ps.PipelineSpec != nil {
err := o.processPipelineSpec(ps.PipelineSpec, path)
if err != nil {
return errors.Wrapf(err, "failed to ")
}
}
err := yamls.SaveFile(prs, path)
if err != nil {
return errors.Wrapf(err, "failed to save file %s", path)
}
return nil
}
func (o *Options) processPipelineSpec(spec *v1beta1.PipelineSpec, path string) error {
spec.Params = RemoveDefaultParamSpecs(spec.Params)
for i := range spec.Tasks {
task := &spec.Tasks[i]
task.Params = RemoveDefaultParams(task.Params)
ts := task.TaskSpec
if ts != nil {
ts.Params = RemoveDefaultParamSpecs(ts.Params)
var steps []v1beta1.Step
for j := range ts.Steps {
s := ts.Steps[j]
if !removeStepNames[s.Name] {
s = o.convertToScriptStep(s)
steps = append(steps, s)
}
}
ts.Steps = steps
}
ss := ts.StepTemplate
if ss != nil {
ss.Env = RemoveDefaultEnvVars(ss.Env)
}
}
return nil
}
func (o *Options) processPipeline(pipeline *v1beta1.Pipeline, path string) error {
err := o.processPipelineSpec(&pipeline.Spec, path)
if err != nil {
return errors.Wrapf(err, "failed to ")
}
err = yamls.SaveFile(pipeline, path)
if err != nil {
return errors.Wrapf(err, "failed to save file %s", path)
}
return nil
}
func (o *Options) processTask(task *v1beta1.Task, path string) error {
return nil
}
func (o *Options) convertToScriptStep(s v1beta1.Step) v1beta1.Step {
if len(s.Command) == 0 || len(s.Args) == 0 {
return s
}
bin := s.Command[0]
arg := ""
if bin == "jx" {
arg = "jx " + strings.Join(s.Args, " ")
bin = "/usr/bin/env bash"
} else {
if !shellBinaries[bin] {
return s
}
if len(s.Command) == 2 && s.Command[1] == "-c" && len(s.Args) == 1 {
arg = s.Args[0]
} else if len(s.Command) == 1 && len(s.Args) == 2 && s.Args[0] == "-c" {
arg = s.Args[1]
}
}
if arg == "" {
return s
}
if strings.HasPrefix(arg, "jx ") {
bin = "/usr/bin/env bash"
}
s.Command = nil
s.Args = nil
s.Script = "#!" + bin + "\n" + strings.ReplaceAll(arg, " && ", "\n") + "\n"
return s
}
// RemoveDefaultParamSpecs removes default parameters
func RemoveDefaultParamSpecs(from []v1alpha1.ParamSpec) []v1alpha1.ParamSpec {
var params []v1beta1.ParamSpec
for _, p := range from {
if !defaultParameterNames[p.Name] {
params = append(params, p)
}
}
return params
}
// RemoveDefaultParams removes default params
func RemoveDefaultParams(from []v1alpha1.Param) []v1alpha1.Param {
var params []v1beta1.Param
for _, p := range from {
if !defaultParameterNames[p.Name] {
params = append(params, p)
}
}
return params
}
// RemoveDefaultEnvVars removes default params
func RemoveDefaultEnvVars(from []corev1.EnvVar) []corev1.EnvVar {
var answer []corev1.EnvVar
for _, p := range from {
if !defaultParameterNames[p.Name] {
answer = append(answer, p)
}
}
return answer
}