Skip to content

Commit

Permalink
Merge pull request #883 from mmorel-35/linter-2
Browse files Browse the repository at this point in the history
enable govet and unparam linters
  • Loading branch information
squeed authored Apr 24, 2023
2 parents 00b82fb + 10ddd9e commit 65fe256
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 66 deletions.
6 changes: 4 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ issues:
text: " and that stutters;"

linters:
disable:
- errcheck
enable:
- contextcheck
- durationcheck
Expand All @@ -16,17 +18,17 @@ linters:
- gocritic
- gofumpt
- gosimple
- govet
- ineffassign
- misspell
- nonamedreturns
- predeclared
- revive
- staticcheck
- unconvert
- unparam
- unused
- wastedassign
disable:
- errcheck

linters-settings:
gci:
Expand Down
11 changes: 4 additions & 7 deletions integration/integration_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,12 @@ var _ = Describe("Basic PTP using cnitool", func() {
Expect(basicBridgeIP).To(ContainSubstring("10.11.2."))

var chainedBridgeBandwidthPort, basicBridgePort int
var err error

By(fmt.Sprintf("starting echo server in %s\n\n", contNS1.ShortName()))
chainedBridgeBandwidthPort, chainedBridgeBandwidthSession, err = startEchoServerInNamespace(contNS1)
Expect(err).ToNot(HaveOccurred())
chainedBridgeBandwidthPort, chainedBridgeBandwidthSession = startEchoServerInNamespace(contNS1)

By(fmt.Sprintf("starting echo server in %s\n\n", contNS2.ShortName()))
basicBridgePort, basicBridgeSession, err = startEchoServerInNamespace(contNS2)
Expect(err).ToNot(HaveOccurred())
basicBridgePort, basicBridgeSession = startEchoServerInNamespace(contNS2)

packetInBytes := 20000 // The shaper needs to 'warm'. Send enough to cause it to throttle,
// balanced by run time.
Expand Down Expand Up @@ -242,7 +239,7 @@ func makeTCPClientInNS(netns string, address string, port int, numBytes int) {
Expect(string(out)).To(Equal(message))
}

func startEchoServerInNamespace(netNS Namespace) (int, *gexec.Session, error) {
func startEchoServerInNamespace(netNS Namespace) (int, *gexec.Session) {
session, err := startInNetNS(echoServerBinaryPath, netNS)
Expect(err).NotTo(HaveOccurred())

Expand All @@ -259,7 +256,7 @@ func startEchoServerInNamespace(netNS Namespace) (int, *gexec.Session, error) {
io.Copy(GinkgoWriter, io.MultiReader(session.Out, session.Err))
}()

return port, session, nil
return port, session
}

func startInNetNS(binPath string, namespace Namespace) (*gexec.Session, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ip/link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func makeVeth(name, vethPeerName string, mtu int, mac string, hostNS ns.NetNS) (
veth, err = makeVethPair(name, peerName, mtu, mac, hostNS)
switch {
case err == nil:
return peerName, veth, err
return peerName, veth, nil

case os.IsExist(err):
if peerExists(peerName) && vethPeerName == "" {
Expand Down
24 changes: 12 additions & 12 deletions plugins/main/host-device/host-device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ func canonicalizeIP(ip *net.IP) error {
// LoadIPAMConfig creates IPAMConfig using json encoded configuration provided
// as `bytes`. At the moment values provided in envArgs are ignored so there
// is no possibility to overload the json configuration using envArgs
func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, string, error) {
func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, error) {
n := Net{}
if err := json.Unmarshal(bytes, &n); err != nil {
return nil, "", err
return nil, err
}

if n.IPAM == nil {
return nil, "", fmt.Errorf("IPAM config missing 'ipam' key")
return nil, fmt.Errorf("IPAM config missing 'ipam' key")
}

// Validate all ranges
Expand All @@ -103,13 +103,13 @@ func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, string, error) {
for i := range n.IPAM.Addresses {
ip, addr, err := net.ParseCIDR(n.IPAM.Addresses[i].AddressStr)
if err != nil {
return nil, "", fmt.Errorf("invalid CIDR %s: %s", n.IPAM.Addresses[i].AddressStr, err)
return nil, fmt.Errorf("invalid CIDR %s: %s", n.IPAM.Addresses[i].AddressStr, err)
}
n.IPAM.Addresses[i].Address = *addr
n.IPAM.Addresses[i].Address.IP = ip

if err := canonicalizeIP(&n.IPAM.Addresses[i].Address.IP); err != nil {
return nil, "", fmt.Errorf("invalid address %d: %s", i, err)
return nil, fmt.Errorf("invalid address %d: %s", i, err)
}

if n.IPAM.Addresses[i].Address.IP.To4() != nil {
Expand All @@ -123,7 +123,7 @@ func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, string, error) {
e := IPAMEnvArgs{}
err := types.LoadArgs(envArgs, &e)
if err != nil {
return nil, "", err
return nil, err
}

if e.IP != "" {
Expand All @@ -132,7 +132,7 @@ func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, string, error) {

ip, subnet, err := net.ParseCIDR(ipstr)
if err != nil {
return nil, "", fmt.Errorf("invalid CIDR %s: %s", ipstr, err)
return nil, fmt.Errorf("invalid CIDR %s: %s", ipstr, err)
}

addr := Address{Address: net.IPNet{IP: ip, Mask: subnet.Mask}}
Expand All @@ -149,7 +149,7 @@ func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, string, error) {
for _, item := range strings.Split(string(e.GATEWAY), ",") {
gwip := net.ParseIP(strings.TrimSpace(item))
if gwip == nil {
return nil, "", fmt.Errorf("invalid gateway address: %s", item)
return nil, fmt.Errorf("invalid gateway address: %s", item)
}

for i := range n.IPAM.Addresses {
Expand All @@ -164,14 +164,14 @@ func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, string, error) {
// CNI spec 0.2.0 and below supported only one v4 and v6 address
if numV4 > 1 || numV6 > 1 {
if ok, _ := version.GreaterThanOrEqualTo(n.CNIVersion, "0.3.0"); !ok {
return nil, "", fmt.Errorf("CNI version %v does not support more than 1 address per family", n.CNIVersion)
return nil, fmt.Errorf("CNI version %v does not support more than 1 address per family", n.CNIVersion)
}
}

// Copy net name into IPAM so not to drag Net struct around
n.IPAM.Name = n.Name

return n.IPAM, n.CNIVersion, nil
return n.IPAM, nil
}

func buildOneConfig(name, cniVersion string, orig *Net, prevResult types.Result) (*Net, error) {
Expand Down Expand Up @@ -868,7 +868,7 @@ var _ = Describe("base functionality", func() {
err = json.Unmarshal([]byte(conf), &n)
Expect(err).NotTo(HaveOccurred())

n.IPAM, _, err = LoadIPAMConfig([]byte(conf), "")
n.IPAM, err = LoadIPAMConfig([]byte(conf), "")
Expect(err).NotTo(HaveOccurred())

if testutils.SpecVersionHasCHECK(ver) {
Expand Down Expand Up @@ -984,7 +984,7 @@ var _ = Describe("base functionality", func() {
err = json.Unmarshal([]byte(conf), &n)
Expect(err).NotTo(HaveOccurred())

n.IPAM, _, err = LoadIPAMConfig([]byte(conf), "")
n.IPAM, err = LoadIPAMConfig([]byte(conf), "")
Expect(err).NotTo(HaveOccurred())

if testutils.SpecVersionHasCHECK(ver) {
Expand Down
22 changes: 10 additions & 12 deletions plugins/meta/bandwidth/bandwidth_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"github.com/containernetworking/plugins/pkg/testutils"
)

func buildOneConfig(name, cniVersion string, orig *PluginConf, prevResult types.Result) (*PluginConf, []byte, error) {
func buildOneConfig(name, cniVersion string, orig *PluginConf, prevResult types.Result) ([]byte, error) {
var err error

inject := map[string]interface{}{
Expand All @@ -54,12 +54,12 @@ func buildOneConfig(name, cniVersion string, orig *PluginConf, prevResult types.

confBytes, err := json.Marshal(orig)
if err != nil {
return nil, nil, err
return nil, err
}

err = json.Unmarshal(confBytes, &config)
if err != nil {
return nil, nil, fmt.Errorf("unmarshal existing network bytes: %s", err)
return nil, fmt.Errorf("unmarshal existing network bytes: %s", err)
}

for key, value := range inject {
Expand All @@ -68,15 +68,15 @@ func buildOneConfig(name, cniVersion string, orig *PluginConf, prevResult types.

newBytes, err := json.Marshal(config)
if err != nil {
return nil, nil, err
return nil, err
}

conf := &PluginConf{}
if err := json.Unmarshal(newBytes, &conf); err != nil {
return nil, nil, fmt.Errorf("error parsing configuration: %s", err)
return nil, fmt.Errorf("error parsing configuration: %s", err)
}

return conf, newBytes, nil
return newBytes, nil
}

var _ = Describe("bandwidth test", func() {
Expand Down Expand Up @@ -950,7 +950,7 @@ var _ = Describe("bandwidth test", func() {
EgressRate: rateInBits,
}
tbfPluginConf.Type = "bandwidth"
_, newConfBytes, err := buildOneConfig("myBWnet", ver, tbfPluginConf, containerWithTbfResult)
newConfBytes, err := buildOneConfig("myBWnet", ver, tbfPluginConf, containerWithTbfResult)
Expect(err).NotTo(HaveOccurred())

args := &skel.CmdArgs{
Expand All @@ -977,7 +977,7 @@ var _ = Describe("bandwidth test", func() {
}
checkConf.Type = "bandwidth"

_, newCheckBytes, err := buildOneConfig("myBWnet", ver, checkConf, result)
newCheckBytes, err := buildOneConfig("myBWnet", ver, checkConf, result)
Expect(err).NotTo(HaveOccurred())

args = &skel.CmdArgs{
Expand All @@ -995,10 +995,8 @@ var _ = Describe("bandwidth test", func() {
})).To(Succeed())

By("starting a tcp server on both containers")
portServerWithTbf, echoServerWithTbf, err = startEchoServerInNamespace(containerWithTbfNS)
Expect(err).NotTo(HaveOccurred())
portServerWithoutTbf, echoServerWithoutTbf, err = startEchoServerInNamespace(containerWithoutTbfNS)
Expect(err).NotTo(HaveOccurred())
portServerWithTbf, echoServerWithTbf = startEchoServerInNamespace(containerWithTbfNS)
portServerWithoutTbf, echoServerWithoutTbf = startEchoServerInNamespace(containerWithoutTbfNS)
})

AfterEach(func() {
Expand Down
4 changes: 2 additions & 2 deletions plugins/meta/bandwidth/bandwidth_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func startInNetNS(binPath string, netNS ns.NetNS) (*gexec.Session, error) {
return session, err
}

func startEchoServerInNamespace(netNS ns.NetNS) (int, *gexec.Session, error) {
func startEchoServerInNamespace(netNS ns.NetNS) (int, *gexec.Session) {
session, err := startInNetNS(echoServerBinaryPath, netNS)
Expect(err).NotTo(HaveOccurred())

Expand All @@ -83,7 +83,7 @@ func startEchoServerInNamespace(netNS ns.NetNS) (int, *gexec.Session, error) {
io.Copy(GinkgoWriter, io.MultiReader(session.Out, session.Err))
}()

return port, session, nil
return port, session
}

func makeTCPClientInNS(netns string, address string, port int, numBytes int) {
Expand Down
17 changes: 10 additions & 7 deletions plugins/meta/firewall/firewall_firewalld_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,21 @@ func (f *fakeFirewalld) clear() {
f.source = ""
}

//nolint:unparam
func (f *fakeFirewalld) AddSource(zone, source string) (string, *dbus.Error) {
f.zone = zone
f.source = source
return "", nil
}

//nolint:unparam
func (f *fakeFirewalld) RemoveSource(zone, source string) (string, *dbus.Error) {
f.zone = zone
f.source = source
return "", nil
}

//nolint:unparam
func (f *fakeFirewalld) QuerySource(zone, source string) (bool, *dbus.Error) {
if f.zone != zone {
return false, nil
Expand Down Expand Up @@ -101,7 +104,7 @@ func spawnSessionDbus(wg *sync.WaitGroup) (string, *exec.Cmd) {
return busAddr, cmd
}

func makeFirewalldConf(ver, ifname string, ns ns.NetNS) []byte {
func makeFirewalldConf(ver string, ns ns.NetNS) []byte {
return []byte(fmt.Sprintf(`{
"cniVersion": "%s",
"name": "firewalld-test",
Expand All @@ -111,7 +114,7 @@ func makeFirewalldConf(ver, ifname string, ns ns.NetNS) []byte {
"prevResult": {
"cniVersion": "%s",
"interfaces": [
{"name": "%s", "sandbox": "%s"}
{"name": "eth0", "sandbox": "%s"}
],
"ips": [
{
Expand All @@ -122,7 +125,7 @@ func makeFirewalldConf(ver, ifname string, ns ns.NetNS) []byte {
}
]
}
}`, ver, ver, ifname, ns.Path()))
}`, ver, ver, ns.Path()))
}

var _ = Describe("firewalld test", func() {
Expand Down Expand Up @@ -192,7 +195,7 @@ var _ = Describe("firewalld test", func() {
It(fmt.Sprintf("[%s] works with a config", ver), func() {
Expect(isFirewalldRunning()).To(BeTrue())

conf := makeFirewalldConf(ver, ifname, targetNs)
conf := makeFirewalldConf(ver, targetNs)
args := &skel.CmdArgs{
ContainerID: "dummy",
Netns: targetNs.Path(),
Expand All @@ -218,7 +221,7 @@ var _ = Describe("firewalld test", func() {
It(fmt.Sprintf("[%s] defaults to the firewalld backend", ver), func() {
Expect(isFirewalldRunning()).To(BeTrue())

conf := makeFirewalldConf(ver, ifname, targetNs)
conf := makeFirewalldConf(ver, targetNs)
args := &skel.CmdArgs{
ContainerID: "dummy",
Netns: targetNs.Path(),
Expand All @@ -236,7 +239,7 @@ var _ = Describe("firewalld test", func() {
It(fmt.Sprintf("[%s] passes through the prevResult", ver), func() {
Expect(isFirewalldRunning()).To(BeTrue())

conf := makeFirewalldConf(ver, ifname, targetNs)
conf := makeFirewalldConf(ver, targetNs)
args := &skel.CmdArgs{
ContainerID: "dummy",
Netns: targetNs.Path(),
Expand All @@ -260,7 +263,7 @@ var _ = Describe("firewalld test", func() {
It(fmt.Sprintf("[%s] works with Check", ver), func() {
Expect(isFirewalldRunning()).To(BeTrue())

conf := makeFirewalldConf(ver, ifname, targetNs)
conf := makeFirewalldConf(ver, targetNs)
args := &skel.CmdArgs{
ContainerID: "dummy",
Netns: targetNs.Path(),
Expand Down
5 changes: 1 addition & 4 deletions plugins/meta/firewall/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,16 @@ func (ib *iptablesBackend) addRules(_ *FirewallNetConf, result *current.Result,
return nil
}

func (ib *iptablesBackend) delRules(_ *FirewallNetConf, result *current.Result, ipt *iptables.IPTables, proto iptables.Protocol) error {
func (ib *iptablesBackend) delRules(_ *FirewallNetConf, result *current.Result, ipt *iptables.IPTables, proto iptables.Protocol) {
rules := make([][]string, 0)
for _, ip := range result.IPs {
if protoForIP(ip.Address) == proto {
rules = append(rules, getPrivChainRules(ipString(ip.Address))...)
}
}

if len(rules) > 0 {
cleanupRules(ipt, ib.privChainName, rules)
}

return nil
}

func (ib *iptablesBackend) checkRules(_ *FirewallNetConf, result *current.Result, ipt *iptables.IPTables, proto iptables.Protocol) error {
Expand Down
6 changes: 2 additions & 4 deletions plugins/meta/portmap/portmap_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ var _ = Describe("portmap integration tests", func() {
fmt.Fprintln(GinkgoWriter, "namespace:", targetNS.Path())

// Start an echo server and get the port
containerPort, session, err = StartEchoServerInNamespace(targetNS)
Expect(err).NotTo(HaveOccurred())
containerPort, session = StartEchoServerInNamespace(targetNS)
})

AfterEach(func() {
Expand Down Expand Up @@ -329,8 +328,7 @@ var _ = Describe("portmap integration tests", func() {
fmt.Fprintln(GinkgoWriter, "namespace:", targetNS2.Path())

// Start an echo server and get the port
containerPort, session2, err := StartEchoServerInNamespace(targetNS2)
Expect(err).NotTo(HaveOccurred())
containerPort, session2 := StartEchoServerInNamespace(targetNS2)

runtimeConfig2 := libcni.RuntimeConf{
ContainerID: fmt.Sprintf("unit-test2-%d", hostPort),
Expand Down
Loading

0 comments on commit 65fe256

Please sign in to comment.