Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit 9960eae

Browse files
committed
api updates
updates flagged by golangci-lint Signed-off-by: John O'Loughlin <[email protected]>
1 parent beba4d1 commit 9960eae

25 files changed

+216
-256
lines changed

Diff for: cniovs/cniovs.go

+17-19
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,17 @@ import (
4848
"github.com/intel/userspace-cni-network-plugin/pkg/types"
4949
)
5050

51-
//
5251
// Constants
53-
//
5452
const (
5553
defaultBridge = "br0"
5654
DefaultHostVhostuserBaseDir = "/var/lib/vhost_sockets/"
5755
)
5856

59-
//
6057
// Types
61-
//
6258
type CniOvs struct {
6359
}
6460

65-
//
6661
// API Functions
67-
//
6862
func (cniOvs CniOvs) AddOnHost(conf *types.NetConf,
6963
args *skel.CmdArgs,
7064
kubeClient kubernetes.Interface,
@@ -196,7 +190,11 @@ func (cniOvs CniOvs) DelFromHost(conf *types.NetConf, args *skel.CmdArgs, shared
196190
func (cniOvs CniOvs) DelFromContainer(conf *types.NetConf, args *skel.CmdArgs, sharedDir string, pod *v1.Pod) error {
197191
logging.Infof("OVS DelFromContainer: ENTER - Container %s Iface %s", args.ContainerID[:12], args.IfName)
198192

199-
configdata.FileCleanup(sharedDir, "")
193+
var err = configdata.FileCleanup(sharedDir, "")
194+
195+
if err != nil {
196+
logging.Debugf("DelFromContainer(ovs): %v", err)
197+
}
200198

201199
return nil
202200
}
@@ -243,15 +241,15 @@ func createSharedDir(sharedDir, oldSharedDir string) error {
243241
if os.IsNotExist(err) {
244242
err = os.MkdirAll(sharedDir, 0750)
245243
if err != nil {
246-
logging.Errorf("createSharedDir: Failed to create dir (%s): %v", sharedDir, err)
244+
_ = logging.Errorf("createSharedDir: Failed to create dir (%s): %v", sharedDir, err)
247245
return err
248246
}
249247

250248
if strings.Contains(sharedDir, DefaultHostVhostuserBaseDir) {
251249
logging.Debugf("createSharedDir: Mount from %s to %s", oldSharedDir, sharedDir)
252250
err = unix.Mount(oldSharedDir, sharedDir, "", unix.MS_BIND, "")
253251
if err != nil {
254-
logging.Errorf("createSharedDir: Failed to bind mount: %s", err)
252+
_ = logging.Errorf("createSharedDir: Failed to bind mount: %s", err)
255253
return err
256254
}
257255
}
@@ -296,15 +294,15 @@ func addLocalDeviceVhost(conf *types.NetConf, args *skel.CmdArgs, actualSharedDi
296294
sharedDir := getShortSharedDir(actualSharedDir)
297295
err = createSharedDir(sharedDir, actualSharedDir)
298296
if err != nil {
299-
logging.Errorf("addLocalDeviceVhost: Failed to create shared dir: %v", err)
297+
_ = logging.Errorf("addLocalDeviceVhost: Failed to create shared dir: %v", err)
300298
return err
301299
}
302300

303301
group := conf.HostConf.VhostConf.Group
304302
if group != "" {
305303
err = setSharedDirGroup(sharedDir, group)
306304
if err != nil {
307-
logging.Errorf("addLocalDeviceVhost: Failed to set shared dir group: %v", err)
305+
_ = logging.Errorf("addLocalDeviceVhost: Failed to set shared dir group: %v", err)
308306
return err
309307
}
310308
}
@@ -341,7 +339,7 @@ func delLocalDeviceVhost(conf *types.NetConf, args *skel.CmdArgs, actualSharedDi
341339
// ovs-vsctl --if-exists del-port
342340
err := deleteVhostPort(data.Vhostname, conf.HostConf.BridgeConf.BridgeName)
343341
if err != nil {
344-
logging.Errorf("delLocalDeviceVhost: Failed to delete port: %v", err)
342+
_ = logging.Errorf("delLocalDeviceVhost: Failed to delete port: %v", err)
345343
return err
346344
}
347345

@@ -350,17 +348,17 @@ func delLocalDeviceVhost(conf *types.NetConf, args *skel.CmdArgs, actualSharedDi
350348
logging.Debugf("delLocalDeviceVhost: Unmount shared directory: %v", sharedDir)
351349
_, err = os.Stat(sharedDir)
352350
if os.IsNotExist(err) {
353-
logging.Errorf("delLocalDeviceVhost: shared directory %s does not exist to unmount", sharedDir)
351+
_ = logging.Errorf("delLocalDeviceVhost: shared directory %s does not exist to unmount", sharedDir)
354352
return nil
355353
}
356354
err = unix.Unmount(sharedDir, 0)
357355
if err != nil {
358-
logging.Errorf("Failed to unmount dir: %v", err)
356+
_ = logging.Errorf("Failed to unmount dir: %v", err)
359357
return err
360358
}
361359
err = os.Remove(sharedDir)
362360
if err != nil {
363-
logging.Errorf("Failed to remove dir: %v", err)
361+
_ = logging.Errorf("Failed to remove dir: %v", err)
364362
return err
365363
}
366364
} else {
@@ -379,7 +377,7 @@ func delLocalDeviceVhost(conf *types.NetConf, args *skel.CmdArgs, actualSharedDi
379377

380378
// Remove files with matching container ID and IfName
381379
for _, fileName := range filesForContainerID {
382-
if match, _ := regexp.MatchString(fileBaseName+"*", fileName); match == true {
380+
if match, _ := regexp.MatchString(fileBaseName+"*", fileName); match {
383381
logging.Debugf("OVS DelFromContainer: %s matches file %s", fileBaseName, fileName)
384382
file := filepath.Join(sharedDir, fileName)
385383
if err = os.Remove(file); err != nil {
@@ -392,7 +390,7 @@ func delLocalDeviceVhost(conf *types.NetConf, args *skel.CmdArgs, actualSharedDi
392390

393391
// In case the Socketfile name was passed in:
394392
if conf.HostConf.VhostConf.Socketfile != fileBaseName {
395-
if match, _ := regexp.MatchString(conf.HostConf.VhostConf.Socketfile+"*", fileName); match == true {
393+
if match, _ := regexp.MatchString(conf.HostConf.VhostConf.Socketfile+"*", fileName); match {
396394
file := filepath.Join(sharedDir, fileName)
397395
if err = os.Remove(file); err != nil {
398396
return err
@@ -415,7 +413,7 @@ func delLocalDeviceVhost(conf *types.NetConf, args *skel.CmdArgs, actualSharedDi
415413
func addLocalNetworkBridge(conf *types.NetConf, args *skel.CmdArgs, data *OvsSavedData) error {
416414
var err error
417415

418-
if found := findBridge(conf.HostConf.BridgeConf.BridgeName); found == false {
416+
if found := findBridge(conf.HostConf.BridgeConf.BridgeName); !found {
419417
logging.Debugf("addLocalNetworkBridge(): Bridge %s not found, creating", conf.HostConf.BridgeConf.BridgeName)
420418
err = createBridge(conf.HostConf.BridgeConf.BridgeName)
421419

@@ -438,7 +436,7 @@ func addLocalNetworkBridge(conf *types.NetConf, args *skel.CmdArgs, data *OvsSav
438436
func delLocalNetworkBridge(conf *types.NetConf, args *skel.CmdArgs, data *OvsSavedData) error {
439437
var err error
440438

441-
if containInterfaces := doesBridgeContainInterfaces(conf.HostConf.BridgeConf.BridgeName); containInterfaces == false {
439+
if containInterfaces := doesBridgeContainInterfaces(conf.HostConf.BridgeConf.BridgeName); !containInterfaces {
442440
logging.Debugf("delLocalNetworkBridge(): No interfaces found, deleting Bridge %s", conf.HostConf.BridgeConf.BridgeName)
443441
err = deleteBridge(conf.HostConf.BridgeConf.BridgeName)
444442
} else {

Diff for: cniovs/cniovs_test.go

+21-20
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package cniovs
1717
import (
1818
"errors"
1919
"fmt"
20-
"io/ioutil"
2120
"os"
2221
"os/user"
2322
"path"
@@ -83,7 +82,7 @@ func TestAddOnHost(t *testing.T) {
8382
var result *current.Result
8483
args := testdata.GetTestArgs()
8584

86-
sharedDir, dirErr := ioutil.TempDir("/tmp", "test-cniovs-")
85+
sharedDir, dirErr := os.MkdirTemp("/tmp", "test-cniovs-")
8786
require.NoError(t, dirErr, "Can't create temporary directory")
8887
defer os.RemoveAll(sharedDir)
8988

@@ -113,7 +112,7 @@ func TestAddOnContainer(t *testing.T) {
113112
args := testdata.GetTestArgs()
114113
ovs := CniOvs{}
115114

116-
sharedDir, dirErr := ioutil.TempDir("/tmp", "test-cniovs-")
115+
sharedDir, dirErr := os.MkdirTemp("/tmp", "test-cniovs-")
117116
require.NoError(t, dirErr, "Can't create temporary directory")
118117
defer os.RemoveAll(sharedDir)
119118

@@ -131,7 +130,7 @@ func TestDelOnContainer(t *testing.T) {
131130
args := testdata.GetTestArgs()
132131
ovs := CniOvs{}
133132

134-
sharedDir, dirErr := ioutil.TempDir("/tmp", "test-cniovs-")
133+
sharedDir, dirErr := os.MkdirTemp("/tmp", "test-cniovs-")
135134
require.NoError(t, dirErr, "Can't create temporary directory")
136135
// just in case that DelFromContainer fails
137136
defer os.RemoveAll(sharedDir)
@@ -191,10 +190,10 @@ func TestDelFromHost(t *testing.T) {
191190
}
192191
path := path.Join(localDir, fileName)
193192

194-
require.NoError(t, ioutil.WriteFile(path, []byte(tc.savedData), 0644), "Can't create test file")
193+
require.NoError(t, os.WriteFile(path, []byte(tc.savedData), 0644), "Can't create test file")
195194
defer os.Remove(path)
196195

197-
sharedDir, dirErr := ioutil.TempDir("/tmp", "test-cniovs-")
196+
sharedDir, dirErr := os.MkdirTemp("/tmp", "test-cniovs-")
198197
require.NoError(t, dirErr, "Can't create temporary directory")
199198
defer os.RemoveAll(sharedDir)
200199

@@ -217,10 +216,10 @@ func TestGenerateRandomMacAddress(t *testing.T) {
217216
nr := 10
218217
t.Run(fmt.Sprintf("generate %v random MAC addresses", nr), func(t *testing.T) {
219218
macs := make(map[string]int)
219+
mac_regex := regexp.MustCompile("^[0-9a-fA-F]{2}(:([0-9a-fA-F]){2}){5}$")
220220
for i := 0; i < nr; i++ {
221221
mac := generateRandomMacAddress()
222-
ok, err := regexp.Match("^[0-9a-fA-F]{2}(:([0-9a-fA-F]){2}){5}$", []byte(mac))
223-
require.NoError(t, err, fmt.Sprintf("MAC is incorrect. MAC: %q", mac))
222+
ok := mac_regex.Match([]byte(mac))
224223
require.True(t, ok, fmt.Sprintf("MAC is incorrect. MAC: %q", mac))
225224
macs[mac]++
226225
}
@@ -315,7 +314,7 @@ func TestCreateSharedDir(t *testing.T) {
315314
}
316315
for _, tc := range testCases {
317316
t.Run(tc.name, func(t *testing.T) {
318-
sharedDir, dirErr := ioutil.TempDir("/tmp", "test-cniovs-")
317+
sharedDir, dirErr := os.MkdirTemp("/tmp", "test-cniovs-")
319318
sharedDirNoPath := strings.Split(sharedDir, "/")[2]
320319
require.NoError(t, dirErr, "Can't create temporary directory")
321320
tc.sharedDir = strings.Replace(tc.sharedDir, "#sharedDir#", sharedDir, -1)
@@ -332,7 +331,7 @@ func TestCreateSharedDir(t *testing.T) {
332331
}
333332

334333
// cleanup
335-
unix.Unmount(tc.sharedDir, 0)
334+
_ = unix.Unmount(tc.sharedDir, 0)
336335
os.RemoveAll(tc.sharedDir)
337336
os.RemoveAll(tc.oldSharedDir)
338337

@@ -368,7 +367,7 @@ func TestSetSharedDirGroup(t *testing.T) {
368367
}
369368
for _, tc := range testCases {
370369
t.Run(tc.name, func(t *testing.T) {
371-
sharedDir, dirErr := ioutil.TempDir("/tmp", "test-cniovs-")
370+
sharedDir, dirErr := os.MkdirTemp("/tmp", "test-cniovs-")
372371
require.NoError(t, dirErr, "Can't create temporary directory")
373372
tc.sharedDir = strings.Replace(tc.sharedDir, "#sharedDir#", sharedDir, -1)
374373
defer os.RemoveAll(sharedDir)
@@ -377,7 +376,7 @@ func TestSetSharedDirGroup(t *testing.T) {
377376
if tc.group == "#group#" {
378377
dirInfo, _ := os.Stat(sharedDir)
379378
dirSys := dirInfo.Sys().(*syscall.Stat_t)
380-
group, _ := user.LookupGroupId(string(48 + dirSys.Gid))
379+
group, _ := user.LookupGroupId(string(rune(48 + dirSys.Gid)))
381380
tc.group = group.Name
382381
}
383382

@@ -458,7 +457,7 @@ func TestAddLocalDeviceVhost(t *testing.T) {
458457
args := testdata.GetTestArgs()
459458
execCommand := &FakeExecCommand{Err: tc.fakeErr}
460459

461-
sharedDir, dirErr := ioutil.TempDir("/tmp", "test-cniovs-")
460+
sharedDir, dirErr := os.MkdirTemp("/tmp", "test-cniovs-")
462461
require.NoError(t, dirErr, "Can't create temporary directory")
463462
if !tc.createDir {
464463
require.NoError(t, os.RemoveAll(sharedDir))
@@ -483,7 +482,7 @@ func TestAddLocalDeviceVhost(t *testing.T) {
483482
defer os.RemoveAll(defaultOvSSocketDir)
484483
}
485484
path := path.Join(defaultOvSSocketDir, socketFile)
486-
require.NoError(t, ioutil.WriteFile(path, []byte(""), 0644), "Can't create test file")
485+
require.NoError(t, os.WriteFile(path, []byte(""), 0644), "Can't create test file")
487486
defer os.Remove(path)
488487

489488
}
@@ -515,9 +514,9 @@ func createSocket(t *testing.T, dir, socket string, isDir bool) (socketPath stri
515514
if isDir {
516515
// error case - create dir instead of file and create file inside it
517516
require.NoError(t, os.MkdirAll(socketPath, 0700), "Can't create test directory")
518-
require.NoError(t, ioutil.WriteFile(path.Join(socketPath, socket), []byte(""), 0644), "Can't create test file")
517+
require.NoError(t, os.WriteFile(path.Join(socketPath, socket), []byte(""), 0644), "Can't create test file")
519518
} else {
520-
require.NoError(t, ioutil.WriteFile(socketPath, []byte(""), 0644), "Can't create test file")
519+
require.NoError(t, os.WriteFile(socketPath, []byte(""), 0644), "Can't create test file")
521520
}
522521
return
523522
}
@@ -658,18 +657,20 @@ func TestDelLocalDeviceVhost(t *testing.T) {
658657
// cleanup if needed
659658
defer os.RemoveAll(tc.sharedDir)
660659
defer os.RemoveAll(sharedDir)
661-
defer unix.Unmount(sharedDir, 0)
660+
defer func () {
661+
_ = unix.Unmount(sharedDir, 0)
662+
}()
662663
} else {
663664
var dirErr error
664-
sharedDir, dirErr = ioutil.TempDir("/tmp", "test-cniovs-")
665+
sharedDir, dirErr = os.MkdirTemp("/tmp", "test-cniovs-")
665666
require.NoError(dirErr, "Can't create temporary directory")
666667
switch tc.brokenDir {
667668
case "proc":
668669
require.NoError(os.RemoveAll(sharedDir))
669670
sharedDir = "/proc/broken_shared_dir"
670671
case "file":
671672
require.NoError(os.RemoveAll(sharedDir))
672-
require.NoError(ioutil.WriteFile(sharedDir, []byte(""), 0644), "Can't create test file")
673+
require.NoError(os.WriteFile(sharedDir, []byte(""), 0644), "Can't create test file")
673674
defer os.Remove(sharedDir)
674675
}
675676
// cleanup if needed
@@ -690,7 +691,7 @@ func TestDelLocalDeviceVhost(t *testing.T) {
690691
// prepare noise files, which shall remain and avoid shared dir removal
691692
for i := 0; i < tc.noiseFiles; i++ {
692693
path := path.Join(sharedDir, fmt.Sprintf("noise-file-%v", i))
693-
require.NoError(ioutil.WriteFile(path, []byte(""), 0644), "Can't create test file")
694+
require.NoError(os.WriteFile(path, []byte(""), 0644), "Can't create test file")
694695
defer os.Remove(path)
695696
}
696697

Diff for: cniovs/localdb.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ package cniovs
2222
import (
2323
"encoding/json"
2424
"fmt"
25-
"io/ioutil"
2625
"os"
2726
"path/filepath"
2827

@@ -76,7 +75,7 @@ func SaveConfig(conf *types.NetConf, args *skel.CmdArgs, data *OvsSavedData) err
7675

7776
path := filepath.Join(localDir, fileName)
7877

79-
return ioutil.WriteFile(path, dataBytes, 0644)
78+
return os.WriteFile(path, dataBytes, 0644)
8079
} else {
8180
return fmt.Errorf("ERROR: serializing delegate OVS saved data: %v", err)
8281
}
@@ -89,7 +88,7 @@ func LoadConfig(conf *types.NetConf, args *skel.CmdArgs, data *OvsSavedData) err
8988
path := filepath.Join(localDir, fileName)
9089

9190
if _, err := os.Stat(path); err == nil {
92-
if dataBytes, err := ioutil.ReadFile(path); err == nil {
91+
if dataBytes, err := os.ReadFile(path); err == nil {
9392
if err = json.Unmarshal(dataBytes, data); err != nil {
9493
return fmt.Errorf("ERROR: Failed to parse OVS saved data: %v", err)
9594
}
@@ -102,7 +101,7 @@ func LoadConfig(conf *types.NetConf, args *skel.CmdArgs, data *OvsSavedData) err
102101
}
103102

104103
// Delete file (and directory if empty)
105-
configdata.FileCleanup(localDir, path)
104+
_ = configdata.FileCleanup(localDir, path)
106105

107106
return nil
108107
}

Diff for: cniovs/localdb_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package cniovs
1717
import (
1818
"errors"
1919
"fmt"
20-
"io/ioutil"
2120
"os"
2221
"path"
2322
"testing"
@@ -106,7 +105,7 @@ func TestLoadConfig(t *testing.T) {
106105
case "none":
107106
require.NoFileExists(t, path, "Saved configuration shall not exist")
108107
case "corrupted":
109-
require.NoError(t, ioutil.WriteFile(path, []byte("{"), 0644), "Can't create test file")
108+
require.NoError(t, os.WriteFile(path, []byte("{"), 0644), "Can't create test file")
110109
defer os.Remove(path)
111110
case "directory":
112111
require.NoError(t, os.Mkdir(path, 0700), "Can't create test dir")

Diff for: cniovs/ovsctrl.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func createVhostPort(sock_dir string, sock_name string, client bool, bridge_name
6969
cmd := "ovs-vsctl"
7070
args := []string{"add-port", bridge_name, sock_name, "--", "set", "Interface", sock_name, type_str}
7171

72-
if client == true {
72+
if client {
7373
socketarg := "options:vhost-server-path=" + filepath.Join(sock_dir, sock_name)
7474
logging.Debugf("Additional string: %s", socketarg)
7575

@@ -80,18 +80,18 @@ func createVhostPort(sock_dir string, sock_name string, client bool, bridge_name
8080
return "", err
8181
}
8282

83-
if client == false {
83+
if !client {
8484
// Determine the location OvS uses for Sockets. Default location can be
8585
// overwritten with environmental variable: OVS_SOCKDIR
8686
ovs_socket_dir, ok := os.LookupEnv("OVS_SOCKDIR")
87-
if ok == false {
87+
if !ok {
8888
ovs_socket_dir = defaultOvSSocketDir
8989
}
9090

9191
// Move socket to defined dir for easier mounting
9292
err = os.Rename(filepath.Join(ovs_socket_dir, sock_name), filepath.Join(sock_dir, sock_name))
9393
if err != nil {
94-
logging.Errorf("Rename ERROR: %v", err)
94+
_ = logging.Errorf("Rename ERROR: %v", err)
9595
err = nil
9696

9797
//deleteVhostPort(sock_name, bridge_name)
@@ -159,7 +159,7 @@ func findBridge(bridge_name string) bool {
159159
name, err := execCommand(cmd, args)
160160
logging.Verbosef("ovsctl.findBridge(): return name=%v err=%v", name, err)
161161
if err == nil {
162-
if name != nil && len(name) != 0 {
162+
if len(name) != 0 {
163163
found = true
164164
}
165165
}
@@ -177,7 +177,7 @@ func doesBridgeContainInterfaces(bridge_name string) bool {
177177
name, err := execCommand(cmd, args)
178178
logging.Verbosef("ovsctl.doesBridgeContainInterfaces(): return name=%v err=%v", name, err)
179179
if err == nil {
180-
if name != nil && len(name) != 0 {
180+
if len(name) != 0 {
181181
found = true
182182
}
183183
}

0 commit comments

Comments
 (0)