Skip to content

Commit 0801d3c

Browse files
Merge pull request #316 from saschagrunert/fix/quick-wins
Fix error wrapping, deprecated APIs, and logging consistency
2 parents fe89255 + 390ac1a commit 0801d3c

2 files changed

Lines changed: 15 additions & 14 deletions

File tree

‎pkg/ocicni/ocicni.go‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"io/fs"
89
"net"
910
"os"
1011
"path"
@@ -330,7 +331,7 @@ func loadNetworks(ctx context.Context, confDir string, cni *libcni.CNIConfig) (n
330331
confList, err = libcni.ConfListFromFile(confFile)
331332
if err != nil {
332333
// do not log ENOENT errors
333-
if !os.IsNotExist(err) {
334+
if !errors.Is(err, fs.ErrNotExist) {
334335
logrus.Errorf("Error loading CNI config list file %s: %v", confFile, err)
335336
}
336337

@@ -347,7 +348,7 @@ func loadNetworks(ctx context.Context, confDir string, cni *libcni.CNIConfig) (n
347348
conf, err := libcni.NetworkPluginConfFromBytes(bytes)
348349
if err != nil {
349350
// do not log ENOENT errors
350-
if !os.IsNotExist(err) {
351+
if !errors.Is(err, fs.ErrNotExist) {
351352
logrus.Errorf("Error loading CNI config file %s: %v", confFile, err)
352353
}
353354

@@ -372,7 +373,7 @@ func loadNetworks(ctx context.Context, confDir string, cni *libcni.CNIConfig) (n
372373
// Validation on CNI config should be done to pre-check presence
373374
// of plugins which are necessary.
374375
if _, err := cni.ValidateNetworkList(ctx, confList); err != nil {
375-
logrus.Warningf("Error validating CNI config file %s: %v", confFile, err)
376+
logrus.Warnf("Error validating CNI config file %s: %v", confFile, err)
376377

377378
continue
378379
}
@@ -606,7 +607,7 @@ func (plugin *cniNetworkPlugin) forEachNetwork(ctx context.Context, podNetwork *
606607
cniNet, newRt, err = plugin.loadNetworkFromCache(network.Name, rt)
607608
if err != nil {
608609
logrus.Errorf("Error loading cached network config: %v", err)
609-
logrus.Warningf("Falling back to loading from existing plugins on disk")
610+
logrus.Warnf("Falling back to loading from existing plugins on disk")
610611
} else {
611612
// Use the updated RuntimeConf
612613
rt = newRt
@@ -732,7 +733,7 @@ func (plugin *cniNetworkPlugin) getCachedNetworkInfo(containerID string) ([]NetA
732733
}
733734

734735
if cachedInfo.Kind != libcni.CNICacheV1 {
735-
logrus.Warningf("Unknown CNI cache file %s kind %q", cacheFile, cachedInfo.Kind)
736+
logrus.Warnf("Unknown CNI cache file %s kind %q", cacheFile, cachedInfo.Kind)
736737

737738
continue
738739
}
@@ -746,7 +747,7 @@ func (plugin *cniNetworkPlugin) getCachedNetworkInfo(containerID string) ([]NetA
746747
}
747748

748749
if cachedInfo.IfName == "" || cachedInfo.NetName == "" {
749-
logrus.Warningf("Missing CNI cache file %s ifname %q or netname %q", cacheFile, cachedInfo.IfName, cachedInfo.NetName)
750+
logrus.Warnf("Missing CNI cache file %s ifname %q or netname %q", cacheFile, cachedInfo.IfName, cachedInfo.NetName)
750751

751752
continue
752753
}
@@ -963,7 +964,7 @@ func (network *cniNetwork) checkNetwork(ctx context.Context, rt *libcni.RuntimeC
963964
}
964965

965966
if cniInterface == nil || len(ips) == 0 {
966-
return nil, fmt.Errorf("neither IPv4 nor IPv6 found when retrieving network status: %v", errs)
967+
return nil, fmt.Errorf("neither IPv4 nor IPv6 found when retrieving network status: %w", errors.Join(errs...))
967968
}
968969

969970
result = &cniv1.Result{
@@ -998,7 +999,7 @@ func buildCNIRuntimeConf(podNetwork *PodNetwork, ifName string, runtimeConfig *R
998999
runtimeConfig = &RuntimeConfig{}
9991000
}
10001001

1001-
logrus.Infof("Got pod network %+v", podNetwork)
1002+
logrus.Debugf("Got pod network %+v", podNetwork)
10021003

10031004
rt := &libcni.RuntimeConf{
10041005
ContainerID: podNetwork.ID,

‎pkg/ocicni/util_freebsd.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ func getContainerDetails(nsm *nsManager, netnsJailName, interfaceName, addrType
2929
interfaceName,
3030
addrType).CombinedOutput()
3131
if err != nil {
32-
return nil, nil, fmt.Errorf("Unexpected command output %s with error: %v", output, err)
32+
return nil, nil, fmt.Errorf("unexpected command output %s with error: %w", output, err)
3333
}
3434

3535
lines := strings.Split(string(output), "\n")
3636
if len(lines) < 3 {
37-
return nil, nil, fmt.Errorf("Unexpected command output %s", output)
37+
return nil, nil, fmt.Errorf("unexpected command output %s", output)
3838
}
3939
fields := strings.Fields(strings.TrimSpace(lines[2]))
4040
if len(fields) < 2 {
41-
return nil, nil, fmt.Errorf("Unexpected address output %s ", lines[0])
41+
return nil, nil, fmt.Errorf("unexpected address output %s ", lines[0])
4242
}
4343
ip, ipNet, err := net.ParseCIDR(fields[1])
4444
if err != nil {
45-
return nil, nil, fmt.Errorf("CNI failed to parse ip from output %s due to %v", output, err)
45+
return nil, nil, fmt.Errorf("failed to parse ip from output %s due to %w", output, err)
4646
}
4747
if ip.To4() == nil {
4848
ipNet.IP = ip
@@ -56,7 +56,7 @@ func getContainerDetails(nsm *nsManager, netnsJailName, interfaceName, addrType
5656
interfaceName,
5757
"ether").CombinedOutput()
5858
if err != nil {
59-
return nil, nil, fmt.Errorf("unexpected ifconfig command output %s with error: %v", output, err)
59+
return nil, nil, fmt.Errorf("unexpected ifconfig command output %s with error: %w", output, err)
6060
}
6161

6262
lines = strings.Split(string(output), "\n")
@@ -69,7 +69,7 @@ func getContainerDetails(nsm *nsManager, netnsJailName, interfaceName, addrType
6969
}
7070
mac, err := net.ParseMAC(fields[1])
7171
if err != nil {
72-
return nil, nil, fmt.Errorf("failed to parse MAC from output %s due to %v", output, err)
72+
return nil, nil, fmt.Errorf("failed to parse MAC from output %s due to %w", output, err)
7373
}
7474

7575
return ipNet, &mac, nil

0 commit comments

Comments
 (0)