Skip to content

Commit

Permalink
fix fingerprinting and common enhancer call
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiahui-Zhang-20 committed Dec 20, 2023
1 parent ea40479 commit 289c61c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
41 changes: 26 additions & 15 deletions enhancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,19 @@ func runEnhancers(ctx context.Context, eventObject *v1.Event, kind string, objec
return err
}

// Might reset back to old fingerprint if
// there exists root owner(s) to the object
oldFingerprint := sentryEvent.Fingerprint
// Call the specific enhancer for the object
callObjectEnhancer(ctx, scope, &KindObjectPair{
kind,
object,
}, sentryEvent)
// Remove any fingerprinting so the event
// can be grouped by its owners instead
if len(rootOwners) != 0 {
sentryEvent.Fingerprint = oldFingerprint
}

Check warning on line 72 in enhancers.go

View check run for this annotation

Codecov / codecov/patch

enhancers.go#L71-L72

Added lines #L71 - L72 were not covered by tests

// Call specific enhancers for all root owners
// (there most likely is just one root owner)
Expand All @@ -79,15 +87,18 @@ type KindObjectPair struct {
object metav1.Object
}

// Finds the root owning objects of an object
// and returns an empty slice if the object has
// no owning objects
func findRootOwners(ctx context.Context, kindObjPair *KindObjectPair) ([]KindObjectPair, error) {

// use DFS to find the leaves of the owner references graph
// Use DFS to find the leaves of the owner references graph
rootOwners, err := ownerRefDFS(ctx, kindObjPair)
if err != nil {
return nil, err
}

// if the object has no owner references
// If the object has no owner references
if rootOwners[0].object.GetUID() == kindObjPair.object.GetUID() {
return []KindObjectPair{}, nil
}
Expand All @@ -96,7 +107,7 @@ func findRootOwners(ctx context.Context, kindObjPair *KindObjectPair) ([]KindObj

}

// this function finds performs DFS to find the leaves the owner references graph
// Performs DFS to find the leaves the owner references graph
func ownerRefDFS(ctx context.Context, kindObjPair *KindObjectPair) ([]KindObjectPair, error) {

parents := kindObjPair.object.GetOwnerReferences()
Expand Down Expand Up @@ -189,10 +200,10 @@ func podEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object,
nodeName := podObj.Spec.NodeName
setTagIfNotEmpty(scope, "node_name", nodeName)

// Add the cronjob to the fingerprint
// Add the pod name to the fingerprint
sentryEvent.Fingerprint = append(sentryEvent.Fingerprint, KindPod, podObj.Name)

// Add the cronjob to the tag
// Add the pod to the tag
setTagIfNotEmpty(scope, "pod_name", object.GetName())
podObj.ManagedFields = []metav1.ManagedFieldsEntry{}
metadataJson, err := prettyJson(podObj.ObjectMeta)
Expand All @@ -202,7 +213,7 @@ func podEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object,
})
}

// Add breadcrumb with cronjob timestamps
// Add breadcrumb with pod timestamps
scope.AddBreadcrumb(&sentry.Breadcrumb{
Message: fmt.Sprintf("Created pod %s", object.GetName()),
Level: sentry.LevelInfo,
Expand All @@ -221,10 +232,10 @@ func jobEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object,
return errors.New("failed to cast object to Job object")
}

// Add the cronjob to the fingerprint
// Add the job to the fingerprint
sentryEvent.Fingerprint = append(sentryEvent.Fingerprint, KindJob, jobObj.Name)

// Add the cronjob to the tag
// Add the job to the tag

Check warning on line 238 in enhancers.go

View check run for this annotation

Codecov / codecov/patch

enhancers.go#L238

Added line #L238 was not covered by tests
setTagIfNotEmpty(scope, "job_name", object.GetName())
jobObj.ManagedFields = []metav1.ManagedFieldsEntry{}
metadataJson, err := prettyJson(jobObj.ObjectMeta)
Expand All @@ -234,7 +245,7 @@ func jobEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object,
})
}

// Add breadcrumb with cronjob timestamps
// Add breadcrumb with job timestamps
scope.AddBreadcrumb(&sentry.Breadcrumb{
Message: fmt.Sprintf("Created job %s", object.GetName()),
Level: sentry.LevelInfo,
Expand Down Expand Up @@ -286,10 +297,10 @@ func replicaSetEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.
return errors.New("failed to cast object to ReplicaSet object")
}

// Add the cronjob to the fingerprint
// Add the replicaset to the fingerprint
sentryEvent.Fingerprint = append(sentryEvent.Fingerprint, KindReplicaset, replicasetObj.Name)

// Add the cronjob to the tag
// Add the replicaset to the tag

Check warning on line 303 in enhancers.go

View check run for this annotation

Codecov / codecov/patch

enhancers.go#L303

Added line #L303 was not covered by tests
setTagIfNotEmpty(scope, "replicaset_name", object.GetName())
replicasetObj.ManagedFields = []metav1.ManagedFieldsEntry{}
metadataJson, err := prettyJson(replicasetObj.ObjectMeta)
Expand All @@ -299,7 +310,7 @@ func replicaSetEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.
})
}

// Add breadcrumb with cronjob timestamps
// Add breadcrumb with replicaset timestamps
scope.AddBreadcrumb(&sentry.Breadcrumb{
Message: fmt.Sprintf("Created replicaset %s", object.GetName()),
Level: sentry.LevelInfo,
Expand All @@ -315,10 +326,10 @@ func deploymentEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.
if !ok {
return errors.New("failed to cast object to Deployment object")
}
// Add the cronjob to the fingerprint
// Add the deployment to the fingerprint
sentryEvent.Fingerprint = append(sentryEvent.Fingerprint, KindDeployment, deploymentObj.Name)

// Add the cronjob to the tag
// Add the deployment to the tag

Check warning on line 332 in enhancers.go

View check run for this annotation

Codecov / codecov/patch

enhancers.go#L332

Added line #L332 was not covered by tests
setTagIfNotEmpty(scope, "deployment_name", object.GetName())
deploymentObj.ManagedFields = []metav1.ManagedFieldsEntry{}
metadataJson, err := prettyJson(deploymentObj.ObjectMeta)
Expand All @@ -328,7 +339,7 @@ func deploymentEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.
})
}

// Add breadcrumb with cronjob timestamps
// Add breadcrumb with deployment timestamps
scope.AddBreadcrumb(&sentry.Breadcrumb{
Message: fmt.Sprintf("Created deployment %s", object.GetName()),
Level: sentry.LevelInfo,
Expand Down
7 changes: 5 additions & 2 deletions watcher_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,15 @@ func buildSentryEventFromGeneralEvent(ctx context.Context, event *v1.Event, scop

involvedObj, ok := findObject(ctx, event.InvolvedObject.Kind, event.InvolvedObject.Namespace, event.InvolvedObject.Name)

// cannot find event
// Cannot find the involved object related to the event
// note: this may mean the object is not a supported kind
// (e.g. Node, Service, StatefulSets)
if !ok {
runCommonEnhancer(ctx, scope, sentryEvent)
return sentryEvent
}

// run enhancers with the involved object
// Run enhancers with the involved object
runEnhancers(ctx, event, event.InvolvedObject.Kind, involvedObj, scope, sentryEvent)
return sentryEvent
}
Expand Down

0 comments on commit 289c61c

Please sign in to comment.