Skip to content

Commit e4b4e7b

Browse files
authored
CLOUDP-252271 Use golangci-lint and gosec (mongodb#1558)
1 parent 8f69586 commit e4b4e7b

File tree

10 files changed

+23
-42
lines changed

10 files changed

+23
-42
lines changed

Diff for: .github/workflows/code-health.yml

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
Black:
88
runs-on: ubuntu-latest
99
steps:
10-
- uses: actions/checkout@v2
10+
- uses: actions/checkout@v4
1111

1212
- name: Black Check
1313
uses: jpetrucciani/black-check@7f5b2ad20fa5484f1884f07c1937e032ed8cd939
@@ -21,3 +21,13 @@ jobs:
2121
uses: jpetrucciani/mypy-check@179fdad632bf3ccf4cabb7ee4307ef25e51d2f96
2222
with:
2323
path: scripts/*/*.py
24+
25+
Golangci-lint:
26+
runs-on: ubuntu-latest
27+
steps:
28+
- uses: actions/checkout@v4
29+
- uses: actions/setup-go@v5
30+
with:
31+
go-version: stable
32+
- name: golangci-lint
33+
uses: golangci/golangci-lint-action@v6

Diff for: .github/workflows/main.yaml

-22
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,3 @@ jobs:
5151
5252
- name: Move the dependencies
5353
run: mv .venv /home/runner/work/_temp/_github_workflow
54-
55-
# This part is not needed until we can add GO linting
56-
# - name : Install Operator SDK
57-
# run: |
58-
# curl -s https://api.github.com/repos/operator-framework/operator-sdk/releases/latest | grep browser_download_url | grep x86_64-linux-gnu | cut -d '"' -f 4 | wget -i -
59-
# sudo mv operator-sdk-*-x86_64-linux-gnu /usr/local/bin/operator-sdk
60-
# sudo chmod 777 /usr/local/bin/operator-sdk
61-
# - name: Generate DeepCopy
62-
# Run: operator-sdk generate k8s
63-
64-
- name: Lint Code Base
65-
uses: docker://github/super-linter:v4
66-
env:
67-
VALIDATE_ALL_CODEBASE: true
68-
# Now we set the PYTHONPATH to the path of the dependencies *inside* the container
69-
PYTHONPATH: "/github/workspace/:\
70-
/github/workflow/.venv/lib/python3.6/site-packages"
71-
VALIDATE_YAML: true
72-
VALIDATE_PYTHON: true
73-
VALIDATE_BASH: true
74-
FILTER_REGEX_EXCLUDE: "/helm-charts/charts/community-operator/templates/*"
75-
# VALIDATE_GO: true This is currently broken: https://github.com/github/super-linter/issues/143

Diff for: .golangci.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,14 @@ linters:
2323
- staticcheck
2424
- unused
2525
- gosimple
26-
- structcheck
27-
- varcheck
2826
- ineffassign
29-
- deadcode
3027
- typecheck
3128
- rowserrcheck
3229
- gosec
3330
- unconvert
3431

3532
run:
36-
modules-download-mode:
33+
modules-download-mode: mod
3734
# timeout for analysis, e.g. 30s, 5m, default is 1m
3835
timeout: 5m
3936
# default concurrency is a available CPU number

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Reviewers, please ensure that the CLA has been signed by referring to [the contr
6969

7070
This project uses the following linters upon every Pull Request:
7171

72+
* `gosec` is a tool that find security problems in the code
7273
* `Black` is a tool that verifies if Python code is properly formatted
7374
* `MyPy` is a Static Type Checker for Python
7475
* `Kube-linter` is a tool that verified if all Kubernetes YAML manifests are formatted correctly

Diff for: cmd/readiness/readiness_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestDeadlockDetection(t *testing.T) {
106106
isReadyExpected: false,
107107
},
108108
}
109-
for testName, _ := range tests {
109+
for testName := range tests {
110110
testConfig := tests[testName]
111111
t.Run(testName, func(t *testing.T) {
112112
ready, err := isPodReady(ctx, testConfig.conf)
@@ -225,7 +225,7 @@ func TestObtainingCurrentStep(t *testing.T) {
225225
expectedStep: "test",
226226
},
227227
}
228-
for testName, _ := range tests {
228+
for testName := range tests {
229229
testConfig := tests[testName]
230230
t.Run(testName, func(t *testing.T) {
231231
step := findCurrentStep(testConfig.processStatuses)

Diff for: controllers/construct/mongodbstatefulset.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ export NSS_WRAPPER_PASSWD=/tmp/passwd
6868
export LD_PRELOAD=libnss_wrapper.so
6969
export NSS_WRAPPER_GROUP=/etc/group
7070
fi
71-
7271
`
72+
//nolint:gosec //The credentials path is hardcoded in the container.
7373
MongodbUserCommandWithAPIKeyExport = `current_uid=$(id -u)
7474
AGENT_API_KEY="$(cat /mongodb-automation/agent-api-key/agentApiKey)"
7575
declare -r current_uid

Diff for: test/e2e/mongodbtests/mongodbtests.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func HasExpectedMetadata(ctx context.Context, mdb *mdbv1.MongoDBCommunity, expec
262262
assert.NoError(t, err)
263263
assert.NotEmpty(t, statefulSetList.Items)
264264
for _, s := range statefulSetList.Items {
265-
containsMetadata(t, &s.ObjectMeta, expectedLabels, expectedAnnotations, "statefulset "+s.Name)
265+
containsMetadata(t, s.ObjectMeta, expectedLabels, expectedAnnotations, "statefulset "+s.Name)
266266
}
267267

268268
volumeList := corev1.PersistentVolumeList{}
@@ -272,7 +272,7 @@ func HasExpectedMetadata(ctx context.Context, mdb *mdbv1.MongoDBCommunity, expec
272272
for _, s := range volumeList.Items {
273273
volName := s.Name
274274
if strings.HasPrefix(volName, "data-volume-") || strings.HasPrefix(volName, "logs-volume-") {
275-
containsMetadata(t, &s.ObjectMeta, expectedLabels, expectedAnnotations, "volume "+volName)
275+
containsMetadata(t, s.ObjectMeta, expectedLabels, expectedAnnotations, "volume "+volName)
276276
}
277277
}
278278

@@ -304,12 +304,12 @@ func HasExpectedMetadata(ctx context.Context, mdb *mdbv1.MongoDBCommunity, expec
304304
continue
305305
}
306306

307-
containsMetadata(t, &s.ObjectMeta, expectedLabels, expectedAnnotations, "pod "+s.Name)
307+
containsMetadata(t, s.ObjectMeta, expectedLabels, expectedAnnotations, "pod "+s.Name)
308308
}
309309
}
310310
}
311311

312-
func containsMetadata(t *testing.T, metadata *metav1.ObjectMeta, expectedLabels map[string]string, expectedAnnotations map[string]string, msg string) {
312+
func containsMetadata(t *testing.T, metadata metav1.ObjectMeta, expectedLabels map[string]string, expectedAnnotations map[string]string, msg string) {
313313
labels := metadata.Labels
314314
for k, v := range expectedLabels {
315315
assert.Contains(t, labels, k, msg+" has label "+k)

Diff for: test/e2e/replica_set_arbiter/replica_set_arbiter_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ func TestReplicaSetArbiter(t *testing.T) {
6969
resourceName: "mdb4",
7070
},
7171
}
72-
for testName, _ := range tests {
72+
for testName := range tests {
7373
t.Run(testName, func(t *testing.T) {
74-
testConfig, _ := tests[testName]
74+
testConfig := tests[testName]
7575
mdb, user := e2eutil.NewTestMongoDB(testCtx, testConfig.resourceName, "")
7676
mdb.Spec.Arbiters = testConfig.numberOfArbiters
7777
mdb.Spec.Members = testConfig.numberOfMembers

Diff for: test/e2e/replica_set_operator_upgrade/replica_set_operator_upgrade_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestReplicaSetOperatorUpgrade(t *testing.T) {
7373
// TestReplicaSetOperatorUpgradeFrom0_7_2 is intended to be run locally not in CI.
7474
// It simulates deploying cluster using community operator 0.7.2 and then upgrading it using newer version.
7575
func TestReplicaSetOperatorUpgradeFrom0_7_2(t *testing.T) {
76-
ctx := context.Background()
76+
ctx := context.Background() //nolint
7777
t.Skip("Supporting this test in CI requires installing also CRDs from release v0.7.2")
7878
resourceName := "mdb-upg"
7979
testConfig := setup.LoadTestConfigFromEnv()

Diff for: test/e2e/util/wait/wait.go

-5
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,6 @@ func waitForStatefulSetCondition(ctx context.Context, t *testing.T, mdb *mdbv1.M
161161
return waitForStatefulSetConditionWithSpecificSts(ctx, t, mdb, MembersStatefulSet, waitOpts, condition)
162162
}
163163

164-
func waitForStatefulSetConditionArbiters(ctx context.Context, t *testing.T, mdb *mdbv1.MongoDBCommunity, waitOpts Options, condition func(set appsv1.StatefulSet) bool) error {
165-
// uses members statefulset
166-
return waitForStatefulSetConditionWithSpecificSts(ctx, t, mdb, ArbitersStatefulSet, waitOpts, condition)
167-
}
168-
169164
func ForPodReadiness(ctx context.Context, t *testing.T, isReady bool, containerName string, timeout time.Duration, pod corev1.Pod) error {
170165
return wait.PollUntilContextTimeout(ctx, time.Second*3, timeout, false, func(ctx context.Context) (done bool, err error) {
171166
err = e2eutil.TestClient.Get(ctx, types.NamespacedName{Name: pod.Name, Namespace: pod.Namespace}, &pod)

0 commit comments

Comments
 (0)