Skip to content

Commit 39a8401

Browse files
committed
libnetwork: fix lint errors
Signed-off-by: Paul Holzinger <[email protected]>
1 parent 707829f commit 39a8401

23 files changed

+107
-97
lines changed

.golangci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,11 @@ linters-settings:
116116
- unnecessaryBlock
117117
gocyclo:
118118
min-complexity: 35
119+
120+
issues:
121+
# Excluding configuration per-path, per-linter, per-text and per-source
122+
exclude-rules:
123+
# Exclude some linters from running on tests files.
124+
- path: _test\.go
125+
linters:
126+
- dupl

libnetwork/cni/cni_conversion.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func createNetworkFromCNIConfigList(conf *libcni.NetworkConfigList, confPath str
7676
network.Options["vlan"] = strconv.Itoa(bridge.Vlan)
7777
}
7878

79-
err = convertIPAMConfToNetwork(&network, bridge.IPAM, confPath)
79+
err = convertIPAMConfToNetwork(&network, &bridge.IPAM, confPath)
8080
if err != nil {
8181
return nil, err
8282
}
@@ -98,7 +98,7 @@ func createNetworkFromCNIConfigList(conf *libcni.NetworkConfigList, confPath str
9898
network.Options["mode"] = vlan.Mode
9999
}
100100

101-
err = convertIPAMConfToNetwork(&network, vlan.IPAM, confPath)
101+
err = convertIPAMConfToNetwork(&network, &vlan.IPAM, confPath)
102102
if err != nil {
103103
return nil, err
104104
}
@@ -126,7 +126,7 @@ func findPluginByName(plugins []*libcni.NetworkConfig, name string) bool {
126126

127127
// convertIPAMConfToNetwork converts A cni IPAMConfig to libpod network subnets.
128128
// It returns an array of subnets and an extra bool if dhcp is configured.
129-
func convertIPAMConfToNetwork(network *types.Network, ipam ipamConfig, confPath string) error {
129+
func convertIPAMConfToNetwork(network *types.Network, ipam *ipamConfig, confPath string) error {
130130
if ipam.PluginType == types.DHCPIPAMDriver {
131131
network.IPAMOptions["driver"] = types.DHCPIPAMDriver
132132
return nil
@@ -288,7 +288,7 @@ func (n *cniNetwork) createCNIConfigListFromNetwork(network *types.Network, writ
288288

289289
switch network.Driver {
290290
case types.BridgeNetworkDriver:
291-
bridge := newHostLocalBridge(network.NetworkInterface, isGateway, ipMasq, mtu, vlan, ipamConf)
291+
bridge := newHostLocalBridge(network.NetworkInterface, isGateway, ipMasq, mtu, vlan, &ipamConf)
292292
plugins = append(plugins, bridge, newPortMapPlugin(), newFirewallPlugin(), newTuningPlugin())
293293
// if we find the dnsname plugin we add configuration for it
294294
if hasDNSNamePlugin(n.cniPluginDirs) && network.DNSEnabled {
@@ -297,10 +297,10 @@ func (n *cniNetwork) createCNIConfigListFromNetwork(network *types.Network, writ
297297
}
298298

299299
case types.MacVLANNetworkDriver:
300-
plugins = append(plugins, newVLANPlugin(types.MacVLANNetworkDriver, network.NetworkInterface, vlanPluginMode, mtu, ipamConf))
300+
plugins = append(plugins, newVLANPlugin(types.MacVLANNetworkDriver, network.NetworkInterface, vlanPluginMode, mtu, &ipamConf))
301301

302302
case types.IPVLANNetworkDriver:
303-
plugins = append(plugins, newVLANPlugin(types.IPVLANNetworkDriver, network.NetworkInterface, vlanPluginMode, mtu, ipamConf))
303+
plugins = append(plugins, newVLANPlugin(types.IPVLANNetworkDriver, network.NetworkInterface, vlanPluginMode, mtu, &ipamConf))
304304

305305
default:
306306
return nil, "", errors.Errorf("driver %q is not supported by cni", network.Driver)

libnetwork/cni/cni_exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (e *cniExec) ExecPlugin(ctx context.Context, pluginPath string, stdinData [
8787
}
8888

8989
// annotatePluginError parses the common cni plugin error json.
90-
func annotatePluginError(err error, plugin string, stdout []byte, stderr []byte) error {
90+
func annotatePluginError(err error, plugin string, stdout, stderr []byte) error {
9191
pluginName := filepath.Base(plugin)
9292
emsg := cniPluginError{
9393
plugin: pluginName,

libnetwork/cni/cni_suite_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ func TestCni(t *testing.T) {
2525
RunSpecs(t, "CNI Suite")
2626
}
2727

28-
func getNetworkInterface(cniConfDir string, machine bool) (types.ContainerNetwork, error) {
29-
return cni.NewCNINetworkInterface(cni.InitConfig{
28+
func getNetworkInterface(cniConfDir string) (types.ContainerNetwork, error) {
29+
return cni.NewCNINetworkInterface(&cni.InitConfig{
3030
CNIConfigDir: cniConfDir,
3131
CNIPluginDirs: cniPluginDirs,
32-
IsMachine: machine,
3332
LockFile: filepath.Join(cniConfDir, "cni.lock"),
3433
})
3534
}

libnetwork/cni/cni_types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func newNcList(name, version string, labels, options map[string]string) ncList {
133133
}
134134

135135
// newHostLocalBridge creates a new LocalBridge for host-local
136-
func newHostLocalBridge(name string, isGateWay, ipMasq bool, mtu int, vlan int, ipamConf ipamConfig) *hostLocalBridge {
136+
func newHostLocalBridge(name string, isGateWay, ipMasq bool, mtu, vlan int, ipamConf *ipamConfig) *hostLocalBridge {
137137
caps := make(map[string]bool)
138138
caps["ips"] = true
139139
bridge := hostLocalBridge{
@@ -144,7 +144,7 @@ func newHostLocalBridge(name string, isGateWay, ipMasq bool, mtu int, vlan int,
144144
MTU: mtu,
145145
HairpinMode: true,
146146
Vlan: vlan,
147-
IPAM: ipamConf,
147+
IPAM: *ipamConf,
148148
}
149149
// if we use host-local set the ips cap to ensure we can set static ips via runtime config
150150
if ipamConf.PluginType == types.HostLocalIPAMDriver {
@@ -255,10 +255,10 @@ func hasDNSNamePlugin(paths []string) bool {
255255
}
256256

257257
// newVLANPlugin creates a macvlanconfig with a given device name
258-
func newVLANPlugin(pluginType, device, mode string, mtu int, ipam ipamConfig) VLANConfig {
258+
func newVLANPlugin(pluginType, device, mode string, mtu int, ipam *ipamConfig) VLANConfig {
259259
m := VLANConfig{
260260
PluginType: pluginType,
261-
IPAM: ipam,
261+
IPAM: *ipam,
262262
}
263263
if mtu > 0 {
264264
m.MTU = mtu

libnetwork/cni/config.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ import (
1616

1717
// NetworkCreate will take a partial filled Network and fill the
1818
// missing fields. It creates the Network and returns the full Network.
19+
// nolint:gocritic
1920
func (n *cniNetwork) NetworkCreate(net types.Network) (types.Network, error) {
2021
n.lock.Lock()
2122
defer n.lock.Unlock()
2223
err := n.loadNetworks()
2324
if err != nil {
2425
return types.Network{}, err
2526
}
26-
network, err := n.networkCreate(net, false)
27+
network, err := n.networkCreate(&net, false)
2728
if err != nil {
2829
return types.Network{}, err
2930
}
@@ -34,7 +35,7 @@ func (n *cniNetwork) NetworkCreate(net types.Network) (types.Network, error) {
3435

3536
// networkCreate will fill out the given network struct and return the new network entry.
3637
// If defaultNet is true it will not validate against used subnets and it will not write the cni config to disk.
37-
func (n *cniNetwork) networkCreate(newNetwork types.Network, defaultNet bool) (*network, error) {
38+
func (n *cniNetwork) networkCreate(newNetwork *types.Network, defaultNet bool) (*network, error) {
3839
// if no driver is set use the default one
3940
if newNetwork.Driver == "" {
4041
newNetwork.Driver = types.DefaultNetworkDriver
@@ -46,7 +47,7 @@ func (n *cniNetwork) networkCreate(newNetwork types.Network, defaultNet bool) (*
4647
return nil, errors.Wrap(types.ErrInvalidArg, "ID can not be set for network create")
4748
}
4849

49-
err := internalutil.CommonNetworkCreate(n, &newNetwork)
50+
err := internalutil.CommonNetworkCreate(n, newNetwork)
5051
if err != nil {
5152
return nil, err
5253
}
@@ -68,20 +69,20 @@ func (n *cniNetwork) networkCreate(newNetwork types.Network, defaultNet bool) (*
6869

6970
switch newNetwork.Driver {
7071
case types.BridgeNetworkDriver:
71-
err = internalutil.CreateBridge(n, &newNetwork, usedNetworks)
72+
err = internalutil.CreateBridge(n, newNetwork, usedNetworks)
7273
if err != nil {
7374
return nil, err
7475
}
7576
case types.MacVLANNetworkDriver, types.IPVLANNetworkDriver:
76-
err = createIPMACVLAN(&newNetwork)
77+
err = createIPMACVLAN(newNetwork)
7778
if err != nil {
7879
return nil, err
7980
}
8081
default:
8182
return nil, errors.Wrapf(types.ErrInvalidArg, "unsupported driver %s", newNetwork.Driver)
8283
}
8384

84-
err = internalutil.ValidateSubnets(&newNetwork, usedNetworks)
85+
err = internalutil.ValidateSubnets(newNetwork, usedNetworks)
8586
if err != nil {
8687
return nil, err
8788
}
@@ -95,11 +96,11 @@ func (n *cniNetwork) networkCreate(newNetwork types.Network, defaultNet bool) (*
9596
newNetwork.DNSEnabled = false
9697
}
9798

98-
cniConf, path, err := n.createCNIConfigListFromNetwork(&newNetwork, !defaultNet)
99+
cniConf, path, err := n.createCNIConfigListFromNetwork(newNetwork, !defaultNet)
99100
if err != nil {
100101
return nil, err
101102
}
102-
return &network{cniNet: cniConf, libpodNet: &newNetwork, filename: path}, nil
103+
return &network{cniNet: cniConf, libpodNet: newNetwork, filename: path}, nil
103104
}
104105

105106
// NetworkRemove will remove the Network with the given name or ID.

libnetwork/cni/config_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ import (
1010
"path/filepath"
1111
"time"
1212

13+
"github.com/containers/common/libnetwork/types"
14+
"github.com/containers/common/libnetwork/util"
1315
. "github.com/onsi/ginkgo"
1416
. "github.com/onsi/gomega"
1517
gomegaTypes "github.com/onsi/gomega/types"
1618
"github.com/sirupsen/logrus"
17-
18-
"github.com/containers/common/libnetwork/types"
19-
"github.com/containers/common/libnetwork/util"
2019
)
2120

2221
var _ = Describe("Config", func() {
@@ -39,7 +38,7 @@ var _ = Describe("Config", func() {
3938

4039
JustBeforeEach(func() {
4140
var err error
42-
libpodNet, err = getNetworkInterface(cniConfDir, false)
41+
libpodNet, err = getNetworkInterface(cniConfDir)
4342
if err != nil {
4443
Fail("Failed to create NewCNINetworkInterface")
4544
}
@@ -111,7 +110,7 @@ var _ = Describe("Config", func() {
111110
Expect(network2).To(Equal(network1))
112111

113112
// create a new interface to force a config load from disk
114-
libpodNet, err = getNetworkInterface(cniConfDir, false)
113+
libpodNet, err = getNetworkInterface(cniConfDir)
115114
Expect(err).To(BeNil())
116115

117116
network2, err = libpodNet.NetworkInspect(network1.Name)
@@ -351,7 +350,7 @@ var _ = Describe("Config", func() {
351350
grepInFile(path, `"mode": "`+mode+`"`)
352351

353352
// reload configs from disk
354-
libpodNet, err = getNetworkInterface(cniConfDir, false)
353+
libpodNet, err = getNetworkInterface(cniConfDir)
355354
Expect(err).To(BeNil())
356355

357356
network2, err := libpodNet.NetworkInspect(network1.Name)
@@ -417,7 +416,7 @@ var _ = Describe("Config", func() {
417416
Expect(network1.Subnets[0].LeaseRange).To(BeNil())
418417

419418
// reload configs from disk
420-
libpodNet, err = getNetworkInterface(cniConfDir, false)
419+
libpodNet, err = getNetworkInterface(cniConfDir)
421420
Expect(err).To(BeNil())
422421
// check the the networks are identical
423422
network2, err := libpodNet.NetworkInspect(network1.Name)
@@ -667,7 +666,7 @@ var _ = Describe("Config", func() {
667666
Expect(network1.Subnets[0].LeaseRange.EndIP.String()).To(Equal(endIP))
668667

669668
// create a new interface to force a config load from disk
670-
libpodNet, err = getNetworkInterface(cniConfDir, false)
669+
libpodNet, err = getNetworkInterface(cniConfDir)
671670
Expect(err).To(BeNil())
672671

673672
network1, err = libpodNet.NetworkInspect(network1.Name)
@@ -1364,7 +1363,7 @@ var _ = Describe("Config", func() {
13641363

13651364
})
13661365

1367-
func grepInFile(path string, match string) {
1366+
func grepInFile(path, match string) {
13681367
data, err := ioutil.ReadFile(path)
13691368
ExpectWithOffset(1, err).To(BeNil())
13701369
ExpectWithOffset(1, string(data)).To(ContainSubstring(match))

libnetwork/cni/network.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type InitConfig struct {
7070

7171
// NewCNINetworkInterface creates the ContainerNetwork interface for the CNI backend.
7272
// Note: The networks are not loaded from disk until a method is called.
73-
func NewCNINetworkInterface(conf InitConfig) (types.ContainerNetwork, error) {
73+
func NewCNINetworkInterface(conf *InitConfig) (types.ContainerNetwork, error) {
7474
// TODO: consider using a shared memory lock
7575
lock, err := lockfile.GetLockfile(conf.LockFile)
7676
if err != nil {
@@ -203,7 +203,7 @@ func (n *cniNetwork) createDefaultNetwork() (*network, error) {
203203
{Subnet: n.defaultSubnet},
204204
},
205205
}
206-
return n.networkCreate(net, true)
206+
return n.networkCreate(&net, true)
207207
}
208208

209209
// getNetwork will lookup a network by name or ID. It returns an

libnetwork/cni/run.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ func (n *cniNetwork) Setup(namespacePath string, options types.SetupOptions) (ma
6969

7070
results := make(map[string]types.StatusBlock, len(options.Networks))
7171
for name, netOpts := range options.Networks {
72+
netOpts := netOpts
7273
network := n.networks[name]
73-
rt := getRuntimeConfig(namespacePath, options.ContainerName, options.ContainerID, name, ports, netOpts)
74+
rt := getRuntimeConfig(namespacePath, options.ContainerName, options.ContainerID, name, ports, &netOpts)
7475

7576
// If we have more than one static ip we need parse the ips via runtime config,
7677
// make sure to add the ips capability to the first plugin otherwise it doesn't get the ips
@@ -157,7 +158,7 @@ func CNIResultToStatus(res cnitypes.Result) (types.StatusBlock, error) {
157158
return result, nil
158159
}
159160

160-
func getRuntimeConfig(netns, conName, conID, networkName string, ports []cniPortMapEntry, opts types.PerNetworkOptions) *libcni.RuntimeConf {
161+
func getRuntimeConfig(netns, conName, conID, networkName string, ports []cniPortMapEntry, opts *types.PerNetworkOptions) *libcni.RuntimeConf {
161162
rt := &libcni.RuntimeConf{
162163
ContainerID: conID,
163164
NetNS: netns,
@@ -230,7 +231,8 @@ func (n *cniNetwork) teardown(namespacePath string, options types.TeardownOption
230231

231232
var multiErr *multierror.Error
232233
for name, netOpts := range options.Networks {
233-
rt := getRuntimeConfig(namespacePath, options.ContainerName, options.ContainerID, name, ports, netOpts)
234+
netOpts := netOpts
235+
rt := getRuntimeConfig(namespacePath, options.ContainerName, options.ContainerID, name, ports, &netOpts)
234236

235237
cniConfList, newRt, err := getCachedNetworkConfig(n.cniConf, name, rt)
236238
if err == nil {

libnetwork/cni/run_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@ import (
2424
"time"
2525

2626
"github.com/containernetworking/plugins/pkg/ns"
27+
"github.com/containers/common/libnetwork/types"
28+
"github.com/containers/common/pkg/netns"
29+
"github.com/containers/storage/pkg/stringid"
30+
"github.com/containers/storage/pkg/unshare"
2731
. "github.com/onsi/ginkgo"
2832
. "github.com/onsi/gomega"
2933
"github.com/sirupsen/logrus"
3034
"github.com/vishvananda/netlink"
3135
"golang.org/x/sys/unix"
32-
33-
"github.com/containers/common/libnetwork/types"
34-
"github.com/containers/common/pkg/netns"
35-
"github.com/containers/storage/pkg/stringid"
36-
"github.com/containers/storage/pkg/unshare"
3736
)
3837

3938
var _ = Describe("run CNI", func() {
@@ -98,7 +97,7 @@ var _ = Describe("run CNI", func() {
9897

9998
JustBeforeEach(func() {
10099
var err error
101-
libpodNet, err = getNetworkInterface(cniConfDir, false)
100+
libpodNet, err = getNetworkInterface(cniConfDir)
102101
if err != nil {
103102
Fail("Failed to create NewCNINetworkInterface")
104103
}
@@ -141,7 +140,7 @@ var _ = Describe("run CNI", func() {
141140
Expect(res[defNet].DNSSearchDomains).To(BeEmpty())
142141

143142
// reload the interface so the networks are reload from disk
144-
libpodNet, err := getNetworkInterface(cniConfDir, false)
143+
libpodNet, err := getNetworkInterface(cniConfDir)
145144
Expect(err).To(BeNil())
146145

147146
err = libpodNet.Teardown(netNSContainer.Path(), types.TeardownOptions(setupOpts))
@@ -398,7 +397,7 @@ var _ = Describe("run CNI", func() {
398397
i, err := net.InterfaceByName(intName1)
399398
Expect(err).To(BeNil())
400399
Expect(i.Name).To(Equal(intName1))
401-
Expect(i.HardwareAddr).To(Equal((net.HardwareAddr)(macInt1)))
400+
Expect(i.HardwareAddr).To(Equal(net.HardwareAddr(macInt1)))
402401
addrs, err := i.Addrs()
403402
Expect(err).To(BeNil())
404403
subnet := &net.IPNet{

0 commit comments

Comments
 (0)