Skip to content

Commit 18eff73

Browse files
authored
ci: Add golangi-lint configuration (#77)
1 parent 0b58f2c commit 18eff73

19 files changed

+78
-50
lines changed

.github/workflows/golangci-lint.yaml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: golangci-lint
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- main
7+
pull_request:
8+
9+
permissions:
10+
contents: read
11+
# Optional: allow read access to pull request. Use with `only-new-issues` option.
12+
# pull-requests: read
13+
14+
defaults:
15+
run:
16+
shell: bash
17+
18+
jobs:
19+
golangci:
20+
name: lint
21+
runs-on: ubuntu-latest
22+
timeout-minutes: 10
23+
steps:
24+
- uses: actions/checkout@v3
25+
- uses: actions/setup-go@v4
26+
with:
27+
go-version: "1.20"
28+
cache: false
29+
- name: golangci-lint
30+
# https://github.com/golangci/golangci-lint-action/releases/tag/v3.7.0
31+
uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc
32+
with:
33+
version: v1.55
34+
only-new-issues: true
35+
install-mode: "binary"
36+
args: --timeout=10m

.golangci.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
linters:
2+
disable-all: true
3+
enable:
4+
- bodyclose
5+
- deadcode
6+
- dogsled
7+
- dupl
8+
- errcheck
9+
- exportloopref
10+
- gochecknoinits
11+
- goconst
12+
- gocritic
13+
- gocyclo
14+
# - godot
15+
- gofmt
16+
- goimports
17+
- gosec
18+
- gosimple
19+
- govet
20+
- ineffassign
21+
- misspell
22+
- nakedret
23+
- prealloc
24+
- revive
25+
- staticcheck
26+
- structcheck
27+
- typecheck
28+
- unconvert
29+
- unparam
30+
- unused
31+
- varcheck
32+
- whitespace

Makefile

+10-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ test: ## Run tests
2323
.PHONY: test
2424

2525
# Coverage
26-
COVERAGE_MODE = atomic
27-
COVERAGE_PROFILE = coverage.out
26+
COVERAGE_MODE = atomic
27+
COVERAGE_PROFILE = coverage.out
2828
COVERAGE_REPORT_DIR = .coverage
2929
COVERAGE_REPORT_DIR_ABS = $(MKFILE_DIR)/$(COVERAGE_REPORT_DIR)
3030
COVERAGE_REPORT_FILE_ABS = $(COVERAGE_REPORT_DIR_ABS)/$(COVERAGE_PROFILE)
@@ -55,3 +55,11 @@ fmt: ## Run "go fmt"
5555
go fmt ./...; \
5656
git diff --exit-code;
5757
.PHONY: fmt
58+
59+
lint: ## Lint (using "golangci-lint")
60+
golangci-lint run -v $(ARGS)
61+
.PHONY: lint
62+
63+
lint-fix: ARGS=--fix
64+
lint-fix: lint ### Lint and apply fixes (when applicable)
65+
.PHONY: lint-fix

crons.go

-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const (
2121
// by checking the job status to determine if the job just created pod (job starting)
2222
// or if the job exited
2323
func runSentryCronsCheckin(ctx context.Context, job *batchv1.Job, eventHandlerType EventHandlerType) error {
24-
2524
hub := sentry.GetHubFromContext(ctx)
2625
if hub == nil {
2726
return errors.New("cannot get hub from context")
@@ -45,7 +44,6 @@ func runSentryCronsCheckin(ctx context.Context, job *batchv1.Job, eventHandlerTy
4544
}
4645

4746
hub.WithScope(func(scope *sentry.Scope) {
48-
4947
// If DSN annotation provided, we bind a new client with that DSN
5048
client, ok := dsnClientMapping.GetClientFromObject(ctx, &job.ObjectMeta, hub.Client().Options())
5149
if ok {
@@ -71,7 +69,6 @@ func runSentryCronsCheckin(ctx context.Context, job *batchv1.Job, eventHandlerTy
7169

7270
// Sends the checkin event to sentry crons for when a job starts
7371
func checkinJobStarting(ctx context.Context, job *batchv1.Job, cronsMonitorData *CronsMonitorData) error {
74-
7572
logger := zerolog.Ctx(ctx)
7673

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

10299
// Sends the checkin event to sentry crons for when a job ends
103100
func checkinJobEnding(ctx context.Context, job *batchv1.Job, cronsMonitorData *CronsMonitorData) error {
104-
105101
hub := sentry.GetHubFromContext(ctx)
106102
if hub == nil {
107103
return errors.New("cannot get hub from context")

crons_monitor_data.go

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ type CronsMonitorData struct {
3434

3535
// Constructor for cronsMonitorData
3636
func NewCronsMonitorData(monitorSlug string, schedule string, completions *int32) *CronsMonitorData {
37-
3837
// Get required number of pods to complete
3938
var requiredCompletions int32
4039
if completions == nil {

crons_monitor_data_test.go

-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
)
1010

1111
func TestNewCronsJobData(t *testing.T) {
12-
1312
fakeId := "080181f33ca343f89b0bf55d50abfeee"
1413

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

2524
func TestGetCheckinId(t *testing.T) {
26-
2725
fakeId := "080181f33ca343f89b0bf55d50abfeee"
2826

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

3937
func TestNewCronsMonitorData(t *testing.T) {
40-
4138
fakeMonitorSlug := "cronjob-slug"
4239
fakeSchedule := "* * * * *"
4340
var fakeCompletions int32 = 3
@@ -59,7 +56,6 @@ func TestNewCronsMonitorData(t *testing.T) {
5956
}
6057

6158
func TestAddJob(t *testing.T) {
62-
6359
fakeId := "080181f33ca343f89b0bf55d50abfeee"
6460
fakeMonitorSlug := "cronjob-slug"
6561
fakeSchedule := "* * * * *"
@@ -84,15 +80,13 @@ func TestAddJob(t *testing.T) {
8480
}
8581

8682
func TestNewCronsMetaData(t *testing.T) {
87-
8883
cronsMetaData := NewCronsMetaData()
8984
if cronsMetaData.cronsMonitorDataMap == nil {
9085
t.Errorf("Failed to create cronsMonitorDataMap")
9186
}
9287
}
9388

9489
func TestAddCronsMonitorData(t *testing.T) {
95-
9690
cronsMetaData := NewCronsMetaData()
9791
if cronsMetaData.cronsMonitorDataMap == nil {
9892
t.Errorf("Failed to create cronsMonitorDataMap")
@@ -115,7 +109,6 @@ func TestAddCronsMonitorData(t *testing.T) {
115109
}
116110

117111
func TestDeleteCronsMonitorData(t *testing.T) {
118-
119112
cronsMetaData := NewCronsMetaData()
120113
if cronsMetaData.cronsMonitorDataMap == nil {
121114
t.Errorf("Failed to create cronsMonitorDataMap")
@@ -135,11 +128,9 @@ func TestDeleteCronsMonitorData(t *testing.T) {
135128
if ok {
136129
t.Errorf("Failed to delete cronsMonitorData from map")
137130
}
138-
139131
}
140132

141133
func TestGetCronsMonitorData(t *testing.T) {
142-
143134
cronsMetaData := NewCronsMetaData()
144135
if cronsMetaData.cronsMonitorDataMap == nil {
145136
t.Errorf("Failed to create cronsMonitorDataMap")
@@ -159,5 +150,4 @@ func TestGetCronsMonitorData(t *testing.T) {
159150
if retCronsMonitorData != cronsMonitorData {
160151
t.Errorf("Failed to get correct cronsMonitorData to map")
161152
}
162-
163153
}

enhancers.go

-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
const breadcrumbLimit = 20
1717

1818
func runEnhancers(ctx context.Context, eventObject *v1.Event, kind string, object metav1.Object, scope *sentry.Scope, sentryEvent *sentry.Event) error {
19-
2019
logger := zerolog.Ctx(ctx)
2120
logger.Debug().Msgf("Running the enhancer")
2221

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

9089
func objectEnhancer(ctx context.Context, scope *sentry.Scope, kindObjectPair *KindObjectPair, sentryEvent *sentry.Event) error {
91-
9290
objectTag := fmt.Sprintf("%s/%s", kindObjectPair.kind, kindObjectPair.object.GetName())
9391
ctx, logger := getLoggerWithTag(ctx, "object", objectTag)
9492

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

198196
func jobEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object, sentryEvent *sentry.Event) error {
199-
200197
jobObj, ok := object.(*batchv1.Job)
201198
if !ok {
202199
return errors.New("failed to cast object to Job object")
@@ -226,7 +223,6 @@ func jobEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object,
226223
}
227224

228225
func cronjobEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object, sentryEvent *sentry.Event) error {
229-
230226
cronjobObj, ok := object.(*batchv1.CronJob)
231227
if !ok {
232228
return errors.New("failed to cast object to CronJob object")
@@ -261,7 +257,6 @@ func cronjobEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Obj
261257
}
262258

263259
func replicaSetEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object, sentryEvent *sentry.Event) error {
264-
265260
replicasetObj, ok := object.(*appsv1.ReplicaSet)
266261
if !ok {
267262
return errors.New("failed to cast object to ReplicaSet object")
@@ -291,7 +286,6 @@ func replicaSetEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.
291286
}
292287

293288
func deploymentEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.Object, sentryEvent *sentry.Event) error {
294-
295289
deploymentObj, ok := object.(*appsv1.Deployment)
296290
if !ok {
297291
return errors.New("failed to cast object to Deployment object")
@@ -323,7 +317,6 @@ func deploymentEnhancer(ctx context.Context, scope *sentry.Scope, object metav1.
323317
// and returns an empty slice if the object has
324318
// no owning objects
325319
func findRootOwners(ctx context.Context, kindObjPair *KindObjectPair) ([]KindObjectPair, error) {
326-
327320
// Use DFS to find the leaves of the owner references graph
328321
rootOwners, err := ownerRefDFS(ctx, kindObjPair)
329322
if err != nil {
@@ -340,7 +333,6 @@ func findRootOwners(ctx context.Context, kindObjPair *KindObjectPair) ([]KindObj
340333

341334
// Performs DFS to find the leaves the owner references graph
342335
func ownerRefDFS(ctx context.Context, kindObjPair *KindObjectPair) ([]KindObjectPair, error) {
343-
344336
parents := kindObjPair.object.GetOwnerReferences()
345337
// the owners slice to be returned
346338
rootOwners := []KindObjectPair{}

enhancers_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
)
1212

1313
func TestRunEnhancers(t *testing.T) {
14-
1514
// Create empty context
1615
ctx := context.Background()
1716
// Create simple fake client

informer_cronjobs.go

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
)
1313

1414
func createCronjobInformer(ctx context.Context, factory informers.SharedInformerFactory, namespace string) (cache.SharedIndexInformer, error) {
15-
1615
logger := zerolog.Ctx(ctx)
1716

1817
logger.Debug().Msgf("Starting cronjob informer\n")

informer_deployments.go

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
)
1010

1111
func createDeploymentInformer(ctx context.Context, factory informers.SharedInformerFactory, namespace string) (cache.SharedIndexInformer, error) {
12-
1312
logger := zerolog.Ctx(ctx)
1413

1514
logger.Debug().Msgf("starting deployment informer\n")

informer_jobs.go

-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
)
1313

1414
func createJobInformer(ctx context.Context, factory informers.SharedInformerFactory, namespace string) (cache.SharedIndexInformer, error) {
15-
1615
logger := zerolog.Ctx(ctx)
1716

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

3332
handler.UpdateFunc = func(oldObj, newObj interface{}) {
34-
3533
oldJob := oldObj.(*batchv1.Job)
3634
newJob := newObj.(*batchv1.Job)
3735

informer_replicasets.go

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
)
1111

1212
func createReplicasetInformer(ctx context.Context, factory informers.SharedInformerFactory, namespace string) (cache.SharedIndexInformer, error) {
13-
1413
logger := zerolog.Ctx(ctx)
1514

1615
logger.Debug().Msgf("starting replicaset informer\n")

informers.go

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ var deploymentInformer cache.SharedIndexInformer
1818
// if we opt into cronjob, attach the job/cronjob event handlers
1919
// and add to the crons monitor data struct for Sentry Crons
2020
func startInformers(ctx context.Context, namespace string) error {
21-
2221
clientset, err := getClientsetFromContext(ctx)
2322
if err != nil {
2423
return errors.New("failed to get clientset")

sentry_dsn_data.go

-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ func (d *DsnClientMapping) GetClientFromMap(dsn string) (*sentry.Client, bool) {
6464
}
6565

6666
func (d *DsnClientMapping) GetClientFromObject(ctx context.Context, object metav1.Object, clientOptions sentry.ClientOptions) (*sentry.Client, bool) {
67-
6867
// If the custom DSN flag is set to false
6968
// then avoid searching for the custom DSN
7069
// or adding an alternative client and instead
@@ -99,7 +98,6 @@ func (d *DsnClientMapping) GetClientFromObject(ctx context.Context, object metav
9998

10099
// Recursive function to find if there is a DSN annotation
101100
func searchDsn(ctx context.Context, obj metav1.Object) (string, error) {
102-
103101
dsn, ok := obj.GetAnnotations()[DSNAnnotation]
104102
if ok {
105103
return dsn, nil
@@ -120,7 +118,6 @@ func searchDsn(ctx context.Context, obj metav1.Object) (string, error) {
120118
}
121119

122120
func findObject(ctx context.Context, kind string, namespace string, name string) (metav1.Object, bool) {
123-
124121
clientset, err := getClientsetFromContext(ctx)
125122
if err != nil {
126123
return nil, false
@@ -157,7 +154,6 @@ func findObject(ctx context.Context, kind string, namespace string, name string)
157154
obj, ok, err := deploymentInformer.GetIndexer().GetByKey(namespace + "/" + name)
158155
if ok && err == nil {
159156
deployment = obj.(*v1.Deployment)
160-
161157
}
162158
}
163159
if deployment == nil {

0 commit comments

Comments
 (0)