generated from LinuxSuRen/.github
/
run.go
200 lines (176 loc) · 5.4 KB
/
run.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
package pipeline
import (
"bytes"
"context"
"fmt"
"github.com/kubesphere-sigs/ks/kubectl-plugin/common"
"github.com/kubesphere-sigs/ks/kubectl-plugin/pipeline/option"
"github.com/kubesphere-sigs/ks/kubectl-plugin/types"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
"text/template"
)
func newPipelineRunCmd() (cmd *cobra.Command) {
opt := &pipelineRunOpt{}
cmd = &cobra.Command{
Use: "run",
Short: "Start a Pipeline",
PreRunE: opt.preRunE,
RunE: opt.runE,
}
flags := cmd.Flags()
flags.StringVarP(&opt.pipeline, "pipeline", "p", "",
"The Pipeline name that you want to run")
flags.StringVarP(&opt.namespace, "namespace", "n", "",
"The namespace of target Pipeline")
flags.StringVarP(&opt.project, "project", "", "",
"The project of target Pipeline")
flags.BoolVarP(&opt.batch, "batch", "b", false, "Run pipeline as batch mode")
flags.StringToStringVarP(&opt.parameters, "parameters", "P", map[string]string{}, "The parameters that you want to pass, example of single parameter: name=value")
return
}
type pipelineRunOpt struct {
pipeline string
namespace string
project string
batch bool
parameters map[string]string
// inner fields
client dynamic.Interface
option.PipelineCreateOption
}
func (o *pipelineRunOpt) triggerPipeline(ns, pipeline string, parameters map[string]string) (err error) {
pipelineRunYaml, err := parsePipelineRunTpl(map[string]interface{}{
"name": pipeline,
"namespace": ns,
"parameters": parameters,
})
if err != nil {
return err
}
var pipelineRunObj *unstructured.Unstructured
if pipelineRunObj, err = types.GetObjectFromYaml(pipelineRunYaml); err != nil {
err = fmt.Errorf("failed to unmarshal yaml to Pipelinerun object, %v", err)
return
}
if _, err = o.client.Resource(types.GetPipelineRunSchema()).Namespace(ns).Create(context.TODO(),
pipelineRunObj, metav1.CreateOptions{}); err != nil {
err = fmt.Errorf("failed create PipelineRun, error: %v", err)
}
return
}
func (o *pipelineRunOpt) preRunE(cmd *cobra.Command, args []string) (err error) {
o.client = common.GetDynamicClient(cmd.Root().Context())
o.PipelineCreateOption.Client = o.client
if o.pipeline == "" && len(args) > 0 {
o.pipeline = args[0]
}
if o.project != "" {
var devopsProject *unstructured.Unstructured
if devopsProject, err = o.getDevOpsProjectByGenerateName(o.project); err == nil && devopsProject != nil {
o.namespace = devopsProject.GetName()
} else {
err = fmt.Errorf("unable to find namespace by devops project: %s, error: %v", o.project, err)
return
}
}
if err = o.wizard(cmd, args); err != nil {
return
}
return
}
func (o *pipelineRunOpt) runE(_ *cobra.Command, _ []string) (err error) {
err = o.triggerPipeline(o.namespace, o.pipeline, o.parameters)
return
}
func (o *pipelineRunOpt) wizard(_ *cobra.Command, _ []string) (err error) {
if o.batch {
// without wizard in batch mode
return
}
if o.Workspace == "" {
var wsNames []string
if wsNames, err = o.GetWorkspaceTemplateNameList(); err == nil {
if o.Workspace, err = option.ChooseObjectFromArray("workspace name", wsNames); err != nil {
return
}
} else {
return
}
}
if o.namespace == "" {
var projectNames []string
if projectNames, err = o.GetDevOpsNamespaceList(); err == nil {
if o.namespace, err = option.ChooseObjectFromArray("project name", projectNames); err != nil {
return
}
} else {
return
}
}
if o.pipeline == "" {
var pipelineNames []string
if pipelineNames, err = o.getPipelineNameList(); err == nil && len(pipelineNames) > 0 {
if o.pipeline, err = option.ChooseObjectFromArray("pipeline name", pipelineNames); err != nil {
return
}
} else if len(pipelineNames) == 0 {
err = fmt.Errorf("no Pipelines found in namespace '%s', please create it first", o.namespace)
return
}
}
return
}
func (o *pipelineRunOpt) getDevOpsProjectByGenerateName(name string) (result *unstructured.Unstructured, err error) {
ctx := context.TODO()
var projectList *unstructured.UnstructuredList
if projectList, err = o.Client.Resource(types.GetDevOpsProjectSchema()).List(ctx, metav1.ListOptions{}); err == nil {
for i := range projectList.Items {
item := projectList.Items[i]
if item.GetGenerateName() == name {
result = &item
return
}
}
}
return
}
func (o *pipelineRunOpt) getPipelineNameList() (names []string, err error) {
names, err = o.GetUnstructuredNameListInNamespace(o.namespace, true, []string{}, types.GetPipelineSchema())
return
}
func parsePipelineRunTpl(data map[string]interface{}) (pipelineRunYaml string, err error) {
var tpl *template.Template
if tpl, err = template.New("pipelineRunTpl").Parse(pipelineRunTpl); err != nil {
err = fmt.Errorf("failed to parse template:'%s', error: %v", pipelineRunTpl, err)
return
}
if err != nil {
return
}
var buf bytes.Buffer
if err = tpl.Execute(&buf, data); err != nil {
err = fmt.Errorf("failed to render pipeline template, error: %v", err)
return
}
return buf.String(), nil
}
var pipelineRunTpl = `
apiVersion: devops.kubesphere.io/v1alpha3
kind: PipelineRun
metadata:
generateName: {{.name}}
namespace: {{.namespace}}
spec:
pipelineRef:
name: {{.name}}
{{- if .parameters }}
parameters:
{{- range $name, $value := .parameters }}
- name: {{ $name | printf "%q" }}
value: {{ $value | printf "%q" }}
{{- end }}
{{- end }}
`