-
Notifications
You must be signed in to change notification settings - Fork 786
/
controller_pipelinerunner.go
221 lines (192 loc) · 6.21 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
package cmd
import (
"encoding/json"
"fmt"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net/http"
"strconv"
"time"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
pipelineapi "github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
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
}
// PipelineRunRequest the request to trigger a pipeline run
type PipelineRunRequest struct {
GitURL string `json:"gitUrl,omitempty"`
Branch string `json:"branch,omitempty"`
Kind string `json:"kind,omitempty"`
Context string `json:"context,omitempty"`
}
// PipelineRunResponse the results of triggering a pipeline run
type PipelineRunResponse struct {
Resources []kube.ObjectReference `json:"resources,omitempty"`
}
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(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := ControllerPipelineRunnerOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
}
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)
},
}
options.addCommonFlags(cmd)
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.")
return cmd
}
// Run will implement this command
func (o *ControllerPipelineRunnerOptions) Run() error {
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("Serving build numbers 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) {
arguments := &PipelineRunRequest{}
err := o.unmarshalBody(w, r, arguments)
o.onError(err)
if err != nil {
return
}
if o.Verbose {
logrus.Infof("got payload %#v", arguments)
}
if arguments.GitURL == "" {
o.returnError("missing gitUrl property", w, r)
return
}
if arguments.Branch == "" {
arguments.Branch = "master"
}
if arguments.Kind == "" {
arguments.Kind = "release"
}
pr := &StepCreateTaskOptions{}
pr.CommonOptions = o.CommonOptions
// defaults
pr.SourceName = "source"
pr.Duration = time.Second * 20
pr.Trigger = string(pipelineapi.PipelineTriggerTypeManual)
pr.CloneGitURL = arguments.GitURL
pr.DeleteTempDir = true
pr.Context = arguments.Context
pr.Branch = arguments.Branch
pr.PipelineKind = arguments.Kind
err = pr.Run()
if err != nil {
o.returnError(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(message string, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
w.Write([]byte(message))
}