Skip to content

Commit ae1262c

Browse files
webhooks: only pull project for which request was received (#793)
If a webhook request's project matches a wildcard, pull only that project instead of pulling the entire wildcard (which can be expensive for wildcards matching many projects).
1 parent 5aca2a0 commit ae1262c

File tree

5 files changed

+50
-8
lines changed

5 files changed

+50
-8
lines changed

pkg/controller/controller.go

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func (c *Controller) registerTasks() {
7777
schemas.TaskTypePullEnvironmentsFromProject: c.TaskHandlerPullEnvironmentsFromProject,
7878
schemas.TaskTypePullEnvironmentsFromProjects: c.TaskHandlerPullEnvironmentsFromProjects,
7979
schemas.TaskTypePullMetrics: c.TaskHandlerPullMetrics,
80+
schemas.TaskTypePullProject: c.TaskHandlerPullProject,
8081
schemas.TaskTypePullProjectsFromWildcard: c.TaskHandlerPullProjectsFromWildcard,
8182
schemas.TaskTypePullProjectsFromWildcards: c.TaskHandlerPullProjectsFromWildcards,
8283
schemas.TaskTypePullRefMetrics: c.TaskHandlerPullRefMetrics,

pkg/controller/projects.go

+31
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,37 @@ import (
99
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/schemas"
1010
)
1111

12+
// PullProject ..
13+
func (c *Controller) PullProject(ctx context.Context, name string) error {
14+
gp, err := c.Gitlab.GetProject(ctx, name)
15+
if err != nil {
16+
return err
17+
}
18+
p := schemas.NewProject(gp.PathWithNamespace)
19+
20+
projectExists, err := c.Store.ProjectExists(ctx, p.Key())
21+
if err != nil {
22+
return err
23+
}
24+
25+
if !projectExists {
26+
log.WithFields(log.Fields{
27+
"project-name": p.Name,
28+
}).Info("discovered new project")
29+
30+
if err := c.Store.SetProject(ctx, p); err != nil {
31+
log.WithContext(ctx).
32+
WithError(err).
33+
Error()
34+
}
35+
36+
c.ScheduleTask(ctx, schemas.TaskTypePullRefsFromProject, string(p.Key()), p)
37+
c.ScheduleTask(ctx, schemas.TaskTypePullEnvironmentsFromProject, string(p.Key()), p)
38+
}
39+
40+
return nil
41+
}
42+
1243
// PullProjectsFromWildcard ..
1344
func (c *Controller) PullProjectsFromWildcard(ctx context.Context, w config.Wildcard) error {
1445
foundProjects, err := c.Gitlab.ListProjects(ctx, w)

pkg/controller/scheduler.go

+7
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ func NewTaskController(ctx context.Context, r *redis.Client, maximumJobsQueueSiz
7171
return
7272
}
7373

74+
// TaskHandlerPullProject ..
75+
func (c *Controller) TaskHandlerPullProject(ctx context.Context, name string) error {
76+
defer c.unqueueTask(ctx, schemas.TaskTypePullProject, name)
77+
78+
return c.PullProject(ctx, name)
79+
}
80+
7481
// TaskHandlerPullProjectsFromWildcard ..
7582
func (c *Controller) TaskHandlerPullProjectsFromWildcard(ctx context.Context, id string, w config.Wildcard) error {
7683
defer c.unqueueTask(ctx, schemas.TaskTypePullProjectsFromWildcard, id)

pkg/controller/webhooks.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ func (c *Controller) triggerRefMetricsPull(ctx context.Context, ref schemas.Ref)
155155

156156
// Perhaps the project is discoverable through a wildcard
157157
if !projectExists && len(c.Config.Wildcards) > 0 {
158-
for id, w := range c.Config.Wildcards {
158+
for _, w := range c.Config.Wildcards {
159159
// If in all our wildcards we have one which can potentially match the project ref
160-
// received, we trigger a scan
160+
// received, we trigger a pull of the project
161161
matches, err := isRefMatchingWilcard(w, ref)
162162
if err != nil {
163163
log.WithContext(ctx).
@@ -168,8 +168,8 @@ func (c *Controller) triggerRefMetricsPull(ctx context.Context, ref schemas.Ref)
168168
}
169169

170170
if matches {
171-
c.ScheduleTask(context.TODO(), schemas.TaskTypePullProjectsFromWildcard, strconv.Itoa(id), strconv.Itoa(id), w)
172-
log.WithFields(logFields).Info("project ref not currently exported but its configuration matches a wildcard, triggering a pull of the projects from this wildcard")
171+
c.ScheduleTask(context.TODO(), schemas.TaskTypePullProject, ref.Project.Name)
172+
log.WithFields(logFields).Info("project ref not currently exported but its configuration matches a wildcard, triggering a pull of the project")
173173
} else {
174174
log.WithFields(logFields).Debug("project ref not matching wildcard, skipping..")
175175
}
@@ -269,9 +269,9 @@ func (c *Controller) triggerEnvironmentMetricsPull(ctx context.Context, env sche
269269

270270
// Perhaps the project is discoverable through a wildcard
271271
if !projectExists && len(c.Config.Wildcards) > 0 {
272-
for id, w := range c.Config.Wildcards {
272+
for _, w := range c.Config.Wildcards {
273273
// If in all our wildcards we have one which can potentially match the env
274-
// received, we trigger a scan
274+
// received, we trigger a pull of the project
275275
matches, err := isEnvMatchingWilcard(w, env)
276276
if err != nil {
277277
log.WithContext(ctx).
@@ -282,8 +282,8 @@ func (c *Controller) triggerEnvironmentMetricsPull(ctx context.Context, env sche
282282
}
283283

284284
if matches {
285-
c.ScheduleTask(ctx, schemas.TaskTypePullProjectsFromWildcard, strconv.Itoa(id), strconv.Itoa(id), w)
286-
log.WithFields(logFields).Info("project environment not currently exported but its configuration matches a wildcard, triggering a pull of the projects from this wildcard")
285+
c.ScheduleTask(context.TODO(), schemas.TaskTypePullProject, env.ProjectName)
286+
log.WithFields(logFields).Info("project environment not currently exported but its configuration matches a wildcard, triggering a pull of the project")
287287
} else {
288288
log.WithFields(logFields).Debug("project ref not matching wildcard, skipping..")
289289
}

pkg/schemas/tasks.go

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ package schemas
44
type TaskType string
55

66
const (
7+
// TaskTypePullProject ..
8+
TaskTypePullProject TaskType = "PullProject"
9+
710
// TaskTypePullProjectsFromWildcard ..
811
TaskTypePullProjectsFromWildcard TaskType = "PullProjectsFromWildcard"
912

0 commit comments

Comments
 (0)