Skip to content

Commit

Permalink
refactor pod enhancer so all workload resource types are used
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiahui-Zhang-20 committed Dec 14, 2023
1 parent 9086675 commit c5831d8
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 187 deletions.
87 changes: 0 additions & 87 deletions crons.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package main
import (
"context"
"errors"
"fmt"
"time"

"github.com/getsentry/sentry-go"
"github.com/rs/zerolog"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache"
)
Expand Down Expand Up @@ -197,87 +194,3 @@ func checkinJobEnding(ctx context.Context, job *batchv1.Job, cronsMonitorData *C
)
return nil
}

// Adds to the sentry events whenever it is associated with a cronjob
// so the sentry event contains the corresponding slug monitor, cronjob name, timestamp of when the cronjob began, and
// the k8s cronjob metadata
func runCronsDataHandler(ctx context.Context, scope *sentry.Scope, pod *v1.Pod, sentryEvent *sentry.Event) (bool, error) {

// get owningCronJob if exists
owningCronJob, err := getOwningCronJob(ctx, pod)
if err != nil {
return false, err
}

// pod not part of a cronjob
if owningCronJob == nil {
return false, nil
}

scope.SetContext("Monitor", sentry.Context{
"Slug": owningCronJob.Name,
})

sentryEvent.Fingerprint = append(sentryEvent.Fingerprint, owningCronJob.Kind, owningCronJob.Name)

setTagIfNotEmpty(scope, "cronjob_name", owningCronJob.Name)

// add breadcrumb with cronJob timestamps
scope.AddBreadcrumb(&sentry.Breadcrumb{
Message: fmt.Sprintf("Created cronjob %s", owningCronJob.Name),
Level: sentry.LevelInfo,
Timestamp: owningCronJob.CreationTimestamp.Time,
}, breadcrumbLimit)

metadataJson, err := prettyJson(owningCronJob.ObjectMeta)

if err == nil {
scope.SetContext("Cronjob", sentry.Context{
"Metadata": metadataJson,
})
} else {
return false, err
}

return true, nil
}

// returns the cronjob that is the grandparent of a pod if exists
// but returns nil is no cronjob is found
func getOwningCronJob(ctx context.Context, pod *v1.Pod) (*batchv1.CronJob, error) {

clientset, err := getClientsetFromContext(ctx)
if err != nil {
return nil, err
}

namespace := pod.Namespace

// first attempt to group events by cronJobs
var owningCronJob *batchv1.CronJob = nil

// check if the pod corresponds to a cronJob
for _, podRef := range pod.ObjectMeta.OwnerReferences {
// check the pod has a job as an owner
if !*podRef.Controller || podRef.Kind != "Job" {
continue
}
// find the owning job
owningJob, err := clientset.BatchV1().Jobs(namespace).Get(context.Background(), podRef.Name, metav1.GetOptions{})
if err != nil {
continue
}
// check if owning job is owned by a cronJob
for _, jobRef := range owningJob.ObjectMeta.OwnerReferences {
if !*jobRef.Controller || jobRef.Kind != "CronJob" {
continue
}
owningCronJob, err = clientset.BatchV1().CronJobs(namespace).Get(context.Background(), jobRef.Name, metav1.GetOptions{})
if err != nil {
continue
}
}
}

return owningCronJob, nil
}
15 changes: 10 additions & 5 deletions enhancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (

"github.com/getsentry/sentry-go"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func runEnhancers(ctx context.Context, objectRef *v1.ObjectReference, cachedObject interface{}, scope *sentry.Scope, sentryEvent *sentry.Event) {
involvedObject := fmt.Sprintf("%s/%s", objectRef.Kind, objectRef.Name)
func runEnhancers(ctx context.Context, kind string, cachedObject metav1.Object, scope *sentry.Scope, sentryEvent *sentry.Event) {
involvedObject := fmt.Sprintf("%s/%s", kind, cachedObject.GetName())
ctx, logger := getLoggerWithTag(ctx, "object", involvedObject)

var err error
Expand All @@ -19,11 +20,15 @@ func runEnhancers(ctx context.Context, objectRef *v1.ObjectReference, cachedObje
runCommonEnhancer(ctx, scope, sentryEvent)

// Then, run kind-specific enhancers
switch objectRef.Kind {
// The Pod enhancer will call corresponding enhancers depending on its root owners
switch kind {
case "Pod":
err = runPodEnhancer(ctx, objectRef, cachedObject, scope, sentryEvent)
podObj, ok := cachedObject.(*v1.Pod)
if !ok {
return
}
err = runPodEnhancer(ctx, podObj, scope, sentryEvent)
}

if err != nil {
logger.Error().Msgf("Error running an enhancer: %v", err)
}
Expand Down
Loading

0 comments on commit c5831d8

Please sign in to comment.