Skip to content

Commit bf1e685

Browse files
Merge pull request #191 from saschagrunert/lint
Enable and fix more linters
2 parents 79359c6 + a0ddd97 commit bf1e685

File tree

6 files changed

+55
-50
lines changed

6 files changed

+55
-50
lines changed

.golangci.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ linters:
2525
- exportloopref
2626
- forbidigo
2727
- forcetypeassert
28+
- gci
29+
- ginkgolinter
2830
- gocheckcompilerdirectives
31+
- gochecknoglobals
2932
- gochecknoinits
3033
- gochecksumtype
3134
- goconst
3235
- gocritic
3336
- gocyclo
37+
- godot
3438
- godox
3539
- gofmt
3640
- gofumpt
@@ -54,6 +58,7 @@ linters:
5458
- misspell
5559
- musttag
5660
- nakedret
61+
- nestif
5762
- nilerr
5863
- nilnil
5964
- noctx
@@ -66,6 +71,7 @@ linters:
6671
- promlinter
6772
- protogetter
6873
- reassign
74+
- revive
6975
- rowserrcheck
7076
- sloglint
7177
- spancheck
@@ -90,26 +96,26 @@ linters:
9096
# - depguard
9197
# - exhaustruct
9298
# - funlen
93-
# - gci
94-
# - ginkgolinter
95-
# - gochecknoglobals
9699
# - gocognit
97-
# - godot
98100
# - goerr113
99101
# - gomnd
100102
# - gosec
101103
# - ireturn
102104
# - lll
103-
# - nestif
104105
# - nlreturn
105106
# - nonamedreturns
106-
# - revive
107107
# - tagliatelle
108108
# - testpackage
109109
# - varnamelen
110110
# - wrapcheck
111111
# - wsl
112112
linters-settings:
113+
revive:
114+
rules:
115+
- name: dot-imports
116+
disabled: true
117+
nestif:
118+
min-complexity: 10
113119
gocritic:
114120
enabled-checks:
115121
- appendAssign

pkg/ocicni/ocicni.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type cniNetwork struct {
5959
config *libcni.NetworkConfigList
6060
}
6161

62-
var errMissingDefaultNetwork = "No CNI configuration file in %s. Has your network provider started?"
62+
const errMissingDefaultNetwork = "no CNI configuration file in %s. Has your network provider started?"
6363

6464
type podLock struct {
6565
// Count of in-flight operations for this pod; when this reaches zero
@@ -210,7 +210,7 @@ func InitCNINoInotify(defaultNetName, confDir, cacheDir string, binDirs ...strin
210210
return initCNI(nil, cacheDir, defaultNetName, confDir, false, binDirs...)
211211
}
212212

213-
// Internal function to allow faking out exec functions for testing
213+
// Internal function to allow faking out exec functions for testing.
214214
func initCNI(exec cniinvoke.Exec, cacheDir, defaultNetName, confDir string, useInotify bool, binDirs ...string) (CNIPlugin, error) {
215215
if confDir == "" {
216216
confDir = DefaultConfDir

pkg/ocicni/ocicni_test.go

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ import (
1212
"reflect"
1313
"strings"
1414

15-
"github.com/vishvananda/netlink"
16-
1715
"github.com/containernetworking/cni/libcni"
1816
"github.com/containernetworking/cni/pkg/types"
1917
cniv04 "github.com/containernetworking/cni/pkg/types/040"
2018
"github.com/containernetworking/cni/pkg/version"
2119
"github.com/containernetworking/plugins/pkg/ns"
2220
"github.com/containernetworking/plugins/pkg/testutils"
23-
2421
. "github.com/onsi/ginkgo/v2"
2522
. "github.com/onsi/gomega"
23+
"github.com/vishvananda/netlink"
2624
)
2725

2826
func writeConfig(dir, fileName, netName, plugin, vers string) (conf, confPath string, err error) {
@@ -88,7 +86,7 @@ func (f *fakeExec) addPlugin(expectedEnv []string, expectedConf string, result t
8886
})
8987
}
9088

91-
// Ensure everything in needles is also present in haystack
89+
// Ensure everything in needles is also present in haystack.
9290
func matchArray(needles, haystack []string) {
9391
Expect(len(needles)).To(BeNumerically("<=", len(haystack)))
9492
for _, e1 := range needles {
@@ -170,7 +168,7 @@ func (f *fakeExec) ExecPlugin(ctx context.Context, pluginPath string, stdinData
170168
}
171169

172170
func (f *fakeExec) FindInPath(plugin string, paths []string) (string, error) {
173-
Expect(len(paths)).To(BeNumerically(">", 0))
171+
Expect(paths).ToNot(BeEmpty())
174172

175173
if f.failFind {
176174
return "", fmt.Errorf("failed to find plugin %q in path %s", plugin, paths)
@@ -233,7 +231,7 @@ var _ = Describe("ocicni operations", func() {
233231
Expect(ok).To(BeTrue())
234232
net := tmp.getDefaultNetwork()
235233
Expect(net.name).To(Equal("test"))
236-
Expect(len(net.config.Plugins)).To(BeNumerically(">", 0))
234+
Expect(net.config.Plugins).ToNot(BeEmpty())
237235
Expect(net.config.Plugins[0].Network.Type).To(Equal("myplugin"))
238236

239237
Expect(ocicni.Shutdown()).NotTo(HaveOccurred())
@@ -256,7 +254,7 @@ var _ = Describe("ocicni operations", func() {
256254
Expect(ok).To(BeTrue())
257255
net := tmp.getDefaultNetwork()
258256
Expect(net.name).To(Equal("test"))
259-
Expect(len(net.config.Plugins)).To(BeNumerically(">", 0))
257+
Expect(net.config.Plugins).ToNot(BeEmpty())
260258
Expect(net.config.Plugins[0].Network.Type).To(Equal("myplugin"))
261259

262260
Expect(ocicni.Shutdown()).NotTo(HaveOccurred())
@@ -314,7 +312,7 @@ var _ = Describe("ocicni operations", func() {
314312
Expect(ok).To(BeTrue())
315313
net := tmp.getDefaultNetwork()
316314
Expect(net.name).To(Equal("test"))
317-
Expect(len(net.config.Plugins)).To(BeNumerically(">", 0))
315+
Expect(net.config.Plugins).ToNot(BeEmpty())
318316
Expect(net.config.Plugins[0].Network.Type).To(Equal("myplugin"))
319317

320318
// If a CNI config file is updated, default network name should be reloaded real-time
@@ -325,7 +323,7 @@ var _ = Describe("ocicni operations", func() {
325323

326324
net = tmp.getDefaultNetwork()
327325
Expect(net.name).To(Equal("secondary"))
328-
Expect(len(net.config.Plugins)).To(BeNumerically(">", 0))
326+
Expect(net.config.Plugins).ToNot(BeEmpty())
329327
Expect(net.config.Plugins[0].Network.Type).To(Equal("testplugin"))
330328

331329
Expect(ocicni.Shutdown()).NotTo(HaveOccurred())
@@ -347,7 +345,7 @@ var _ = Describe("ocicni operations", func() {
347345
Expect(ok).To(BeTrue())
348346
net := tmp.getDefaultNetwork()
349347
Expect(net.name).To(Equal("test"))
350-
Expect(len(net.config.Plugins)).To(BeNumerically(">", 0))
348+
Expect(net.config.Plugins).ToNot(BeEmpty())
351349
Expect(net.config.Plugins[0].Network.Type).To(Equal("myplugin"))
352350

353351
// If a CNI config file is updated, default network name should be reloaded real-time
@@ -358,7 +356,7 @@ var _ = Describe("ocicni operations", func() {
358356

359357
net = tmp.getDefaultNetwork()
360358
Expect(net.name).To(Equal("test"))
361-
Expect(len(net.config.Plugins)).To(BeNumerically(">", 0))
359+
Expect(net.config.Plugins).ToNot(BeEmpty())
362360
Expect(net.config.Plugins[0].Network.Type).To(Equal("testplugin"))
363361

364362
Expect(ocicni.Shutdown()).NotTo(HaveOccurred())
@@ -377,7 +375,7 @@ var _ = Describe("ocicni operations", func() {
377375
Expect(ok).To(BeTrue())
378376
net := tmp.getDefaultNetwork()
379377
Expect(net.name).To(Equal("test"))
380-
Expect(len(net.config.Plugins)).To(BeNumerically(">", 0))
378+
Expect(net.config.Plugins).ToNot(BeEmpty())
381379
Expect(net.config.Plugins[0].Network.Type).To(Equal("myplugin"))
382380

383381
// Delete the default network config, ensure ocicni begins to
@@ -410,7 +408,7 @@ var _ = Describe("ocicni operations", func() {
410408
Expect(ok).To(BeTrue())
411409
net := tmp.getDefaultNetwork()
412410
Expect(net.name).To(Equal("test"))
413-
Expect(len(net.config.Plugins)).To(BeNumerically(">", 0))
411+
Expect(net.config.Plugins).ToNot(BeEmpty())
414412
Expect(net.config.Plugins[0].Network.Type).To(Equal("myplugin"))
415413

416414
// If a new CNI config file is added, default network name should be reloaded real-time
@@ -422,7 +420,7 @@ var _ = Describe("ocicni operations", func() {
422420

423421
net = tmp.getDefaultNetwork()
424422
Expect(net.name).To(Equal("newdefault"))
425-
Expect(len(net.config.Plugins)).To(BeNumerically(">", 0))
423+
Expect(net.config.Plugins).ToNot(BeEmpty())
426424
Expect(net.config.Plugins[0].Network.Type).To(Equal("myplugin"))
427425

428426
Expect(ocicni.Shutdown()).NotTo(HaveOccurred())
@@ -442,7 +440,7 @@ var _ = Describe("ocicni operations", func() {
442440
cniConfig := libcni.NewCNIConfig([]string{"/opt/cni/bin"}, &fakeExec{})
443441
netMap, defname, err := loadNetworks(context.TODO(), tmpDir, cniConfig)
444442
Expect(err).NotTo(HaveOccurred())
445-
Expect(len(netMap)).To(Equal(4))
443+
Expect(netMap).To(HaveLen(4))
446444
// filenames are sorted asciibetically
447445
Expect(defname).To(Equal("network2"))
448446
})
@@ -451,7 +449,7 @@ var _ = Describe("ocicni operations", func() {
451449
cniConfig := libcni.NewCNIConfig([]string{"/opt/cni/bin"}, &fakeExec{})
452450
netMap, defname, err := loadNetworks(context.TODO(), tmpDir, cniConfig)
453451
Expect(err).NotTo(HaveOccurred())
454-
Expect(len(netMap)).To(Equal(0))
452+
Expect(netMap).To(BeEmpty())
455453
// filenames are sorted asciibetically
456454
Expect(defname).To(Equal(""))
457455
})
@@ -471,7 +469,7 @@ var _ = Describe("ocicni operations", func() {
471469

472470
// We expect the type=myplugin2 network be ignored since it
473471
// was read earlier than the type=myplugin network with the same name
474-
Expect(len(netMap)).To(Equal(2))
472+
Expect(netMap).To(HaveLen(2))
475473
net, ok := netMap["network2"]
476474
Expect(ok).To(BeTrue())
477475
Expect(net.config.Plugins[0].Network.Type).To(Equal("myplugin"))
@@ -500,7 +498,7 @@ var _ = Describe("ocicni operations", func() {
500498
runtimeConfig = &RuntimeConfig{IP: "172.16.0.1"}
501499
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
502500
Expect(err).NotTo(HaveOccurred())
503-
Expect(len(rt.Args)).To(Equal(6))
501+
Expect(rt.Args).To(HaveLen(6))
504502
Expect(rt.Args[5][1]).To(Equal("172.16.0.1"))
505503

506504
// runtimeConfig with invalid MAC
@@ -512,14 +510,14 @@ var _ = Describe("ocicni operations", func() {
512510
runtimeConfig = &RuntimeConfig{MAC: "9e:0c:d9:b2:f0:a6"}
513511
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
514512
Expect(err).NotTo(HaveOccurred())
515-
Expect(len(rt.Args)).To(Equal(6))
513+
Expect(rt.Args).To(HaveLen(6))
516514
Expect(rt.Args[5][1]).To(Equal("9e:0c:d9:b2:f0:a6"))
517515

518516
// runtimeConfig with valid IP and valid MAC
519517
runtimeConfig = &RuntimeConfig{IP: "172.16.0.1", MAC: "9e:0c:d9:b2:f0:a6"}
520518
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
521519
Expect(err).NotTo(HaveOccurred())
522-
Expect(len(rt.Args)).To(Equal(7))
520+
Expect(rt.Args).To(HaveLen(7))
523521
Expect(rt.Args[5][1]).To(Equal("172.16.0.1"))
524522
Expect(rt.Args[6][1]).To(Equal("9e:0c:d9:b2:f0:a6"))
525523

@@ -538,8 +536,8 @@ var _ = Describe("ocicni operations", func() {
538536
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
539537
Expect(err).NotTo(HaveOccurred())
540538
pm, ok := rt.CapabilityArgs["portMappings"].([]PortMapping)
541-
Expect(ok).To(Equal(true))
542-
Expect(len(pm)).To(Equal(1))
539+
Expect(ok).To(BeTrue())
540+
Expect(pm).To(HaveLen(1))
543541
Expect(pm[0].HostPort).To(Equal(int32(100)))
544542
Expect(pm[0].ContainerPort).To(Equal(int32(50)))
545543
Expect(pm[0].Protocol).To(Equal("tcp"))
@@ -560,7 +558,7 @@ var _ = Describe("ocicni operations", func() {
560558
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
561559
Expect(err).NotTo(HaveOccurred())
562560
bw, ok := rt.CapabilityArgs["bandwidth"].(map[string]uint64)
563-
Expect(ok).To(Equal(true))
561+
Expect(ok).To(BeTrue())
564562
Expect(bw["ingressRate"]).To(Equal(uint64(1)))
565563
Expect(bw["ingressBurst"]).To(Equal(uint64(2)))
566564
Expect(bw["egressRate"]).To(Equal(uint64(3)))
@@ -581,16 +579,16 @@ var _ = Describe("ocicni operations", func() {
581579
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
582580
Expect(err).NotTo(HaveOccurred())
583581
ir, ok := rt.CapabilityArgs["ipRanges"].([][]IpRange)
584-
Expect(ok).To(Equal(true))
585-
Expect(len(ir)).To(Equal(1))
586-
Expect(len(ir[0])).To(Equal(1))
582+
Expect(ok).To(BeTrue())
583+
Expect(ir).To(HaveLen(1))
584+
Expect(ir[0]).To(HaveLen(1))
587585
Expect(ir[0][0].Gateway).To(Equal("192.168.0.254"))
588586

589587
runtimeConfig = &RuntimeConfig{CgroupPath: "/slice/pod/testing"}
590588
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
591589
Expect(err).NotTo(HaveOccurred())
592590
cg, ok := rt.CapabilityArgs["cgroupPath"].(string)
593-
Expect(ok).To(Equal(true))
591+
Expect(ok).To(BeTrue())
594592
Expect(cg).To(Equal("/slice/pod/testing"))
595593
})
596594

@@ -631,7 +629,7 @@ var _ = Describe("ocicni operations", func() {
631629
results, err := ocicni.SetUpPod(podNet)
632630
Expect(err).NotTo(HaveOccurred())
633631
Expect(fake.addIndex).To(Equal(len(fake.plugins)))
634-
Expect(len(results)).To(Equal(1))
632+
Expect(results).To(HaveLen(1))
635633
r, ok := results[0].Result.(*cniv04.Result)
636634
Expect(ok).To(BeTrue())
637635
Expect(reflect.DeepEqual(r, expectedResult)).To(BeTrue())
@@ -717,7 +715,7 @@ var _ = Describe("ocicni operations", func() {
717715
results, err := ocicni.SetUpPod(podNet)
718716
Expect(err).NotTo(HaveOccurred())
719717
Expect(fake.addIndex).To(Equal(len(fake.plugins)))
720-
Expect(len(results)).To(Equal(2))
718+
Expect(results).To(HaveLen(2))
721719
r, ok := results[0].Result.(*cniv04.Result)
722720
Expect(ok).To(BeTrue())
723721
Expect(reflect.DeepEqual(r, expectedResult1)).To(BeTrue())
@@ -797,7 +795,7 @@ var _ = Describe("ocicni operations", func() {
797795
results, err := ocicni.SetUpPod(podNet)
798796
Expect(err).NotTo(HaveOccurred())
799797
Expect(fake.addIndex).To(Equal(len(fake.plugins)))
800-
Expect(len(results)).To(Equal(2))
798+
Expect(results).To(HaveLen(2))
801799
r, ok := results[0].Result.(*cniv04.Result)
802800
Expect(ok).To(BeTrue())
803801
Expect(reflect.DeepEqual(r, expectedResult1)).To(BeTrue())
@@ -807,7 +805,7 @@ var _ = Describe("ocicni operations", func() {
807805

808806
resultsStatus, errStatus := ocicni.GetPodNetworkStatus(podNet)
809807
Expect(errStatus).NotTo(HaveOccurred())
810-
Expect(len(resultsStatus)).To(Equal(2))
808+
Expect(resultsStatus).To(HaveLen(2))
811809
r, ok = resultsStatus[0].Result.(*cniv04.Result)
812810
Expect(ok).To(BeTrue())
813811
Expect(reflect.DeepEqual(r, expectedResult1)).To(BeTrue())
@@ -897,16 +895,16 @@ var _ = Describe("ocicni operations", func() {
897895
podNet.Networks = []NetAttachment{}
898896
tmp, ok := ocicni.(*cniNetworkPlugin)
899897
Expect(ok).To(BeTrue())
900-
Expect(len(tmp.pods)).To(Equal(0))
898+
Expect(tmp.pods).To(BeEmpty())
901899
tmp.podLock(&podNet)
902-
Expect(len(tmp.pods)).To(Equal(1))
900+
Expect(tmp.pods).To(HaveLen(1))
903901
})
904902
It("verifies that network operations can be unlocked for a pod using cached networks", func() {
905903
podNet.Networks = []NetAttachment{}
906904
tmp, ok := ocicni.(*cniNetworkPlugin)
907905
Expect(ok).To(BeTrue())
908906
tmp.podUnlock(&podNet)
909-
Expect(len(tmp.pods)).To(Equal(0))
907+
Expect(tmp.pods).To(BeEmpty())
910908
})
911909
})
912910

pkg/ocicni/types.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
)
88

99
const (
10-
// DefaultInterfaceName is the string to be used for the interface name inside the net namespace
10+
// DefaultInterfaceName is the string to be used for the interface name inside the net namespace.
1111
DefaultInterfaceName = "eth0"
12-
// CNIPluginName is the default name of the plugin
12+
// CNIPluginName is the default name of the plugin.
1313
CNIPluginName = "cni"
1414
)
1515

@@ -106,7 +106,7 @@ type PodNetwork struct {
106106
Aliases map[string][]string
107107
}
108108

109-
// NetAttachment describes a container network attachment
109+
// NetAttachment describes a container network attachment.
110110
type NetAttachment struct {
111111
// NetName contains the name of the CNI network to which the container
112112
// should be or is attached
@@ -115,7 +115,7 @@ type NetAttachment struct {
115115
Ifname string
116116
}
117117

118-
// NetResult contains the result the network attachment operation
118+
// NetResult contains the result the network attachment operation.
119119
type NetResult struct {
120120
// Result is the CNI Result
121121
Result types.Result
@@ -124,7 +124,7 @@ type NetResult struct {
124124
NetAttachment
125125
}
126126

127-
// CNIPlugin is the interface that needs to be implemented by a plugin
127+
// CNIPlugin is the interface that needs to be implemented by a plugin.
128128
type CNIPlugin interface {
129129
// Name returns the plugin's name. This will be used when searching
130130
// for a plugin by name, e.g.

0 commit comments

Comments
 (0)