Skip to content

Commit 29d5fd0

Browse files
committed
Support commit status for any Flux APIs
Signed-off-by: Matheus Pimenta <matheuscscp@gmail.com>
1 parent 6b1a683 commit 29d5fd0

File tree

5 files changed

+65
-26
lines changed

5 files changed

+65
-26
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/elazarl/goproxy v1.8.0
1818
github.com/fluxcd/cli-utils v0.37.1-flux.1
1919
github.com/fluxcd/notification-controller/api v1.7.0
20-
github.com/fluxcd/pkg/apis/event v0.23.0
20+
github.com/fluxcd/pkg/apis/event v0.23.1-0.20260206173245-2ef53fa8e725
2121
github.com/fluxcd/pkg/apis/meta v1.25.0
2222
github.com/fluxcd/pkg/auth v0.36.0
2323
github.com/fluxcd/pkg/cache v0.13.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ github.com/fluxcd/cli-utils v0.37.1-flux.1 h1:WnG2mHxCPZMj/soIq/S/1zvbrGCJN3GJGb
137137
github.com/fluxcd/cli-utils v0.37.1-flux.1/go.mod h1:aND5wX3LuTFtB7eUT7vsWr8mmxRVSPR2Wkvbn0SqPfw=
138138
github.com/fluxcd/pkg/apis/acl v0.9.0 h1:wBpgsKT+jcyZEcM//OmZr9RiF8klL3ebrDp2u2ThsnA=
139139
github.com/fluxcd/pkg/apis/acl v0.9.0/go.mod h1:TttNS+gocsGLwnvmgVi3/Yscwqrjc17+vhgYfqkfrV4=
140-
github.com/fluxcd/pkg/apis/event v0.23.0 h1:4FsDb/bmukqA2TI6iYI0j4JSH3B+sVTXXsDngrAAgFA=
141-
github.com/fluxcd/pkg/apis/event v0.23.0/go.mod h1:Hoi4DejaNKVahGkRXqGBjT9h1aKmhc7RCYcsgoTieqc=
140+
github.com/fluxcd/pkg/apis/event v0.23.1-0.20260206173245-2ef53fa8e725 h1:pF99bHZOfypZmG3t5AIBJc4X4yT4x2SWVkks5QmHzGU=
141+
github.com/fluxcd/pkg/apis/event v0.23.1-0.20260206173245-2ef53fa8e725/go.mod h1:Hoi4DejaNKVahGkRXqGBjT9h1aKmhc7RCYcsgoTieqc=
142142
github.com/fluxcd/pkg/apis/kustomize v1.15.0 h1:p8wPIxdmn0vy0a664rsE9JKCfnliZz4HUsDcTy4ZOxA=
143143
github.com/fluxcd/pkg/apis/kustomize v1.15.0/go.mod h1:XWdsx8P15OiMaQIvmUjYWdmD3zAwhl5q9osl5iCqcOk=
144144
github.com/fluxcd/pkg/apis/meta v1.25.0 h1:fmZgMoe7yITGfhFqdOs7w2GOu3Y/2Vvz4+4p/eay3eA=

internal/server/event_handlers.go

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
8080
eventLogger.Info("dispatching event", "message", event.Message)
8181

8282
// Dispatch notifications.
83+
var droppedCommitStatusAlerts []*apiv1beta3.Alert
8384
var droppedChangeRequestAlerts []*apiv1beta3.Alert
8485
for i := range alerts {
8586
alert := &alerts[i]
@@ -90,17 +91,33 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
9091
"providerName": alert.Spec.ProviderRef.Name,
9192
})
9293
ctx := log.IntoContext(ctx, alertLogger)
93-
droppedChangeRequestProvider, err := s.dispatchNotification(ctx, event, alert)
94+
dropped, err := s.dispatchNotification(ctx, event, alert)
9495
if err != nil {
9596
alertLogger.Error(err, "failed to dispatch notification")
9697
s.Eventf(alert, corev1.EventTypeWarning, "NotificationDispatchFailed",
9798
"failed to dispatch notification for %s: %s", involvedObjectString(event.InvolvedObject), err)
99+
continue
98100
}
99-
if droppedChangeRequestProvider {
101+
if dropped.commitStatus {
102+
droppedCommitStatusAlerts = append(droppedCommitStatusAlerts, alert)
103+
}
104+
if dropped.changeRequest {
100105
droppedChangeRequestAlerts = append(droppedChangeRequestAlerts, alert)
101106
}
102107
}
103108

109+
// Log if any events were dropped due to being related to a commit status provider
110+
// but not having the required commit metadata key.
111+
if len(droppedCommitStatusAlerts) > 0 {
112+
var alertNames []string
113+
for _, alert := range droppedCommitStatusAlerts {
114+
alertNames = append(alertNames, fmt.Sprintf("%s/%s", alert.Namespace, alert.Name))
115+
}
116+
eventLogger.Info(
117+
"event dropped for commit status providers due to missing commit metadata key",
118+
"alerts", alertNames)
119+
}
120+
104121
// Log if any events were dropped due to being related to a change request comment
105122
// provider but not having the required change request metadata key.
106123
if len(droppedChangeRequestAlerts) > 0 {
@@ -223,13 +240,15 @@ func (s *EventServer) messageIsExcluded(ctx context.Context, msg string, alert *
223240
// and alert data. The returned boolean indicates if the event was dropped due
224241
// to being related to a change request provider but not having the required
225242
// change request metadata key.
226-
func (s *EventServer) dispatchNotification(ctx context.Context, event *eventv1.Event, alert *apiv1beta3.Alert) (bool, error) {
227-
params, droppedChangeRequestProvider, err := s.getNotificationParams(ctx, event, alert)
243+
func (s *EventServer) dispatchNotification(ctx context.Context,
244+
event *eventv1.Event, alert *apiv1beta3.Alert) (droppedProviders, error) {
245+
246+
params, dropped, err := s.getNotificationParams(ctx, event, alert)
228247
if err != nil {
229-
return false, err
248+
return droppedProviders{}, err
230249
}
231250
if params == nil {
232-
return droppedChangeRequestProvider, nil
251+
return dropped, nil
233252
}
234253

235254
go func(n notifier.Interface, e eventv1.Event) {
@@ -249,7 +268,7 @@ func (s *EventServer) dispatchNotification(ctx context.Context, event *eventv1.E
249268
}
250269
}(params.sender, *params.event)
251270

252-
return false, nil
271+
return droppedProviders{}, nil
253272
}
254273

255274
// notificationParams holds the results of the getNotificationParams function.
@@ -260,52 +279,67 @@ type notificationParams struct {
260279
timeout time.Duration
261280
}
262281

282+
// droppedProviders holds boolean values indicating whether the event was dropped
283+
// due to being related to a provider that requires a specific metadata key but
284+
// the event didn't have that key.
285+
type droppedProviders struct {
286+
commitStatus bool
287+
changeRequest bool
288+
}
289+
263290
// getNotificationParams constructs the notification parameters from the given
264291
// event and alert, and returns a notifier, event, token and timeout for sending
265292
// the notification. The returned event is a mutated form of the input event
266293
// based on the alert configuration. A boolean is also returned to indicate if
267294
// the event was dropped due to being related to a change request provider but
268295
// not having the required change request metadata key.
269296
func (s *EventServer) getNotificationParams(ctx context.Context, event *eventv1.Event,
270-
alert *apiv1beta3.Alert) (*notificationParams, bool, error) {
297+
alert *apiv1beta3.Alert) (*notificationParams, droppedProviders, error) {
271298
// Check if event comes from a different namespace.
272299
if s.noCrossNamespaceRefs && event.InvolvedObject.Namespace != alert.Namespace {
273300
accessDenied := fmt.Errorf(
274301
"alert '%s/%s' can't process event from '%s', cross-namespace references have been blocked",
275302
alert.Namespace, alert.Name, involvedObjectString(event.InvolvedObject))
276-
return nil, false, fmt.Errorf("discarding event, access denied to cross-namespace sources: %w", accessDenied)
303+
return nil, droppedProviders{}, fmt.Errorf("discarding event, access denied to cross-namespace sources: %w", accessDenied)
277304
}
278305

279306
var provider apiv1beta3.Provider
280307
providerName := types.NamespacedName{Namespace: alert.Namespace, Name: alert.Spec.ProviderRef.Name}
281308

282309
err := s.kubeClient.Get(ctx, providerName, &provider)
283310
if err != nil {
284-
return nil, false, fmt.Errorf("failed to read provider: %w", err)
311+
return nil, droppedProviders{}, fmt.Errorf("failed to read provider: %w", err)
285312
}
286313

287314
// Skip if the provider is suspended.
288315
if provider.Spec.Suspend {
289-
return nil, false, nil
316+
return nil, droppedProviders{}, nil
290317
}
291318

292319
// Skip if the event has commit status update metadata but the provider is not a git provider.
293320
// Git providers (github, gitlab, etc.) are the ones that set commit statuses.
294321
if !isCommitStatusProvider(provider.Spec.Type) && isCommitStatusUpdate(event) {
295-
return nil, false, nil
322+
return nil, droppedProviders{}, nil
323+
}
324+
325+
// Skip if the provider is a commit status provider but the event doesn't have the commit metadata key.
326+
if isCommitStatusProvider(provider.Spec.Type) && !hasCommitKey(event) {
327+
// Return true on dropped event for a commit status provider
328+
// when the event doesn't have the commit metadata key.
329+
return nil, droppedProviders{commitStatus: true}, nil
296330
}
297331

298-
// Skip if the provider is a change request comment provider but the event
332+
// Skip if the provider is a change request provider but the event
299333
// doesn't have the change request metadata key.
300-
if isChangeRequestCommentProvider(provider.Spec.Type) && !hasChangeRequestKey(event) {
301-
// Return true on dropped event for a change request comment provider
334+
if isChangeRequestProvider(provider.Spec.Type) && !hasChangeRequestKey(event) {
335+
// Return true on dropped event for a change request provider
302336
// when the event doesn't have the change request metadata key.
303-
return nil, true, nil
337+
return nil, droppedProviders{changeRequest: true}, nil
304338
}
305339

306340
// Check object-level workload identity feature gate.
307341
if provider.Spec.ServiceAccountName != "" && !auth.IsObjectLevelWorkloadIdentityEnabled() {
308-
return nil, false, fmt.Errorf(
342+
return nil, droppedProviders{}, fmt.Errorf(
309343
"to use spec.serviceAccountName for provider authentication please enable the %s feature gate in the controller",
310344
auth.FeatureGateObjectLevelWorkloadIdentity)
311345
}
@@ -317,20 +351,20 @@ func (s *EventServer) getNotificationParams(ctx context.Context, event *eventv1.
317351
// Create a commit status for the given provider and event, if applicable.
318352
commitStatus, err := createCommitStatus(ctx, &provider, &notification, alert)
319353
if err != nil {
320-
return nil, false, fmt.Errorf("failed to create commit status: %w", err)
354+
return nil, droppedProviders{}, fmt.Errorf("failed to create commit status: %w", err)
321355
}
322356

323357
sender, token, err := createNotifier(ctx, s.kubeClient, &provider, commitStatus, s.tokenCache)
324358
if err != nil {
325-
return nil, false, fmt.Errorf("failed to initialize notifier for provider '%s': %w", provider.Name, err)
359+
return nil, droppedProviders{}, fmt.Errorf("failed to initialize notifier for provider '%s': %w", provider.Name, err)
326360
}
327361

328362
return &notificationParams{
329363
sender: sender,
330364
event: &notification,
331365
token: token,
332366
timeout: provider.GetTimeout(),
333-
}, false, nil
367+
}, droppedProviders{}, nil
334368
}
335369

336370
// createCommitStatus creates a commit status for the given provider and event.

internal/server/provider_change_request.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222
apiv1beta3 "github.com/fluxcd/notification-controller/api/v1beta3"
2323
)
2424

25-
// isChangeRequestCommentProvider returns true if the provider type is a
26-
// change request comment provider.
27-
func isChangeRequestCommentProvider(providerType string) bool {
25+
// isChangeRequestProvider returns true if the provider type is a
26+
// change request provider.
27+
func isChangeRequestProvider(providerType string) bool {
2828
return providerType == apiv1beta3.GitHubPullRequestCommentProvider ||
2929
providerType == apiv1beta3.GitLabMergeRequestCommentProvider
3030
}

internal/server/provider_commit_status.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,8 @@ func isCommitStatusUpdate(event *eventv1.Event) bool {
108108
key := event.InvolvedObject.GetObjectKind().GroupVersionKind().Group + "/" + eventv1.MetaCommitStatusKey
109109
return event.Metadata[key] == eventv1.MetaCommitStatusUpdateValue
110110
}
111+
112+
// hasCommitKey returns true if the event has the commit metadata key.
113+
func hasCommitKey(event *eventv1.Event) bool {
114+
return event.Metadata[eventv1.Group+"/"+eventv1.MetaCommitKey] != ""
115+
}

0 commit comments

Comments
 (0)