Skip to content

Commit

Permalink
Merge pull request #534 from fluxcd/prefix-event-key
Browse files Browse the repository at this point in the history
Prefix event key attributes with identifier
  • Loading branch information
hiddeco committed May 24, 2023
2 parents 639d66d + b7cccbe commit 3581abc
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions internal/server/event_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,28 +190,35 @@ func (s *EventServer) logRateLimitMiddleware(h http.Handler) http.Handler {
})
}

// eventKeyFunc generates a unique key for an event based on the provided HTTP
// request, which can be used to deduplicate events. The key is calculated by
// concatenating specific event attributes and hashing them using SHA-256.
// The key is then returned as a hex-encoded string.
//
// The event attributes are prefixed with an identifier to avoid collisions
// between different event attributes.
func eventKeyFunc(r *http.Request) (string, error) {
event := r.Context().Value(eventContextKey{}).(*eventv1.Event)

comps := []string{
"event",
event.InvolvedObject.Name,
event.InvolvedObject.Namespace,
event.InvolvedObject.Kind,
event.Message,
"name=" + event.InvolvedObject.Name,
"namespace=" + event.InvolvedObject.Namespace,
"kind=" + event.InvolvedObject.Kind,
"message=" + event.Message,
}

revision, ok := event.Metadata[eventv1.MetaRevisionKey]
if ok {
comps = append(comps, revision)
comps = append(comps, "revision="+revision)
}

token, ok := event.Metadata[eventv1.MetaTokenKey]
if ok {
comps = append(comps, token)
comps = append(comps, "token="+token)
}

val := strings.Join(comps, "/")
digest := sha256.Sum256([]byte(val))
key := strings.Join(comps, "/")
digest := sha256.Sum256([]byte(key))
return fmt.Sprintf("%x", digest), nil
}

0 comments on commit 3581abc

Please sign in to comment.