Skip to content

Commit

Permalink
fix some linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiahui-Zhang-20 committed Dec 22, 2023
1 parent 8f9b884 commit 6a966d8
Show file tree
Hide file tree
Showing 21 changed files with 138 additions and 107 deletions.
14 changes: 6 additions & 8 deletions cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func getClusterConfig() (*rest.Config, error) {
if err == nil {
log.Info().Msg("Detected in-cluster configuration")
return config, nil
}

Check warning on line 53 in cluster_config.go

View check run for this annotation

Codecov / codecov/patch

cluster_config.go#L53

Added line #L53 was not covered by tests

if autoConfig {
log.Warn().Msgf("Could not initialize in-cluster config")

Check warning on line 56 in cluster_config.go

View check run for this annotation

Codecov / codecov/patch

cluster_config.go#L55-L56

Added lines #L55 - L56 were not covered by tests
} else {
if autoConfig {
log.Warn().Msgf("Could not initialize in-cluster config")
} else {
return nil, err
}
return nil, err

Check warning on line 58 in cluster_config.go

View check run for this annotation

Codecov / codecov/patch

cluster_config.go#L58

Added line #L58 was not covered by tests
}
}

Expand All @@ -76,13 +76,11 @@ func getClusterConfig() (*rest.Config, error) {

log.Debug().Msgf("Kubeconfig path: %s", kubeconfig)
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)

if err == nil {
log.Info().Msg("Detected out-of-cluster configuration")
return config, nil
} else {
return nil, err
}
return nil, err

Check warning on line 83 in cluster_config.go

View check run for this annotation

Codecov / codecov/patch

cluster_config.go#L83

Added line #L83 was not covered by tests
}

if config == nil {
Expand Down
19 changes: 14 additions & 5 deletions crons.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,17 @@ func runSentryCronsCheckin(ctx context.Context, job *batchv1.Job, eventHandlerTy
// The job just begun so check in to start
if job.Status.Active == 0 && job.Status.Succeeded == 0 && job.Status.Failed == 0 {
// Add the job to the cronJob informer data
checkinJobStarting(ctx, job, cronsMonitorData)
err := checkinJobStarting(ctx, job, cronsMonitorData)
if err != nil {
return
}

Check warning on line 61 in crons.go

View check run for this annotation

Codecov / codecov/patch

crons.go#L58-L61

Added lines #L58 - L61 were not covered by tests
} else if job.Status.Active > 0 {
return
} else if job.Status.Failed > 0 || job.Status.Succeeded > 0 {
checkinJobEnding(ctx, job, cronsMonitorData)
err := checkinJobEnding(ctx, job, cronsMonitorData)
if err != nil {
return
}

Check warning on line 68 in crons.go

View check run for this annotation

Codecov / codecov/patch

crons.go#L65-L68

Added lines #L65 - L68 were not covered by tests
return // Finished
}
})
Expand All @@ -84,14 +90,17 @@ func checkinJobStarting(ctx context.Context, job *batchv1.Job, cronsMonitorData
logger.Debug().Msgf("Checking in at start of job: %s\n", job.Name)

// All containers running in the pod
checkinId := hub.CaptureCheckIn(
checkinID := hub.CaptureCheckIn(

Check warning on line 93 in crons.go

View check run for this annotation

Codecov / codecov/patch

crons.go#L93

Added line #L93 was not covered by tests
&sentry.CheckIn{
MonitorSlug: cronsMonitorData.MonitorSlug,
Status: sentry.CheckInStatusInProgress,
},
cronsMonitorData.monitorConfig,
)
cronsMonitorData.addJob(job, *checkinId)
err := cronsMonitorData.addJob(job, *checkinID)
if err != nil {
return err
}

Check warning on line 103 in crons.go

View check run for this annotation

Codecov / codecov/patch

crons.go#L100-L103

Added lines #L100 - L103 were not covered by tests

return nil
}
Expand Down Expand Up @@ -129,7 +138,7 @@ func checkinJobEnding(ctx context.Context, job *batchv1.Job, cronsMonitorData *C
logger.Trace().Msgf("checking in at end of job: %s\n", job.Name)
hub.CaptureCheckIn(
&sentry.CheckIn{
ID: jobData.getCheckinId(),
ID: jobData.getCheckinID(),

Check warning on line 141 in crons.go

View check run for this annotation

Codecov / codecov/patch

crons.go#L141

Added line #L141 was not covered by tests
MonitorSlug: cronsMonitorData.MonitorSlug,
Status: jobStatus,
},
Expand Down
12 changes: 6 additions & 6 deletions crons_monitor_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import (

// Struct associated with a job
type CronsJobData struct {
CheckinId sentry.EventID
CheckinID sentry.EventID
}

// Constructor for cronsMonitorData
func NewCronsJobData(checkinId sentry.EventID) *CronsJobData {
return &CronsJobData{
CheckinId: checkinId,
CheckinID: checkinId,
}
}

func (j *CronsJobData) getCheckinId() sentry.EventID {
return j.CheckinId
func (j *CronsJobData) getCheckinID() sentry.EventID {
return j.CheckinID
}

// Struct associated with a cronJob
Expand Down Expand Up @@ -55,10 +55,10 @@ func NewCronsMonitorData(monitorSlug string, schedule string, completions *int32
}

// Add a job to the crons monitor
func (c *CronsMonitorData) addJob(job *batchv1.Job, checkinId sentry.EventID) error {
func (c *CronsMonitorData) addJob(job *batchv1.Job, checkinID sentry.EventID) error {
c.mutex.Lock()
defer c.mutex.Unlock()
c.JobDatas[job.Name] = NewCronsJobData(checkinId)
c.JobDatas[job.Name] = NewCronsJobData(checkinID)
return nil
}

Expand Down
29 changes: 16 additions & 13 deletions crons_monitor_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ import (
)

func TestNewCronsJobData(t *testing.T) {
fakeId := "080181f33ca343f89b0bf55d50abfeee"
fakeID := "080181f33ca343f89b0bf55d50abfeee"

Check failure on line 12 in crons_monitor_data_test.go

View workflow job for this annotation

GitHub Actions / lint

string `080181f33ca343f89b0bf55d50abfeee` has 3 occurrences, make it a constant (goconst)

cronsJobData := NewCronsJobData(sentry.EventID(fakeId))
cronsJobData := NewCronsJobData(sentry.EventID(fakeID))
if cronsJobData == nil {
t.Errorf("Failed to create cronsJobData")
return
}
if cronsJobData.CheckinId != sentry.EventID(fakeId) {
if cronsJobData.CheckinID != sentry.EventID(fakeID) {
t.Errorf("The cronsJobData set to incorrect ID")
}
}

func TestGetCheckinId(t *testing.T) {
fakeId := "080181f33ca343f89b0bf55d50abfeee"
fakeID := "080181f33ca343f89b0bf55d50abfeee"

cronsJobData := NewCronsJobData(sentry.EventID(fakeId))
cronsJobData := NewCronsJobData(sentry.EventID(fakeID))
if cronsJobData == nil {
t.Errorf("Failed to create cronsJobData")
return
}
if cronsJobData.getCheckinId() != sentry.EventID(fakeId) {
if cronsJobData.getCheckinID() != sentry.EventID(fakeID) {
t.Errorf("Retrieved incorrect checkin ID")
}
}
Expand All @@ -56,7 +56,7 @@ func TestNewCronsMonitorData(t *testing.T) {
}

func TestAddJob(t *testing.T) {
fakeId := "080181f33ca343f89b0bf55d50abfeee"
fakeID := "080181f33ca343f89b0bf55d50abfeee"
fakeMonitorSlug := "cronjob-slug"
fakeSchedule := "* * * * *"
var fakeCompletions int32 = 3
Expand All @@ -68,13 +68,16 @@ func TestAddJob(t *testing.T) {
Name: "TestAddJobJob",
},
}
cronsMonitorData.addJob(jobObj, sentry.EventID(fakeId))
err := cronsMonitorData.addJob(jobObj, sentry.EventID(fakeID))
if err != nil {
t.Errorf("Failed to add job")
}

jobData, ok := cronsMonitorData.JobDatas["TestAddJobJob"]
if !ok {
t.Errorf("Failed to add job data")
}
if jobData.CheckinId != sentry.EventID(fakeId) {
if jobData.CheckinID != sentry.EventID(fakeID) {
t.Errorf("Incorrect checkin ID")
}
}
Expand All @@ -92,14 +95,14 @@ func TestAddCronsMonitorData(t *testing.T) {
t.Errorf("Failed to create cronsMonitorDataMap")
}

fakeMonitorSlug := "TestAddCronsMonitorDataCronJob"
cronjobName := "TestAddCronsMonitorDataCronJob"

Check failure on line 98 in crons_monitor_data_test.go

View workflow job for this annotation

GitHub Actions / lint

string `TestAddCronsMonitorDataCronJob` has 3 occurrences, make it a constant (goconst)
fakeSchedule := "* * * * *"
var fakeCompletions int32 = 3
cronsMonitorData := NewCronsMonitorData(fakeMonitorSlug, fakeSchedule, &fakeCompletions)
cronsMonitorData := NewCronsMonitorData(cronjobName, fakeSchedule, &fakeCompletions)

cronsMetaData.addCronsMonitorData("TestAddCronsMonitorDataCronJob", cronsMonitorData)
cronsMetaData.addCronsMonitorData(cronjobName, cronsMonitorData)

retCronsMonitorData, ok := cronsMetaData.cronsMonitorDataMap["TestAddCronsMonitorDataCronJob"]
retCronsMonitorData, ok := cronsMetaData.cronsMonitorDataMap[cronjobName]
if !ok {
t.Errorf("Failed to add cronsMonitorData to map")
}
Expand Down
26 changes: 13 additions & 13 deletions enhancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func runEnhancers(ctx context.Context, eventObject *v1.Event, kind string, objec

// If an event object is provided, we call the event enhancer
if eventObject != nil {
err = eventEnhancer(ctx, scope, eventObject, sentryEvent)
err = eventEnhancer(scope, eventObject)
if err != nil {
return err
}
Expand All @@ -56,7 +56,7 @@ type KindObjectPair struct {
object metav1.Object
}

func eventEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object, sentryEvent *sentry.Event) error {
func eventEnhancer(scope *sentry.Scope, object metav1.Object) error {
eventObj, ok := object.(*v1.Event)
if !ok {
return errors.New("failed to cast object to event object")
Expand Down Expand Up @@ -90,7 +90,7 @@ func objectEnhancer(ctx context.Context, scope *sentry.Scope, kindObjectPair *Ki
objectTag := fmt.Sprintf("%s/%s", kindObjectPair.kind, kindObjectPair.object.GetName())
ctx, logger := getLoggerWithTag(ctx, "object", objectTag)

var err error = nil
var err error

logger.Trace().Msgf("Current fingerprint: %v", sentryEvent.Fingerprint)

Expand Down Expand Up @@ -174,10 +174,10 @@ func podEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object,
// Add the pod to the tag
setTagIfNotEmpty(scope, "pod_name", object.GetName())
podObj.ManagedFields = []metav1.ManagedFieldsEntry{}
metadataJson, err := prettyJson(podObj.ObjectMeta)
metadataJSON, err := prettyJSON(podObj.ObjectMeta)
if err == nil {
scope.SetContext(KindPod, sentry.Context{
"Metadata": metadataJson,
"Metadata": metadataJSON,
})
}

Expand Down Expand Up @@ -205,10 +205,10 @@ func jobEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object,
// Add the job to the tag
setTagIfNotEmpty(scope, "job_name", object.GetName())
jobObj.ManagedFields = []metav1.ManagedFieldsEntry{}
metadataJson, err := prettyJson(jobObj.ObjectMeta)
metadataJSON, err := prettyJSON(jobObj.ObjectMeta)

Check warning on line 208 in enhancers.go

View check run for this annotation

Codecov / codecov/patch

enhancers.go#L208

Added line #L208 was not covered by tests
if err == nil {
scope.SetContext(KindJob, sentry.Context{
"Metadata": metadataJson,
"Metadata": metadataJSON,

Check warning on line 211 in enhancers.go

View check run for this annotation

Codecov / codecov/patch

enhancers.go#L211

Added line #L211 was not covered by tests
})
}

Expand Down Expand Up @@ -239,10 +239,10 @@ func cronjobEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Obj
// Add the cronjob to the tag
setTagIfNotEmpty(scope, "cronjob_name", object.GetName())
cronjobObj.ManagedFields = []metav1.ManagedFieldsEntry{}
metadataJson, err := prettyJson(cronjobObj.ObjectMeta)
metadataJSON, err := prettyJSON(cronjobObj.ObjectMeta)

Check warning on line 242 in enhancers.go

View check run for this annotation

Codecov / codecov/patch

enhancers.go#L242

Added line #L242 was not covered by tests
if err == nil {
scope.SetContext(KindCronjob, sentry.Context{
"Metadata": metadataJson,
"Metadata": metadataJSON,

Check warning on line 245 in enhancers.go

View check run for this annotation

Codecov / codecov/patch

enhancers.go#L245

Added line #L245 was not covered by tests
})
}

Expand All @@ -268,10 +268,10 @@ func replicaSetEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.
// Add the replicaset to the tag
setTagIfNotEmpty(scope, "replicaset_name", object.GetName())
replicasetObj.ManagedFields = []metav1.ManagedFieldsEntry{}
metadataJson, err := prettyJson(replicasetObj.ObjectMeta)
metadataJSON, err := prettyJSON(replicasetObj.ObjectMeta)

Check warning on line 271 in enhancers.go

View check run for this annotation

Codecov / codecov/patch

enhancers.go#L271

Added line #L271 was not covered by tests
if err == nil {
scope.SetContext(KindReplicaset, sentry.Context{
"Metadata": metadataJson,
"Metadata": metadataJSON,

Check warning on line 274 in enhancers.go

View check run for this annotation

Codecov / codecov/patch

enhancers.go#L274

Added line #L274 was not covered by tests
})
}

Expand All @@ -296,10 +296,10 @@ func deploymentEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.
// Add the deployment to the tag
setTagIfNotEmpty(scope, "deployment_name", object.GetName())
deploymentObj.ManagedFields = []metav1.ManagedFieldsEntry{}
metadataJson, err := prettyJson(deploymentObj.ObjectMeta)
metadataJSON, err := prettyJSON(deploymentObj.ObjectMeta)

Check warning on line 299 in enhancers.go

View check run for this annotation

Codecov / codecov/patch

enhancers.go#L299

Added line #L299 was not covered by tests
if err == nil {
scope.SetContext(KindDeployment, sentry.Context{
"Metadata": metadataJson,
"Metadata": metadataJSON,

Check warning on line 302 in enhancers.go

View check run for this annotation

Codecov / codecov/patch

enhancers.go#L302

Added line #L302 was not covered by tests
})
}

Expand Down
11 changes: 7 additions & 4 deletions enhancers_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func checkCommonEnhancerPatterns() {
}
}

func matchSinglePattern(ctx context.Context, message string, pattern *commonMsgPattern) (fingerprint []string, matched bool) {
func matchSinglePattern(message string, pattern *commonMsgPattern) (fingerprint []string, matched bool) {
pat := pattern.regex

match := pat.FindStringSubmatch(message)
Expand All @@ -89,14 +89,14 @@ func matchSinglePattern(ctx context.Context, message string, pattern *commonMsgP
return fingerprint, true
}

func matchCommonPatterns(ctx context.Context, scope *sentry.Scope, sentryEvent *sentry.Event) error {
func matchCommonPatterns(ctx context.Context, sentryEvent *sentry.Event) error {
logger := zerolog.Ctx(ctx)
message := sentryEvent.Message

logger.Trace().Msgf("Matching against message: %q", message)

for _, pattern := range patternsAll {
fingerprint, matched := matchSinglePattern(ctx, message, pattern)
fingerprint, matched := matchSinglePattern(message, pattern)
if matched {
logger.Trace().Msgf("Pattern match: %v, fingerprint: %v", pattern, fingerprint)
// Ideally we should set the fingerprint on Scope, but there's no easy way right now to get
Expand All @@ -122,6 +122,9 @@ func runCommonEnhancer(ctx context.Context, scope *sentry.Scope, sentryEvent *se
}

// Match common message patterns
matchCommonPatterns(ctx, scope, sentryEvent)
err := matchCommonPatterns(ctx, sentryEvent)
if err != nil {
return err
}

Check warning on line 128 in enhancers_common.go

View check run for this annotation

Codecov / codecov/patch

enhancers_common.go#L127-L128

Added lines #L127 - L128 were not covered by tests
return nil
}
2 changes: 1 addition & 1 deletion event_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const bufferSize = 1000
var mu sync.RWMutex

// TODO: we could have one buffer per namespace
var eventBuffer *ring.Ring = ring.New(bufferSize)
var eventBuffer = ring.New(bufferSize)

func addEventToBuffer(event *v1.Event) {
mu.Lock()
Expand Down
2 changes: 1 addition & 1 deletion informer_cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"k8s.io/client-go/tools/cache"
)

func createCronjobInformer(ctx context.Context, factory informers.SharedInformerFactory, namespace string) (cache.SharedIndexInformer, error) {
func createCronjobInformer(ctx context.Context, factory informers.SharedInformerFactory) (cache.SharedIndexInformer, error) {

Check failure on line 14 in informer_cronjobs.go

View workflow job for this annotation

GitHub Actions / lint

createCronjobInformer - result 1 (error) is always nil (unparam)

Check warning on line 14 in informer_cronjobs.go

View check run for this annotation

Codecov / codecov/patch

informer_cronjobs.go#L14

Added line #L14 was not covered by tests
logger := zerolog.Ctx(ctx)

logger.Debug().Msgf("Starting cronjob informer\n")
Expand Down
2 changes: 1 addition & 1 deletion informer_deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"k8s.io/client-go/tools/cache"
)

func createDeploymentInformer(ctx context.Context, factory informers.SharedInformerFactory, namespace string) (cache.SharedIndexInformer, error) {
func createDeploymentInformer(ctx context.Context, factory informers.SharedInformerFactory) (cache.SharedIndexInformer, error) {

Check failure on line 11 in informer_deployments.go

View workflow job for this annotation

GitHub Actions / lint

createDeploymentInformer - result 1 (error) is always nil (unparam)

Check warning on line 11 in informer_deployments.go

View check run for this annotation

Codecov / codecov/patch

informer_deployments.go#L11

Added line #L11 was not covered by tests
logger := zerolog.Ctx(ctx)

logger.Debug().Msgf("starting deployment informer\n")
Expand Down
2 changes: 1 addition & 1 deletion informer_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"k8s.io/client-go/tools/cache"
)

func createJobInformer(ctx context.Context, factory informers.SharedInformerFactory, namespace string) (cache.SharedIndexInformer, error) {
func createJobInformer(ctx context.Context, factory informers.SharedInformerFactory) (cache.SharedIndexInformer, error) {

Check failure on line 14 in informer_jobs.go

View workflow job for this annotation

GitHub Actions / lint

createJobInformer - result 1 (error) is always nil (unparam)

Check warning on line 14 in informer_jobs.go

View check run for this annotation

Codecov / codecov/patch

informer_jobs.go#L14

Added line #L14 was not covered by tests
logger := zerolog.Ctx(ctx)

logger.Debug().Msgf("starting job informer\n")
Expand Down
2 changes: 1 addition & 1 deletion informer_replicasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"k8s.io/client-go/tools/cache"
)

func createReplicasetInformer(ctx context.Context, factory informers.SharedInformerFactory, namespace string) (cache.SharedIndexInformer, error) {
func createReplicasetInformer(ctx context.Context, factory informers.SharedInformerFactory) (cache.SharedIndexInformer, error) {

Check failure on line 12 in informer_replicasets.go

View workflow job for this annotation

GitHub Actions / lint

createReplicasetInformer - result 1 (error) is always nil (unparam)

Check warning on line 12 in informer_replicasets.go

View check run for this annotation

Codecov / codecov/patch

informer_replicasets.go#L12

Added line #L12 was not covered by tests
logger := zerolog.Ctx(ctx)

logger.Debug().Msgf("starting replicaset informer\n")
Expand Down
8 changes: 4 additions & 4 deletions informers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ func startInformers(ctx context.Context, namespace string) error {
)

// Create the job informer
jobInformer, err = createJobInformer(ctx, factory, namespace)
jobInformer, err = createJobInformer(ctx, factory)

Check warning on line 34 in informers.go

View check run for this annotation

Codecov / codecov/patch

informers.go#L34

Added line #L34 was not covered by tests
if err != nil {
return err
}
// Create the cronjob informer
cronjobInformer, err = createCronjobInformer(ctx, factory, namespace)
cronjobInformer, err = createCronjobInformer(ctx, factory)

Check warning on line 39 in informers.go

View check run for this annotation

Codecov / codecov/patch

informers.go#L39

Added line #L39 was not covered by tests
if err != nil {
return err
}
// Create the replicaset informer
replicasetInformer, err = createReplicasetInformer(ctx, factory, namespace)
replicasetInformer, err = createReplicasetInformer(ctx, factory)

Check warning on line 44 in informers.go

View check run for this annotation

Codecov / codecov/patch

informers.go#L44

Added line #L44 was not covered by tests
if err != nil {
return err
}
// Create the deployment informer
deploymentInformer, err = createDeploymentInformer(ctx, factory, namespace)
deploymentInformer, err = createDeploymentInformer(ctx, factory)

Check warning on line 49 in informers.go

View check run for this annotation

Codecov / codecov/patch

informers.go#L49

Added line #L49 was not covered by tests
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 6a966d8

Please sign in to comment.