Skip to content

Commit

Permalink
Merge branch 'main' into jz-tests-enhancers
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiahui-Zhang-20 committed Dec 21, 2023
2 parents 16a14ae + 18eff73 commit 0103d6f
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 50 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: golangci-lint
on:
push:
branches:
- master
- main
pull_request:

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read

defaults:
run:
shell: bash

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "1.20"
cache: false
- name: golangci-lint
# https://github.com/golangci/golangci-lint-action/releases/tag/v3.7.0
uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc
with:
version: v1.55
only-new-issues: true
install-mode: "binary"
args: --timeout=10m
32 changes: 32 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
linters:
disable-all: true
enable:
- bodyclose
- deadcode
- dogsled
- dupl
- errcheck
- exportloopref
- gochecknoinits
- goconst
- gocritic
- gocyclo
# - godot
- gofmt
- goimports
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- prealloc
- revive
- staticcheck
- structcheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ test: ## Run tests
.PHONY: test

# Coverage
COVERAGE_MODE = atomic
COVERAGE_PROFILE = coverage.out
COVERAGE_MODE = atomic
COVERAGE_PROFILE = coverage.out
COVERAGE_REPORT_DIR = .coverage
COVERAGE_REPORT_DIR_ABS = $(MKFILE_DIR)/$(COVERAGE_REPORT_DIR)
COVERAGE_REPORT_FILE_ABS = $(COVERAGE_REPORT_DIR_ABS)/$(COVERAGE_PROFILE)
Expand Down Expand Up @@ -55,3 +55,11 @@ fmt: ## Run "go fmt"
go fmt ./...; \
git diff --exit-code;
.PHONY: fmt

lint: ## Lint (using "golangci-lint")
golangci-lint run -v $(ARGS)
.PHONY: lint

lint-fix: ARGS=--fix
lint-fix: lint ### Lint and apply fixes (when applicable)
.PHONY: lint-fix
4 changes: 0 additions & 4 deletions crons.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const (
// by checking the job status to determine if the job just created pod (job starting)
// or if the job exited
func runSentryCronsCheckin(ctx context.Context, job *batchv1.Job, eventHandlerType EventHandlerType) error {

hub := sentry.GetHubFromContext(ctx)
if hub == nil {
return errors.New("cannot get hub from context")
Expand All @@ -45,7 +44,6 @@ func runSentryCronsCheckin(ctx context.Context, job *batchv1.Job, eventHandlerTy
}

hub.WithScope(func(scope *sentry.Scope) {

// If DSN annotation provided, we bind a new client with that DSN
client, ok := dsnClientMapping.GetClientFromObject(ctx, &job.ObjectMeta, hub.Client().Options())
if ok {
Expand All @@ -71,7 +69,6 @@ func runSentryCronsCheckin(ctx context.Context, job *batchv1.Job, eventHandlerTy

// Sends the checkin event to sentry crons for when a job starts
func checkinJobStarting(ctx context.Context, job *batchv1.Job, cronsMonitorData *CronsMonitorData) error {

logger := zerolog.Ctx(ctx)

hub := sentry.GetHubFromContext(ctx)
Expand Down Expand Up @@ -101,7 +98,6 @@ func checkinJobStarting(ctx context.Context, job *batchv1.Job, cronsMonitorData

// Sends the checkin event to sentry crons for when a job ends
func checkinJobEnding(ctx context.Context, job *batchv1.Job, cronsMonitorData *CronsMonitorData) error {

hub := sentry.GetHubFromContext(ctx)
if hub == nil {
return errors.New("cannot get hub from context")
Expand Down
1 change: 0 additions & 1 deletion crons_monitor_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type CronsMonitorData struct {

// Constructor for cronsMonitorData
func NewCronsMonitorData(monitorSlug string, schedule string, completions *int32) *CronsMonitorData {

// Get required number of pods to complete
var requiredCompletions int32
if completions == nil {
Expand Down
10 changes: 0 additions & 10 deletions crons_monitor_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func TestNewCronsJobData(t *testing.T) {

fakeId := "080181f33ca343f89b0bf55d50abfeee"

cronsJobData := NewCronsJobData(sentry.EventID(fakeId))
Expand All @@ -23,7 +22,6 @@ func TestNewCronsJobData(t *testing.T) {
}

func TestGetCheckinId(t *testing.T) {

fakeId := "080181f33ca343f89b0bf55d50abfeee"

cronsJobData := NewCronsJobData(sentry.EventID(fakeId))
Expand All @@ -37,7 +35,6 @@ func TestGetCheckinId(t *testing.T) {
}

func TestNewCronsMonitorData(t *testing.T) {

fakeMonitorSlug := "cronjob-slug"
fakeSchedule := "* * * * *"
var fakeCompletions int32 = 3
Expand All @@ -59,7 +56,6 @@ func TestNewCronsMonitorData(t *testing.T) {
}

func TestAddJob(t *testing.T) {

fakeId := "080181f33ca343f89b0bf55d50abfeee"
fakeMonitorSlug := "cronjob-slug"
fakeSchedule := "* * * * *"
Expand All @@ -84,15 +80,13 @@ func TestAddJob(t *testing.T) {
}

func TestNewCronsMetaData(t *testing.T) {

cronsMetaData := NewCronsMetaData()
if cronsMetaData.cronsMonitorDataMap == nil {
t.Errorf("Failed to create cronsMonitorDataMap")
}
}

func TestAddCronsMonitorData(t *testing.T) {

cronsMetaData := NewCronsMetaData()
if cronsMetaData.cronsMonitorDataMap == nil {
t.Errorf("Failed to create cronsMonitorDataMap")
Expand All @@ -115,7 +109,6 @@ func TestAddCronsMonitorData(t *testing.T) {
}

func TestDeleteCronsMonitorData(t *testing.T) {

cronsMetaData := NewCronsMetaData()
if cronsMetaData.cronsMonitorDataMap == nil {
t.Errorf("Failed to create cronsMonitorDataMap")
Expand All @@ -135,11 +128,9 @@ func TestDeleteCronsMonitorData(t *testing.T) {
if ok {
t.Errorf("Failed to delete cronsMonitorData from map")
}

}

func TestGetCronsMonitorData(t *testing.T) {

cronsMetaData := NewCronsMetaData()
if cronsMetaData.cronsMonitorDataMap == nil {
t.Errorf("Failed to create cronsMonitorDataMap")
Expand All @@ -159,5 +150,4 @@ func TestGetCronsMonitorData(t *testing.T) {
if retCronsMonitorData != cronsMonitorData {
t.Errorf("Failed to get correct cronsMonitorData to map")
}

}
8 changes: 0 additions & 8 deletions enhancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
const breadcrumbLimit = 20

func runEnhancers(ctx context.Context, eventObject *v1.Event, kind string, object metav1.Object, scope *sentry.Scope, sentryEvent *sentry.Event) error {

logger := zerolog.Ctx(ctx)
logger.Debug().Msgf("Running the enhancer")

Expand Down Expand Up @@ -88,7 +87,6 @@ func eventEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Objec
}

func objectEnhancer(ctx context.Context, scope *sentry.Scope, kindObjectPair *KindObjectPair, sentryEvent *sentry.Event) error {

objectTag := fmt.Sprintf("%s/%s", kindObjectPair.kind, kindObjectPair.object.GetName())
ctx, logger := getLoggerWithTag(ctx, "object", objectTag)

Expand Down Expand Up @@ -196,7 +194,6 @@ func podEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object,
}

func jobEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object, sentryEvent *sentry.Event) error {

jobObj, ok := object.(*batchv1.Job)
if !ok {
return errors.New("failed to cast object to Job object")
Expand Down Expand Up @@ -226,7 +223,6 @@ func jobEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object,
}

func cronjobEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object, sentryEvent *sentry.Event) error {

cronjobObj, ok := object.(*batchv1.CronJob)
if !ok {
return errors.New("failed to cast object to CronJob object")
Expand Down Expand Up @@ -261,7 +257,6 @@ func cronjobEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Obj
}

func replicaSetEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object, sentryEvent *sentry.Event) error {

replicasetObj, ok := object.(*appsv1.ReplicaSet)
if !ok {
return errors.New("failed to cast object to ReplicaSet object")
Expand Down Expand Up @@ -291,7 +286,6 @@ func replicaSetEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.
}

func deploymentEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object, sentryEvent *sentry.Event) error {

deploymentObj, ok := object.(*appsv1.Deployment)
if !ok {
return errors.New("failed to cast object to Deployment object")
Expand Down Expand Up @@ -323,7 +317,6 @@ func deploymentEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.
// 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
rootOwners, err := ownerRefDFS(ctx, kindObjPair)
if err != nil {
Expand All @@ -340,7 +333,6 @@ func findRootOwners(ctx context.Context, kindObjPair *KindObjectPair) ([]KindObj

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

parents := kindObjPair.object.GetOwnerReferences()
// the owners slice to be returned
rootOwners := []KindObjectPair{}
Expand Down
1 change: 0 additions & 1 deletion enhancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

func TestRunEnhancers(t *testing.T) {

// Create empty context
ctx := context.Background()
// Create simple fake client
Expand Down
1 change: 0 additions & 1 deletion informer_cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

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

logger := zerolog.Ctx(ctx)

logger.Debug().Msgf("Starting cronjob informer\n")
Expand Down
1 change: 0 additions & 1 deletion informer_deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

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

logger := zerolog.Ctx(ctx)

logger.Debug().Msgf("starting deployment informer\n")
Expand Down
2 changes: 0 additions & 2 deletions informer_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

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

logger := zerolog.Ctx(ctx)

logger.Debug().Msgf("starting job informer\n")
Expand All @@ -31,7 +30,6 @@ func createJobInformer(ctx context.Context, factory informers.SharedInformerFact
}

handler.UpdateFunc = func(oldObj, newObj interface{}) {

oldJob := oldObj.(*batchv1.Job)
newJob := newObj.(*batchv1.Job)

Expand Down
1 change: 0 additions & 1 deletion informer_replicasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

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

logger := zerolog.Ctx(ctx)

logger.Debug().Msgf("starting replicaset informer\n")
Expand Down
1 change: 0 additions & 1 deletion informers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var deploymentInformer cache.SharedIndexInformer
// if we opt into cronjob, attach the job/cronjob event handlers
// and add to the crons monitor data struct for Sentry Crons
func startInformers(ctx context.Context, namespace string) error {

clientset, err := getClientsetFromContext(ctx)
if err != nil {
return errors.New("failed to get clientset")
Expand Down
4 changes: 0 additions & 4 deletions sentry_dsn_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func (d *DsnClientMapping) GetClientFromMap(dsn string) (*sentry.Client, bool) {
}

func (d *DsnClientMapping) GetClientFromObject(ctx context.Context, object metav1.Object, clientOptions sentry.ClientOptions) (*sentry.Client, bool) {

// If the custom DSN flag is set to false
// then avoid searching for the custom DSN
// or adding an alternative client and instead
Expand Down Expand Up @@ -99,7 +98,6 @@ func (d *DsnClientMapping) GetClientFromObject(ctx context.Context, object metav

// Recursive function to find if there is a DSN annotation
func searchDsn(ctx context.Context, obj metav1.Object) (string, error) {

dsn, ok := obj.GetAnnotations()[DSNAnnotation]
if ok {
return dsn, nil
Expand All @@ -120,7 +118,6 @@ func searchDsn(ctx context.Context, obj metav1.Object) (string, error) {
}

func findObject(ctx context.Context, kind string, namespace string, name string) (metav1.Object, bool) {

clientset, err := getClientsetFromContext(ctx)
if err != nil {
return nil, false
Expand Down Expand Up @@ -157,7 +154,6 @@ func findObject(ctx context.Context, kind string, namespace string, name string)
obj, ok, err := deploymentInformer.GetIndexer().GetByKey(namespace + "/" + name)
if ok && err == nil {
deployment = obj.(*v1.Deployment)

}
}
if deployment == nil {
Expand Down
Loading

0 comments on commit 0103d6f

Please sign in to comment.