-
Notifications
You must be signed in to change notification settings - Fork 786
/
controller_pipelinerunner.go
310 lines (273 loc) · 9.2 KB
/
controller_pipelinerunner.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
package cmd
import (
"encoding/json"
"fmt"
"github.com/jenkins-x/jx/pkg/jenkinsfile"
"github.com/jenkins-x/jx/pkg/log"
"io/ioutil"
"net/http"
"strconv"
"time"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
pipelineapi "github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/spf13/cobra"
prowapi "k8s.io/test-infra/prow/apis/prowjobs/v1"
)
const (
// HealthPath is the URL path for the HTTP endpoint that returns health status.
HealthPath = "/health"
// ReadyPath URL path for the HTTP endpoint that returns ready status.
ReadyPath = "/ready"
)
// ControllerPipelineRunnerOptions holds the command line arguments
type ControllerPipelineRunnerOptions struct {
*CommonOptions
BindAddress string
Path string
Port int
NoGitCredeentialsInit bool
}
// PipelineRunRequest the request to trigger a pipeline run
type PipelineRunRequest struct {
Labels map[string]string `json:"labels,omitempty"`
ProwJobSpec prowapi.ProwJobSpec `json:"prowJobSpec,omitempty"`
}
// PipelineRunResponse the results of triggering a pipeline run
type PipelineRunResponse struct {
Resources []kube.ObjectReference `json:"resources,omitempty"`
}
// ObjectReference represents a reference to a k8s resource
type ObjectReference struct {
APIVersion string `json:"apiVersion" protobuf:"bytes,5,opt,name=apiVersion"`
// Kind of the referent.
// More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
Kind string `json:"kind" protobuf:"bytes,1,opt,name=kind"`
// Name of the referent.
// More info: http://kubernetes.io/docs/user-guide/identifiers#names
Name string `json:"name" protobuf:"bytes,3,opt,name=name"`
}
var (
controllerPipelineRunnersLong = templates.LongDesc(`Runs the service to generate Knative PipelineRun resources from source code webhooks`)
controllerPipelineRunnersExample = templates.Examples(`
# run the pipeline runner controller
jx controller pipelinerunner
`)
)
// NewCmdControllerPipelineRunner creates the command
func NewCmdControllerPipelineRunner(commonOpts *CommonOptions) *cobra.Command {
options := ControllerPipelineRunnerOptions{
CommonOptions: commonOpts,
}
cmd := &cobra.Command{
Use: "pipelinerunner",
Short: "Runs the service to generate Knative PipelineRun resources from source code webhooks",
Long: controllerPipelineRunnersLong,
Example: controllerPipelineRunnersExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().IntVarP(&options.Port, optionPort, "", 8080, "The TCP port to listen on.")
cmd.Flags().StringVarP(&options.BindAddress, optionBind, "", "",
"The interface address to bind to (by default, will listen on all interfaces/addresses).")
cmd.Flags().StringVarP(&options.Path, "path", "p", "/",
"The path to listen on for requests to trigger a pipeline run.")
cmd.Flags().StringVarP(&options.ServiceAccount, "service-account", "", "tekton-bot", "The Kubernetes ServiceAccount to use to run the pipeline")
cmd.Flags().BoolVarP(&options.NoGitCredeentialsInit, "no-git-init", "", false, "Disables checking we have setup git credentials on startup")
return cmd
}
// Run will implement this command
func (o *ControllerPipelineRunnerOptions) Run() error {
if !o.NoGitCredeentialsInit {
err := o.initGitConfigAndUser()
if err != nil {
return err
}
}
mux := http.NewServeMux()
mux.Handle(o.Path, http.HandlerFunc(o.piplineRunMethods))
mux.Handle(HealthPath, http.HandlerFunc(o.health))
mux.Handle(ReadyPath, http.HandlerFunc(o.ready))
logrus.Infof("Waiting for dynamic Tekton Pipelines at http://%s:%d%s", o.BindAddress, o.Port, o.Path)
return http.ListenAndServe(":"+strconv.Itoa(o.Port), mux)
}
// health returns either HTTP 204 if the service is healthy, otherwise nothing ('cos it's dead).
func (o *ControllerPipelineRunnerOptions) health(w http.ResponseWriter, r *http.Request) {
logrus.Debug("Health check")
w.WriteHeader(http.StatusNoContent)
}
// ready returns either HTTP 204 if the service is ready to serve requests, otherwise HTTP 503.
func (o *ControllerPipelineRunnerOptions) ready(w http.ResponseWriter, r *http.Request) {
logrus.Debug("Ready check")
if o.isReady() {
w.WriteHeader(http.StatusNoContent)
} else {
w.WriteHeader(http.StatusServiceUnavailable)
}
}
// handle request for pipeline runs
func (o *ControllerPipelineRunnerOptions) piplineRunMethods(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
fmt.Fprintf(w, "Please POST JSON to this endpoint!\n")
case http.MethodHead:
logrus.Info("HEAD Todo...")
case http.MethodPost:
o.startPipelineRun(w, r)
default:
logrus.Errorf("Unsupported method %s for %s", r.Method, o.Path)
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
}
// handle request for pipeline runs
func (o *ControllerPipelineRunnerOptions) startPipelineRun(w http.ResponseWriter, r *http.Request) {
err := o.stepGitCredentials()
if err != nil {
log.Warn(err.Error())
}
arguments := &PipelineRunRequest{}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
o.returnError(err, "could not read the JSON request body: "+err.Error(), w, r)
return
}
err = json.Unmarshal(data, arguments)
if err != nil {
o.returnError(err, "failed to unmarshal the JSON request body: "+err.Error(), w, r)
return
}
if err != nil {
o.returnError(err, "could not parse body: "+err.Error(), w, r)
return
}
if o.Verbose {
logrus.Infof("got payload %#v", arguments)
}
pj := arguments.ProwJobSpec
var revision string
var prNumber string
if pj.Refs == nil {
o.returnError(err, "no prowJobSpec.refs passed in so cannot determine git repository. Input: "+string(data), w, r)
return
}
// todo lets support batches of PRs from Prow
if len(pj.Refs.Pulls) > 0 {
revision = pj.Refs.Pulls[0].SHA
prNumber = strconv.Itoa(pj.Refs.Pulls[0].Number)
} else {
revision = pj.Refs.BaseSHA
}
sourceURL := fmt.Sprintf("https://github.com/%s/%s.git", pj.Refs.Org, pj.Refs.Repo)
if sourceURL == "" {
o.returnError(err, "missing sourceURL property", w, r)
return
}
if revision == "" {
revision = "master"
}
pr := &StepCreateTaskOptions{}
if pj.Type == prowapi.PostsubmitJob {
pr.PipelineKind = jenkinsfile.PipelineKindRelease
} else {
pr.PipelineKind = jenkinsfile.PipelineKindPullRequest
}
branch := getBranch(pj)
if branch == "" {
branch = "master"
}
pr.CommonOptions = o.CommonOptions
// defaults
pr.SourceName = "source"
pr.Duration = time.Second * 20
pr.Trigger = string(pipelineapi.PipelineTriggerTypeManual)
pr.PullRequestNumber = prNumber
pr.CloneGitURL = sourceURL
pr.DeleteTempDir = true
pr.Context = pj.Context
pr.Branch = branch
pr.Revision = revision
pr.ServiceAccount = o.ServiceAccount
// turn map into string array with = separator to match type of custom labels which are CLI flags
for key, value := range arguments.Labels {
pr.CustomLabels = append(pr.CustomLabels, fmt.Sprintf("%s=%s", key, value))
}
log.Infof("triggering pipeline for repo %s branch %s revision %s context %s\n", sourceURL, branch, revision, pj.Context)
err = pr.Run()
if err != nil {
o.returnError(err, err.Error(), w, r)
return
}
results := &PipelineRunResponse{
Resources: pr.Results.ObjectReferences(),
}
err = o.marshalPayload(w, r, results)
o.onError(err)
return
}
func (o *ControllerPipelineRunnerOptions) isReady() bool {
// TODO a better readiness check
return true
}
func (o *ControllerPipelineRunnerOptions) unmarshalBody(w http.ResponseWriter, r *http.Request, result interface{}) error {
// TODO assume JSON for now
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return errors.Wrap(err, "reading the JSON request body")
}
err = json.Unmarshal(data, result)
if err != nil {
return errors.Wrap(err, "unmarshalling the JSON request body")
}
return nil
}
func (o *ControllerPipelineRunnerOptions) marshalPayload(w http.ResponseWriter, r *http.Request, payload interface{}) error {
data, err := json.Marshal(payload)
if err != nil {
return errors.Wrapf(err, "marshalling the JSON payload %#v", payload)
}
w.Write(data)
return nil
}
func (o *ControllerPipelineRunnerOptions) onError(err error) {
if err != nil {
logrus.Errorf("%v", err)
}
}
func (o *ControllerPipelineRunnerOptions) returnError(err error, message string, w http.ResponseWriter, r *http.Request) {
o.onError(err)
w.WriteHeader(400)
w.Write([]byte(message))
}
func (o *ControllerPipelineRunnerOptions) stepGitCredentials() error {
if !o.NoGitCredeentialsInit {
copy := *o.CommonOptions
copy.BatchMode = true
gsc := &StepGitCredentialsOptions{
StepOptions: StepOptions{
CommonOptions: ©,
},
}
err := gsc.Run()
if err != nil {
return errors.Wrapf(err, "failed to run: jx step gc credentials")
}
}
return nil
}
func getBranch(spec prowapi.ProwJobSpec) string {
branch := spec.Refs.BaseRef
if spec.Type == prowapi.PostsubmitJob || spec.Type == prowapi.BatchJob {
return branch
}
if len(spec.Refs.Pulls) > 0 {
// todo lets support multiple PRs for when we are running a batch from Tide
branch = fmt.Sprintf("PR-%v", spec.Refs.Pulls[0].Number)
}
return branch
}