forked from argoproj/argo-events
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.go
241 lines (196 loc) · 7.62 KB
/
start.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
/*
Copyright 2018 BlackRock, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gitlab
import (
"encoding/json"
"io/ioutil"
"net/http"
"reflect"
"github.com/argoproj/argo-events/common"
"github.com/argoproj/argo-events/gateways"
"github.com/argoproj/argo-events/gateways/server"
"github.com/argoproj/argo-events/gateways/server/common/webhook"
"github.com/argoproj/argo-events/pkg/apis/events"
"github.com/argoproj/argo-events/pkg/apis/eventsources/v1alpha1"
"github.com/argoproj/argo-events/store"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/xanzy/go-gitlab"
corev1 "k8s.io/api/core/v1"
)
// controller controls the webhook operations
var (
controller = webhook.NewController()
)
// set up the activation and inactivation channels to control the state of routes.
func init() {
go webhook.ProcessRouteStatus(controller)
}
// getCredentials retrieves credentials to connect to GitLab
func (router *Router) getCredentials(keySelector *corev1.SecretKeySelector, namespace string) (*cred, error) {
token, err := store.GetSecrets(router.k8sClient, namespace, keySelector.Name, keySelector.Key)
if err != nil {
return nil, err
}
return &cred{
token: token,
}, nil
}
// Implement Router
// 1. GetRoute
// 2. HandleRoute
// 3. PostActivate
// 4. PostDeactivate
// GetRoute returns the route
func (router *Router) GetRoute() *webhook.Route {
return router.route
}
// HandleRoute handles incoming requests on the route
func (router *Router) HandleRoute(writer http.ResponseWriter, request *http.Request) {
route := router.GetRoute()
logger := route.Logger.WithFields(
map[string]interface{}{
common.LabelEventSource: route.EventSource.Name,
common.LabelEndpoint: route.Context.Endpoint,
common.LabelPort: route.Context.Port,
})
logger.Info("received a request, processing it...")
if !route.Active {
logger.Info("endpoint is not active, won't process the request")
common.SendErrorResponse(writer, "inactive endpoint")
return
}
body, err := ioutil.ReadAll(request.Body)
if err != nil {
logger.WithError(err).Error("failed to parse request body")
common.SendErrorResponse(writer, err.Error())
return
}
event := &events.GitLabEventData{
Headers: request.Header,
Body: (*json.RawMessage)(&body),
}
eventBody, err := json.Marshal(event)
if err != nil {
logger.Info("failed to marshal event")
common.SendErrorResponse(writer, "invalid event")
return
}
logger.Infoln("dispatching event on route's data channel")
route.DataCh <- eventBody
logger.Info("request successfully processed")
common.SendSuccessResponse(writer, "success")
}
// PostActivate performs operations once the route is activated and ready to consume requests
func (router *Router) PostActivate() error {
route := router.GetRoute()
gitlabEventSource := router.gitlabEventSource
// In order to set up a hook for the GitLab project,
// 1. Get the API access token for client
// 2. Set up GitLab client
// 3. Configure Hook with given event type
// 4. Create project hook
logger := route.Logger.WithFields(map[string]interface{}{
common.LabelEventSource: route.EventSource.Name,
"event-type": gitlabEventSource.Event,
"project-id": gitlabEventSource.ProjectId,
})
logger.Infoln("retrieving the access token credentials...")
c, err := router.getCredentials(gitlabEventSource.AccessToken, gitlabEventSource.Namespace)
if err != nil {
return errors.Errorf("failed to get gitlab credentials. err: %+v", err)
}
logger.Infoln("setting up the client to connect to GitLab...")
router.gitlabClient, err = gitlab.NewClient(c.token, gitlab.WithBaseURL(gitlabEventSource.GitlabBaseURL))
if err != nil {
return errors.Wrapf(err, "failed to initialize client")
}
formattedUrl := common.FormattedURL(gitlabEventSource.Webhook.URL, gitlabEventSource.Webhook.Endpoint)
// Get existing webhooks and check if the integration for same url and event type is already available
if !gitlabEventSource.AllowDuplicate {
hooks, _, err := router.gitlabClient.Projects.ListProjectHooks(router.gitlabEventSource.ProjectId, &gitlab.ListProjectHooksOptions{})
if err != nil {
return errors.Wrapf(err, "failed to list existing hooks to check for duplicates for project id %s", router.gitlabEventSource.ProjectId)
}
for _, hook := range hooks {
elem := reflect.ValueOf(hook).Elem().FieldByName(router.gitlabEventSource.Event)
if ok := elem.IsValid(); !ok {
return errors.Errorf("unknown event %s", router.gitlabEventSource.Event)
}
value := elem.Bool()
if value && hook.URL == formattedUrl {
logger.Infoln("webhook already exists, won't register it...")
return nil
}
}
}
opt := &gitlab.AddProjectHookOptions{
URL: &formattedUrl,
Token: &c.token,
EnableSSLVerification: &router.gitlabEventSource.EnableSSLVerification,
}
logger.Infoln("configuring the type of the GitLab event the hook must register against...")
elem := reflect.ValueOf(opt).Elem().FieldByName(router.gitlabEventSource.Event)
if ok := elem.IsValid(); !ok {
return errors.Errorf("unknown event %s", router.gitlabEventSource.Event)
}
iev := reflect.New(elem.Type().Elem())
reflect.Indirect(iev).SetBool(true)
elem.Set(iev)
logger.Infoln("creating project hook...")
hook, _, err := router.gitlabClient.Projects.AddProjectHook(router.gitlabEventSource.ProjectId, opt)
if err != nil {
return errors.Errorf("failed to add project hook. err: %+v", err)
}
router.hook = hook
logger.WithField("hook-id", hook.ID).Info("hook created for the project")
return nil
}
// PostInactivate performs operations after the route is inactivated
func (router *Router) PostInactivate() error {
gitlabEventSource := router.gitlabEventSource
route := router.route
if gitlabEventSource.DeleteHookOnFinish {
logger := route.Logger.WithFields(map[string]interface{}{
common.LabelEventSource: route.EventSource.Name,
"project-id": gitlabEventSource.ProjectId,
"hook-id": router.hook.ID,
})
logger.Infoln("deleting project hook...")
if _, err := router.gitlabClient.Projects.DeleteProjectHook(router.gitlabEventSource.ProjectId, router.hook.ID); err != nil {
return errors.Errorf("failed to delete hook. err: %+v", err)
}
logger.Infoln("gitlab hook deleted")
}
return nil
}
// StartEventSource starts an event source
func (listener *EventListener) StartEventSource(eventSource *gateways.EventSource, eventStream gateways.Eventing_StartEventSourceServer) error {
defer server.Recover(eventSource.Name)
logger := listener.Logger.WithField(common.LabelEventSource, eventSource.Name)
logger.Info("started processing the event source...")
var gitlabEventSource *v1alpha1.GitlabEventSource
if err := yaml.Unmarshal(eventSource.Value, &gitlabEventSource); err != nil {
logger.WithError(err).Error("failed to parse the event source")
return err
}
if gitlabEventSource.Namespace == "" {
gitlabEventSource.Namespace = listener.Namespace
}
route := webhook.NewRoute(gitlabEventSource.Webhook, listener.Logger, eventSource)
return webhook.ManageRoute(&Router{
route: route,
k8sClient: listener.K8sClient,
gitlabEventSource: gitlabEventSource,
}, controller, eventStream)
}