Skip to content

User-defined rate-limiting key component #1155

@matheuscscp

Description

@matheuscscp

After RFC-0008, users have now the ability to define object-level annotations for sending custom metadata to notification providers. This could be used to give users a finer control over how events are rate-limited:

// 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",
"name=" + event.InvolvedObject.Name,
"namespace=" + event.InvolvedObject.Namespace,
"kind=" + event.InvolvedObject.Kind,
"message=" + event.Message,
}
objectGroup := event.InvolvedObject.GetObjectKind().GroupVersionKind().Group
originRevisionKey := fmt.Sprintf("%s/%s", objectGroup, eventv1.MetaOriginRevisionKey)
originRevision, ok := event.Metadata[originRevisionKey]
if ok {
comps = append(comps, "originRevision="+originRevision)
}
revisionKey := fmt.Sprintf("%s/%s", objectGroup, eventv1.MetaRevisionKey)
revision, ok := event.Metadata[revisionKey]
if ok {
comps = append(comps, "revision="+revision)
}
tokenKey := fmt.Sprintf("%s/%s", objectGroup, eventv1.MetaTokenKey)
token, ok := event.Metadata[tokenKey]
if ok {
comps = append(comps, "token="+token)
}
key := strings.Join(comps, "/")
digest := sha256.Sum256([]byte(key))
return fmt.Sprintf("%x", digest), nil
}

We could use the API eventv1.MetaTokenKey, that was created for this purpose. Today, this key is looked up using the object API Group:

tokenKey := fmt.Sprintf("%s/%s", objectGroup, eventv1.MetaTokenKey)
token, ok := event.Metadata[tokenKey]
if ok {
comps = append(comps, "token="+token)
}

We could look up with the new API Group defined in RFC-0008 the same way:

 tokenKey = fmt.Sprintf("%s/%s", eventv1.Group, eventv1.MetaTokenKey) 
 token, ok = event.Metadata[tokenKey] 
 if ok { 
 	comps = append(comps, "token="+token) 
 } 

Then both tokens can be appended to the rate-limiting key components.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/alertingAlerting related issues and PRsarea/apiAPI related issues and pull requestsenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions