Skip to content

Commit e5b4542

Browse files
authored
Merge pull request #797 from kolyshkin/golangci-lint-v2
Switch to golangci lint v2, fix or suppress found warnings
2 parents 0ea5ed0 + 79a3f0f commit e5b4542

File tree

29 files changed

+91
-85
lines changed

29 files changed

+91
-85
lines changed

.github/workflows/test.yml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,47 @@ on:
1111
jobs:
1212

1313
lint:
14-
runs-on: ubuntu-22.04
14+
runs-on: ubuntu-24.04
1515
steps:
16-
- uses: actions/setup-go@v5
16+
- uses: actions/setup-go@v6
1717
with:
18-
go-version: 1.x # latest
19-
- uses: actions/checkout@v4
20-
- uses: golangci/golangci-lint-action@v6
18+
go-version: stable
19+
- uses: actions/checkout@v5
20+
- uses: golangci/golangci-lint-action@v8
2121
with:
22-
version: v1.61
22+
version: v2.6
2323

2424
commit:
25-
runs-on: ubuntu-22.04
25+
runs-on: ubuntu-24.04
2626
# Only check commits on pull requests.
2727
if: github.event_name == 'pull_request'
2828
steps:
2929
- name: get pr commits
3030
id: 'get-pr-commits'
31-
uses: tim-actions/get-pr-commits@v1.1.0
31+
uses: tim-actions/get-pr-commits@v1.3.1
3232
with:
3333
token: ${{ secrets.GITHUB_TOKEN }}
3434

3535
- name: check subject line length
36-
uses: tim-actions/[email protected].1
36+
uses: tim-actions/[email protected].2
3737
with:
3838
commits: ${{ steps.get-pr-commits.outputs.commits }}
3939
pattern: '^.{0,72}(\n.*)*$'
4040
error: 'Subject too long (max 72)'
4141

4242
test:
43-
runs-on: ubuntu-22.04
43+
runs-on: ubuntu-24.04
4444
strategy:
4545
fail-fast: false
4646
matrix:
47-
go-version: [1.21.x, 1.22.x, 1.23.x]
47+
go-version: [1.21.x, oldstable, stable]
4848
race: ["-race", ""]
4949

5050
steps:
5151
- name: checkout
52-
uses: actions/checkout@v4
52+
uses: actions/checkout@v5
5353
- name: install go ${{ matrix.go-version }}
54-
uses: actions/setup-go@v5
54+
uses: actions/setup-go@v6
5555
with:
5656
go-version: ${{ matrix.go-version }}
5757
- name: build

.golangci.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
# For documentation, see https://golangci-lint.run/usage/configuration/
1+
version: "2"
2+
3+
formatters:
4+
enable:
5+
- gofmt
26

37
linters:
48
disable:
59
- errcheck
10+
settings:
11+
staticcheck:
12+
checks:
13+
- all # Enable all checks, except...
14+
- -ST1005 # https://staticcheck.dev/docs/checks/#ST1005 Incorrectly formatted error string.
15+
- -ST1000 # https://staticcheck.dev/docs/checks/#ST1000 Incorrect or missing package comment.

cgroups/cgroups.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"slices"
89
"strings"
910

1011
rspec "github.com/opencontainers/runtime-spec/specs-go"
@@ -89,10 +90,8 @@ func GetSubsystemPath(pid int, subsystem string) (string, error) {
8990
continue
9091
}
9192
subelems := strings.Split(elem[1], ",")
92-
for _, subelem := range subelems {
93-
if subelem == subsystem {
94-
return elem[2], nil
95-
}
93+
if slices.Contains(subelems, subsystem) {
94+
return elem[2], nil
9695
}
9796
}
9897

cmd/runtimetest/main.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,13 @@ func (c *complianceTester) validateLinuxProcess(spec *rspec.Spec) error {
236236

237237
args := bytes.Split(bytes.Trim(cmdlineBytes, "\x00"), []byte("\x00"))
238238
c.harness.Ok(len(args) == len(spec.Process.Args), "has expected number of process arguments")
239-
_ = c.harness.YAML(map[string]interface{}{
239+
_ = c.harness.YAML(map[string]any{
240240
"expected": spec.Process.Args,
241241
"actual": args,
242242
})
243243
for i, a := range args {
244244
c.harness.Ok(string(a) == spec.Process.Args[i], fmt.Sprintf("has expected process argument %d", i))
245-
_ = c.harness.YAML(map[string]interface{}{
245+
_ = c.harness.YAML(map[string]any{
246246
"index": i,
247247
"expected": spec.Process.Args[i],
248248
"actual": string(a),
@@ -362,7 +362,7 @@ func (c *complianceTester) validateRlimits(spec *rspec.Spec) error {
362362
if err != nil {
363363
return err
364364
}
365-
_ = c.harness.YAML(map[string]interface{}{
365+
_ = c.harness.YAML(map[string]any{
366366
"level": rfcError.Level.String(),
367367
"reference": rfcError.Reference,
368368
"type": r.Type,
@@ -374,7 +374,7 @@ func (c *complianceTester) validateRlimits(spec *rspec.Spec) error {
374374
if err != nil {
375375
return err
376376
}
377-
_ = c.harness.YAML(map[string]interface{}{
377+
_ = c.harness.YAML(map[string]any{
378378
"level": rfcError.Level.String(),
379379
"reference": rfcError.Reference,
380380
"type": r.Type,
@@ -392,7 +392,7 @@ func (c *complianceTester) validateSysctls(spec *rspec.Spec) error {
392392
}
393393

394394
for k, v := range spec.Linux.Sysctl {
395-
keyPath := filepath.Join("/proc/sys", strings.Replace(k, ".", "/", -1))
395+
keyPath := filepath.Join("/proc/sys", strings.ReplaceAll(k, ".", "/"))
396396
vBytes, err := os.ReadFile(keyPath)
397397
if err != nil {
398398
return err
@@ -454,9 +454,10 @@ func testFileReadAccess(path string) (readable bool, err error) {
454454
defer f.Close()
455455
b := make([]byte, 1)
456456
_, err = f.Read(b)
457-
if err == nil {
457+
switch err {
458+
case nil:
458459
return true, nil
459-
} else if err == io.EOF {
460+
case io.EOF:
460461
// Our validation/ tests only use non-empty files for read-access
461462
// tests. So if we get an EOF on the first read, the runtime did
462463
// successfully block readability.
@@ -727,7 +728,7 @@ func (c *complianceTester) validateDevice(device *rspec.LinuxDevice, condition s
727728
if err != nil {
728729
return err
729730
}
730-
_ = c.harness.YAML(map[string]interface{}{
731+
_ = c.harness.YAML(map[string]any{
731732
"level": rfcError.Level.String(),
732733
"reference": rfcError.Reference,
733734
"path": device.Path,
@@ -738,7 +739,7 @@ func (c *complianceTester) validateDevice(device *rspec.LinuxDevice, condition s
738739
if err != nil {
739740
return err
740741
}
741-
_ = c.harness.YAML(map[string]interface{}{
742+
_ = c.harness.YAML(map[string]any{
742743
"level": rfcError.Level.String(),
743744
"reference": rfcError.Reference,
744745
"path": device.Path,
@@ -756,7 +757,7 @@ func (c *complianceTester) validateDevice(device *rspec.LinuxDevice, condition s
756757
if err != nil {
757758
return err
758759
}
759-
_ = c.harness.YAML(map[string]interface{}{
760+
_ = c.harness.YAML(map[string]any{
760761
"level": rfcError.Level.String(),
761762
"reference": rfcError.Reference,
762763
"path": device.Path,
@@ -777,7 +778,7 @@ func (c *complianceTester) validateDevice(device *rspec.LinuxDevice, condition s
777778
if err != nil {
778779
return err
779780
}
780-
_ = c.harness.YAML(map[string]interface{}{
781+
_ = c.harness.YAML(map[string]any{
781782
"level": rfcError.Level.String(),
782783
"reference": rfcError.Reference,
783784
"path": device.Path,
@@ -793,7 +794,7 @@ func (c *complianceTester) validateDevice(device *rspec.LinuxDevice, condition s
793794
if err != nil {
794795
return err
795796
}
796-
_ = c.harness.YAML(map[string]interface{}{
797+
_ = c.harness.YAML(map[string]any{
797798
"level": rfcError.Level.String(),
798799
"reference": rfcError.Reference,
799800
"path": device.Path,
@@ -838,7 +839,7 @@ func (c *complianceTester) validateDefaultSymlinks(spec *rspec.Spec) error {
838839
if err != nil {
839840
return err
840841
}
841-
_ = c.harness.YAML(map[string]interface{}{
842+
_ = c.harness.YAML(map[string]any{
842843
"level": rfcError.Level.String(),
843844
"reference": rfcError.Reference,
844845
"path": symlink,
@@ -991,7 +992,7 @@ func (c *complianceTester) validateOOMScoreAdj(spec *rspec.Spec) error {
991992
if err != nil {
992993
return err
993994
}
994-
_ = c.harness.YAML(map[string]interface{}{
995+
_ = c.harness.YAML(map[string]any{
995996
"level": rfcError.Level.String(),
996997
"reference": rfcError.Reference,
997998
"expected": expected,
@@ -1052,7 +1053,7 @@ func (c *complianceTester) validateIDMappings(mappings []rspec.LinuxIDMapping, p
10521053
return err
10531054
}
10541055
c.harness.Ok(len(idMaps) == len(mappings), fmt.Sprintf("%s has expected number of mappings", path))
1055-
_ = c.harness.YAML(map[string]interface{}{
1056+
_ = c.harness.YAML(map[string]any{
10561057
"expected": mappings,
10571058
"actual": idMaps,
10581059
})
@@ -1185,13 +1186,13 @@ func (c *complianceTester) validatePosixMounts(spec *rspec.Spec) error {
11851186
} else {
11861187
rfcError, err = c.Ok(foundInOrder, specerror.MountsInOrder, spec.Version, fmt.Sprintf("mounts[%d] (%s) found in order", i, configMount.Destination))
11871188
}
1188-
_ = c.harness.YAML(map[string]interface{}{
1189+
_ = c.harness.YAML(map[string]any{
11891190
"level": rfcError.Level.String(),
11901191
"reference": rfcError.Reference,
11911192
"config": configMount,
11921193
"indexConfig": i,
11931194
"indexSystem": configSys[i],
1194-
"earlier": map[string]interface{}{
1195+
"earlier": map[string]any{
11951196
"config": spec.Mounts[highestMatchedConfig],
11961197
"indexConfig": highestMatchedConfig,
11971198
"indexSystem": configSys[highestMatchedConfig],
@@ -1310,10 +1311,11 @@ func run(context *cli.Context) error {
13101311
}
13111312

13121313
validations := defaultValidations
1313-
if platform == "linux" {
1314+
switch platform {
1315+
case "linux":
13141316
validations = append(validations, posixValidations...)
13151317
validations = append(validations, linuxValidations...)
1316-
} else if platform == "solaris" {
1318+
case "solaris":
13171319
validations = append(validations, posixValidations...)
13181320
}
13191321

generate/generate.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"slices"
910
"strings"
1011

1112
"github.com/moby/sys/capability"
@@ -88,7 +89,8 @@ func New(os string) (generator Generator, err error) {
8889
}
8990
}
9091

91-
if os == "linux" {
92+
switch os {
93+
case "linux":
9294
config.Process.Capabilities = &rspec.LinuxCapabilities{
9395
Bounding: []string{
9496
"CAP_CHOWN",
@@ -237,7 +239,7 @@ func New(os string) (generator Generator, err error) {
237239
},
238240
Seccomp: seccomp.DefaultProfile(&config),
239241
}
240-
} else if os == "freebsd" {
242+
case "freebsd":
241243
config.Mounts = []rspec.Mount{
242244
{
243245
Destination: "/dev",
@@ -593,12 +595,10 @@ func (g *Generator) ClearProcessAdditionalGids() {
593595
}
594596

595597
// AddProcessAdditionalGid adds an additional gid into g.Config.Process.AdditionalGids.
596-
func (g *Generator) AddProcessAdditionalGid(gid uint32) {
598+
func (g *Generator) AddProcessAdditionalGid(gid uint32) { //nolint:staticcheck // Ignore ST1003: method AddProcessAdditionalGid should be AddProcessAdditionalGID
597599
g.initConfigProcess()
598-
for _, group := range g.Config.Process.User.AdditionalGids {
599-
if group == gid {
600-
return
601-
}
600+
if slices.Contains(g.Config.Process.User.AdditionalGids, gid) {
601+
return
602602
}
603603
g.Config.Process.User.AdditionalGids = append(g.Config.Process.User.AdditionalGids, gid)
604604
}
@@ -868,7 +868,7 @@ func (g *Generator) DropLinuxResourcesHugepageLimit(pageSize string) {
868868
}
869869
}
870870

871-
// AddLinuxResourcesUnified sets the g.Config.Linux.Resources.Unified
871+
// SetLinuxResourcesUnified sets the g.Config.Linux.Resources.Unified.
872872
func (g *Generator) SetLinuxResourcesUnified(unified map[string]string) {
873873
g.initConfigLinuxResourcesUnified()
874874
for k, v := range unified {

generate/seccomp/seccomp_default.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package seccomp
33
import (
44
"runtime"
55

6-
"github.com/opencontainers/runtime-spec/specs-go"
76
rspec "github.com/opencontainers/runtime-spec/specs-go"
87
)
98

@@ -31,7 +30,7 @@ func arches() []rspec.Arch {
3130
}
3231

3332
// DefaultProfile defines the whitelist for the default seccomp profile.
34-
func DefaultProfile(rs *specs.Spec) *rspec.LinuxSeccomp {
33+
func DefaultProfile(rs *rspec.Spec) *rspec.LinuxSeccomp {
3534
syscalls := []rspec.LinuxSyscall{
3635
{
3736
Names: []string{

generate/seccomp/seccomp_default_linux.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build linux
2-
// +build linux
32

43
package seccomp
54

generate/seccomp/seccomp_default_unsupported.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build !linux
2-
// +build !linux
32

43
package seccomp
54

validate/validate.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"reflect"
1212
"regexp"
1313
"runtime"
14+
"slices"
1415
"strings"
1516
"unicode"
1617
"unicode/utf8"
@@ -440,7 +441,7 @@ func (v *Validator) CheckCapabilities() (errs error) {
440441
if effective && !permitted {
441442
errs = multierror.Append(errs, fmt.Errorf("effective capability %q is not allowed, as it's not permitted", capability))
442443
}
443-
if ambient && !(permitted && inheritable) {
444+
if ambient && !(permitted && inheritable) { //nolint:staticcheck // Ignore QF1001: could apply De Morgan's law.
444445
errs = multierror.Append(errs, fmt.Errorf("ambient capability %q is not allowed, as it's not permitted and inheribate", capability))
445446
}
446447
}
@@ -718,21 +719,18 @@ func (v *Validator) rlimitValid(rlimit rspec.POSIXRlimit) (errs error) {
718719
errs = multierror.Append(errs, fmt.Errorf("hard limit of rlimit %s should not be less than soft limit", rlimit.Type))
719720
}
720721

721-
if v.platform == "linux" {
722-
for _, val := range linuxRlimits {
723-
if val == rlimit.Type {
724-
return
725-
}
722+
switch v.platform {
723+
case "linux":
724+
if slices.Contains(linuxRlimits, rlimit.Type) {
725+
return
726726
}
727727
errs = multierror.Append(errs, specerror.NewError(specerror.PosixProcRlimitsTypeValueError, fmt.Errorf("rlimit type %q may not be valid", rlimit.Type), v.spec.Version))
728-
} else if v.platform == "solaris" {
729-
for _, val := range posixRlimits {
730-
if val == rlimit.Type {
731-
return
732-
}
728+
case "solaris":
729+
if slices.Contains(posixRlimits, rlimit.Type) {
730+
return
733731
}
734732
errs = multierror.Append(errs, specerror.NewError(specerror.PosixProcRlimitsTypeValueError, fmt.Errorf("rlimit type %q may not be valid", rlimit.Type), v.spec.Version))
735-
} else {
733+
default:
736734
logrus.Warnf("process.rlimits validation not yet implemented for platform %q", v.platform)
737735
}
738736

@@ -787,7 +785,7 @@ func checkMandatoryUnit(field reflect.Value, tagField reflect.StructField, paren
787785
return
788786
}
789787

790-
func checkMandatory(obj interface{}) (errs error) {
788+
func checkMandatory(obj any) (errs error) {
791789
objT := reflect.TypeOf(obj)
792790
objV := reflect.ValueOf(obj)
793791
if isStructPtr(objT) {

validate/validate_linux.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build linux
2-
// +build linux
32

43
package validate
54

0 commit comments

Comments
 (0)