Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct fingerprinting and common enhancer call #78

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions enhancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,19 @@
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 @@
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 @@

}

// 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 @@
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 @@
})
}

// 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 @@
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 @@
})
}

// 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 @@
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 @@
})
}

// 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 @@
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 @@
})
}

// 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)
Jiahui-Zhang-20 marked this conversation as resolved.
Show resolved Hide resolved
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