Skip to content

Commit

Permalink
fix lint errors
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Dec 17, 2021
1 parent 707829f commit 5882b63
Show file tree
Hide file tree
Showing 23 changed files with 107 additions and 97 deletions.
8 changes: 8 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,11 @@ linters-settings:
- unnecessaryBlock
gocyclo:
min-complexity: 35

issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- dupl
12 changes: 6 additions & 6 deletions libnetwork/cni/cni_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func createNetworkFromCNIConfigList(conf *libcni.NetworkConfigList, confPath str
network.Options["vlan"] = strconv.Itoa(bridge.Vlan)
}

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

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

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

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

case types.MacVLANNetworkDriver:
plugins = append(plugins, newVLANPlugin(types.MacVLANNetworkDriver, network.NetworkInterface, vlanPluginMode, mtu, ipamConf))
plugins = append(plugins, newVLANPlugin(types.MacVLANNetworkDriver, network.NetworkInterface, vlanPluginMode, mtu, &ipamConf))

case types.IPVLANNetworkDriver:
plugins = append(plugins, newVLANPlugin(types.IPVLANNetworkDriver, network.NetworkInterface, vlanPluginMode, mtu, ipamConf))
plugins = append(plugins, newVLANPlugin(types.IPVLANNetworkDriver, network.NetworkInterface, vlanPluginMode, mtu, &ipamConf))

default:
return nil, "", errors.Errorf("driver %q is not supported by cni", network.Driver)
Expand Down
2 changes: 1 addition & 1 deletion libnetwork/cni/cni_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (e *cniExec) ExecPlugin(ctx context.Context, pluginPath string, stdinData [
}

// annotatePluginError parses the common cni plugin error json.
func annotatePluginError(err error, plugin string, stdout []byte, stderr []byte) error {
func annotatePluginError(err error, plugin string, stdout, stderr []byte) error {
pluginName := filepath.Base(plugin)
emsg := cniPluginError{
plugin: pluginName,
Expand Down
5 changes: 2 additions & 3 deletions libnetwork/cni/cni_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ func TestCni(t *testing.T) {
RunSpecs(t, "CNI Suite")
}

func getNetworkInterface(cniConfDir string, machine bool) (types.ContainerNetwork, error) {
return cni.NewCNINetworkInterface(cni.InitConfig{
func getNetworkInterface(cniConfDir string) (types.ContainerNetwork, error) {
return cni.NewCNINetworkInterface(&cni.InitConfig{
CNIConfigDir: cniConfDir,
CNIPluginDirs: cniPluginDirs,
IsMachine: machine,
LockFile: filepath.Join(cniConfDir, "cni.lock"),
})
}
Expand Down
8 changes: 4 additions & 4 deletions libnetwork/cni/cni_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func newNcList(name, version string, labels, options map[string]string) ncList {
}

// newHostLocalBridge creates a new LocalBridge for host-local
func newHostLocalBridge(name string, isGateWay, ipMasq bool, mtu int, vlan int, ipamConf ipamConfig) *hostLocalBridge {
func newHostLocalBridge(name string, isGateWay, ipMasq bool, mtu, vlan int, ipamConf *ipamConfig) *hostLocalBridge {
caps := make(map[string]bool)
caps["ips"] = true
bridge := hostLocalBridge{
Expand All @@ -144,7 +144,7 @@ func newHostLocalBridge(name string, isGateWay, ipMasq bool, mtu int, vlan int,
MTU: mtu,
HairpinMode: true,
Vlan: vlan,
IPAM: ipamConf,
IPAM: *ipamConf,
}
// if we use host-local set the ips cap to ensure we can set static ips via runtime config
if ipamConf.PluginType == types.HostLocalIPAMDriver {
Expand Down Expand Up @@ -255,10 +255,10 @@ func hasDNSNamePlugin(paths []string) bool {
}

// newVLANPlugin creates a macvlanconfig with a given device name
func newVLANPlugin(pluginType, device, mode string, mtu int, ipam ipamConfig) VLANConfig {
func newVLANPlugin(pluginType, device, mode string, mtu int, ipam *ipamConfig) VLANConfig {
m := VLANConfig{
PluginType: pluginType,
IPAM: ipam,
IPAM: *ipam,
}
if mtu > 0 {
m.MTU = mtu
Expand Down
17 changes: 9 additions & 8 deletions libnetwork/cni/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import (

// NetworkCreate will take a partial filled Network and fill the
// missing fields. It creates the Network and returns the full Network.
// nolint:gocritic
func (n *cniNetwork) NetworkCreate(net types.Network) (types.Network, error) {
n.lock.Lock()
defer n.lock.Unlock()
err := n.loadNetworks()
if err != nil {
return types.Network{}, err
}
network, err := n.networkCreate(net, false)
network, err := n.networkCreate(&net, false)
if err != nil {
return types.Network{}, err
}
Expand All @@ -34,7 +35,7 @@ func (n *cniNetwork) NetworkCreate(net types.Network) (types.Network, error) {

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

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

switch newNetwork.Driver {
case types.BridgeNetworkDriver:
err = internalutil.CreateBridge(n, &newNetwork, usedNetworks)
err = internalutil.CreateBridge(n, newNetwork, usedNetworks)
if err != nil {
return nil, err
}
case types.MacVLANNetworkDriver, types.IPVLANNetworkDriver:
err = createIPMACVLAN(&newNetwork)
err = createIPMACVLAN(newNetwork)
if err != nil {
return nil, err
}
default:
return nil, errors.Wrapf(types.ErrInvalidArg, "unsupported driver %s", newNetwork.Driver)
}

err = internalutil.ValidateSubnets(&newNetwork, usedNetworks)
err = internalutil.ValidateSubnets(newNetwork, usedNetworks)
if err != nil {
return nil, err
}
Expand All @@ -95,11 +96,11 @@ func (n *cniNetwork) networkCreate(newNetwork types.Network, defaultNet bool) (*
newNetwork.DNSEnabled = false
}

cniConf, path, err := n.createCNIConfigListFromNetwork(&newNetwork, !defaultNet)
cniConf, path, err := n.createCNIConfigListFromNetwork(newNetwork, !defaultNet)
if err != nil {
return nil, err
}
return &network{cniNet: cniConf, libpodNet: &newNetwork, filename: path}, nil
return &network{cniNet: cniConf, libpodNet: newNetwork, filename: path}, nil
}

// NetworkRemove will remove the Network with the given name or ID.
Expand Down
17 changes: 8 additions & 9 deletions libnetwork/cni/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (
"path/filepath"
"time"

"github.com/containers/common/libnetwork/types"
"github.com/containers/common/libnetwork/util"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
gomegaTypes "github.com/onsi/gomega/types"
"github.com/sirupsen/logrus"

"github.com/containers/common/libnetwork/types"
"github.com/containers/common/libnetwork/util"
)

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

JustBeforeEach(func() {
var err error
libpodNet, err = getNetworkInterface(cniConfDir, false)
libpodNet, err = getNetworkInterface(cniConfDir)
if err != nil {
Fail("Failed to create NewCNINetworkInterface")
}
Expand Down Expand Up @@ -111,7 +110,7 @@ var _ = Describe("Config", func() {
Expect(network2).To(Equal(network1))

// create a new interface to force a config load from disk
libpodNet, err = getNetworkInterface(cniConfDir, false)
libpodNet, err = getNetworkInterface(cniConfDir)
Expect(err).To(BeNil())

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

// reload configs from disk
libpodNet, err = getNetworkInterface(cniConfDir, false)
libpodNet, err = getNetworkInterface(cniConfDir)
Expect(err).To(BeNil())

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

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

// create a new interface to force a config load from disk
libpodNet, err = getNetworkInterface(cniConfDir, false)
libpodNet, err = getNetworkInterface(cniConfDir)
Expect(err).To(BeNil())

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

})

func grepInFile(path string, match string) {
func grepInFile(path, match string) {
data, err := ioutil.ReadFile(path)
ExpectWithOffset(1, err).To(BeNil())
ExpectWithOffset(1, string(data)).To(ContainSubstring(match))
Expand Down
4 changes: 2 additions & 2 deletions libnetwork/cni/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type InitConfig struct {

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

// getNetwork will lookup a network by name or ID. It returns an
Expand Down
8 changes: 5 additions & 3 deletions libnetwork/cni/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ func (n *cniNetwork) Setup(namespacePath string, options types.SetupOptions) (ma

results := make(map[string]types.StatusBlock, len(options.Networks))
for name, netOpts := range options.Networks {
netOpts := netOpts
network := n.networks[name]
rt := getRuntimeConfig(namespacePath, options.ContainerName, options.ContainerID, name, ports, netOpts)
rt := getRuntimeConfig(namespacePath, options.ContainerName, options.ContainerID, name, ports, &netOpts)

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

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

var multiErr *multierror.Error
for name, netOpts := range options.Networks {
rt := getRuntimeConfig(namespacePath, options.ContainerName, options.ContainerID, name, ports, netOpts)
netOpts := netOpts
rt := getRuntimeConfig(namespacePath, options.ContainerName, options.ContainerID, name, ports, &netOpts)

cniConfList, newRt, err := getCachedNetworkConfig(n.cniConf, name, rt)
if err == nil {
Expand Down
15 changes: 7 additions & 8 deletions libnetwork/cni/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ import (
"time"

"github.com/containernetworking/plugins/pkg/ns"
"github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/netns"
"github.com/containers/storage/pkg/stringid"
"github.com/containers/storage/pkg/unshare"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"

"github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/netns"
"github.com/containers/storage/pkg/stringid"
"github.com/containers/storage/pkg/unshare"
)

var _ = Describe("run CNI", func() {
Expand Down Expand Up @@ -98,7 +97,7 @@ var _ = Describe("run CNI", func() {

JustBeforeEach(func() {
var err error
libpodNet, err = getNetworkInterface(cniConfDir, false)
libpodNet, err = getNetworkInterface(cniConfDir)
if err != nil {
Fail("Failed to create NewCNINetworkInterface")
}
Expand Down Expand Up @@ -141,7 +140,7 @@ var _ = Describe("run CNI", func() {
Expect(res[defNet].DNSSearchDomains).To(BeEmpty())

// reload the interface so the networks are reload from disk
libpodNet, err := getNetworkInterface(cniConfDir, false)
libpodNet, err := getNetworkInterface(cniConfDir)
Expect(err).To(BeNil())

err = libpodNet.Teardown(netNSContainer.Path(), types.TeardownOptions(setupOpts))
Expand Down Expand Up @@ -398,7 +397,7 @@ var _ = Describe("run CNI", func() {
i, err := net.InterfaceByName(intName1)
Expect(err).To(BeNil())
Expect(i.Name).To(Equal(intName1))
Expect(i.HardwareAddr).To(Equal((net.HardwareAddr)(macInt1)))
Expect(i.HardwareAddr).To(Equal(net.HardwareAddr(macInt1)))
addrs, err := i.Addrs()
Expect(err).To(BeNil())
subnet := &net.IPNet{
Expand Down
2 changes: 1 addition & 1 deletion libnetwork/internal/util/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func getRandomIPv6Subnet() (net.IPNet, error) {
// read 8 random bytes
_, err := rand.Read(ip)
if err != nil {
return net.IPNet{}, nil
return net.IPNet{}, err
}
// first byte must be FD as per RFC3879
ip[0] = 0xfd
Expand Down
Loading

0 comments on commit 5882b63

Please sign in to comment.