From 57b7a3c6a8ed709cb96b4a9f81ba39c13c4fced8 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Mon, 22 Jan 2024 22:45:15 +0200 Subject: [PATCH] Wrap nested errors --- pkg/nfd-master/nfd-master.go | 16 ++++++++-------- pkg/resourcemonitor/podresourcesscanner.go | 2 +- pkg/utils/fswatcher.go | 4 ++-- pkg/utils/klog/klog.go | 2 +- pkg/utils/tls.go | 4 ++-- source/cpu/power_amd64.go | 2 +- source/local/local.go | 4 ++-- source/pci/utils.go | 4 ++-- source/usb/utils.go | 2 +- 9 files changed, 20 insertions(+), 20 deletions(-) diff --git a/pkg/nfd-master/nfd-master.go b/pkg/nfd-master/nfd-master.go index fa113f2207..ae501a6cf6 100644 --- a/pkg/nfd-master/nfd-master.go +++ b/pkg/nfd-master/nfd-master.go @@ -251,7 +251,7 @@ func (m *nfdMaster) Run() error { if !m.config.NoPublish { err := m.updateMasterNode() if err != nil { - return fmt.Errorf("failed to update master node: %v", err) + return fmt.Errorf("failed to update master node: %w", err) } } @@ -368,7 +368,7 @@ func (m *nfdMaster) runGrpcServer(errChan chan<- error) { // Create server listening for TCP connections lis, err := net.Listen("tcp", fmt.Sprintf(":%d", m.args.Port)) if err != nil { - errChan <- fmt.Errorf("failed to listen: %v", err) + errChan <- fmt.Errorf("failed to listen: %w", err) return } @@ -414,7 +414,7 @@ func (m *nfdMaster) runGrpcServer(errChan chan<- error) { case err := <-grpcErr: if err != nil { - errChan <- fmt.Errorf("gRPC server exited with an error: %v", err) + errChan <- fmt.Errorf("gRPC server exited with an error: %w", err) } klog.InfoS("gRPC server stopped") } @@ -552,7 +552,7 @@ func (m *nfdMaster) updateMasterNode() error { "/metadata/annotations") err = m.apihelper.PatchNode(cli, node.Name, p) if err != nil { - return fmt.Errorf("failed to patch node annotations: %v", err) + return fmt.Errorf("failed to patch node annotations: %w", err) } return nil @@ -951,7 +951,7 @@ func (m *nfdMaster) setTaints(cli *kubernetes.Clientset, taints []corev1.Taint, if len(patches) > 0 { err = m.apihelper.PatchNode(cli, node.Name, patches) if err != nil { - return fmt.Errorf("error while patching node object: %v", err) + return fmt.Errorf("error while patching node object: %w", err) } klog.V(1).InfoS("patched node annotations for taints", "nodeName", nodeName) } @@ -1112,13 +1112,13 @@ func (m *nfdMaster) updateNodeObject(cli *kubernetes.Clientset, nodeName string, statusPatches := m.createExtendedResourcePatches(node, extendedResources) err = m.apihelper.PatchNodeStatus(cli, node.Name, statusPatches) if err != nil { - return fmt.Errorf("error while patching extended resources: %v", err) + return fmt.Errorf("error while patching extended resources: %w", err) } // Patch the node object in the apiserver err = m.apihelper.PatchNode(cli, node.Name, patches) if err != nil { - return fmt.Errorf("error while patching node object: %v", err) + return fmt.Errorf("error while patching node object: %w", err) } if len(patches) > 0 || len(statusPatches) > 0 { @@ -1235,7 +1235,7 @@ func (m *nfdMaster) configure(filepath string, overrides string) error { // Parse config overrides if err := yaml.Unmarshal([]byte(overrides), c); err != nil { - return fmt.Errorf("failed to parse -options: %s", err) + return fmt.Errorf("failed to parse -options: %w", err) } if m.args.Overrides.NoPublish != nil { c.NoPublish = *m.args.Overrides.NoPublish diff --git a/pkg/resourcemonitor/podresourcesscanner.go b/pkg/resourcemonitor/podresourcesscanner.go index 5ff086463f..37ffb1dd7f 100644 --- a/pkg/resourcemonitor/podresourcesscanner.go +++ b/pkg/resourcemonitor/podresourcesscanner.go @@ -153,7 +153,7 @@ func (resMon *PodResourcesScanner) Scan() (ScanResponse, error) { hasDevice := hasDevice(podResource) isWatchable, isIntegralGuaranteed, err := resMon.isWatchable(podResource.GetNamespace(), podResource.GetName(), hasDevice) if err != nil { - return ScanResponse{}, fmt.Errorf("checking if pod in a namespace is watchable, namespace:%v, pod name %v: %v", podResource.GetNamespace(), podResource.GetName(), err) + return ScanResponse{}, fmt.Errorf("checking if pod in a namespace is watchable, namespace:%v, pod name %v: %w", podResource.GetNamespace(), podResource.GetName(), err) } if !isWatchable { continue diff --git a/pkg/utils/fswatcher.go b/pkg/utils/fswatcher.go index 82bf58b06f..5bc51fc5cc 100644 --- a/pkg/utils/fswatcher.go +++ b/pkg/utils/fswatcher.go @@ -67,7 +67,7 @@ func (w *FsWatcher) reset(names ...string) error { func (w *FsWatcher) initWatcher() error { if w.Watcher != nil { if err := w.Watcher.Close(); err != nil { - return fmt.Errorf("failed to close fsnotify watcher: %v", err) + return fmt.Errorf("failed to close fsnotify watcher: %w", err) } } w.paths = make(map[string]struct{}) @@ -75,7 +75,7 @@ func (w *FsWatcher) initWatcher() error { watcher, err := fsnotify.NewWatcher() if err != nil { w.Watcher = nil - return fmt.Errorf("failed to create fsnotify watcher: %v", err) + return fmt.Errorf("failed to create fsnotify watcher: %w", err) } w.Watcher = watcher diff --git a/pkg/utils/klog/klog.go b/pkg/utils/klog/klog.go index ec8845d0da..08ed254641 100644 --- a/pkg/utils/klog/klog.go +++ b/pkg/utils/klog/klog.go @@ -52,7 +52,7 @@ func MergeKlogConfiguration(klogArgs map[string]*utils.KlogFlagVal, klogConfig K v = a.DefValue() } if err := a.SetFromConfig(v); err != nil { - return fmt.Errorf("failed to set logger option klog.%s = %v: %v", k, v, err) + return fmt.Errorf("failed to set logger option klog.%s = %v: %w", k, v, err) } } } diff --git a/pkg/utils/tls.go b/pkg/utils/tls.go index 3e2e6efe77..45f8b3cd44 100644 --- a/pkg/utils/tls.go +++ b/pkg/utils/tls.go @@ -47,12 +47,12 @@ func (c *TlsConfig) UpdateConfig(certFile, keyFile, caFile string) error { // Load cert for authenticating this server cert, err := tls.LoadX509KeyPair(certFile, keyFile) if err != nil { - return fmt.Errorf("failed to load server certificate: %v", err) + return fmt.Errorf("failed to load server certificate: %w", err) } // Load CA cert for client cert verification caCert, err := os.ReadFile(caFile) if err != nil { - return fmt.Errorf("failed to read root certificate file: %v", err) + return fmt.Errorf("failed to read root certificate file: %w", err) } caPool := x509.NewCertPool() if ok := caPool.AppendCertsFromPEM(caCert); !ok { diff --git a/source/cpu/power_amd64.go b/source/cpu/power_amd64.go index 931af5262d..7bd24e3fdb 100644 --- a/source/cpu/power_amd64.go +++ b/source/cpu/power_amd64.go @@ -69,7 +69,7 @@ func discoverSSTBF() (bool, error) { effectiveBaseFreq, err := strconv.Atoi(strings.TrimSpace(string(data))) if err != nil { - return false, fmt.Errorf("non-integer value of %q: %v", filePath, err) + return false, fmt.Errorf("non-integer value of %q: %w", filePath, err) } // Sanity check: Return an error (we don't have enough information to diff --git a/source/local/local.go b/source/local/local.go index 64508ac086..e880f685e3 100644 --- a/source/local/local.go +++ b/source/local/local.go @@ -291,7 +291,7 @@ func getFeaturesFromHooks() (map[string]string, map[string]string, error) { klog.InfoS("hook directory does not exist", "path", hookDir) return features, labels, nil } - return features, labels, fmt.Errorf("unable to access %v: %v", hookDir, err) + return features, labels, fmt.Errorf("unable to access %v: %w", hookDir, err) } if len(files) > 0 { klog.InfoS("hooks are DEPRECATED since v0.12.0 and support will be removed in a future release; use feature files instead") @@ -382,7 +382,7 @@ func getFeaturesFromFiles() (map[string]string, map[string]string, error) { klog.InfoS("features directory does not exist", "path", featureFilesDir) return features, labels, nil } - return features, labels, fmt.Errorf("unable to access %v: %v", featureFilesDir, err) + return features, labels, fmt.Errorf("unable to access %v: %w", featureFilesDir, err) } for _, file := range files { diff --git a/source/pci/utils.go b/source/pci/utils.go index ac16b90e45..f857baa28c 100644 --- a/source/pci/utils.go +++ b/source/pci/utils.go @@ -36,7 +36,7 @@ var optionalDevAttrs = []string{"sriov_totalvfs", "iommu_group/type", "iommu/int func readSinglePciAttribute(devPath string, attrName string) (string, error) { data, err := os.ReadFile(filepath.Join(devPath, attrName)) if err != nil { - return "", fmt.Errorf("failed to read device attribute %s: %v", attrName, err) + return "", fmt.Errorf("failed to read device attribute %s: %w", attrName, err) } // Strip whitespace and '0x' prefix attrVal := strings.TrimSpace(strings.TrimPrefix(string(data), "0x")) @@ -55,7 +55,7 @@ func readPciDevInfo(devPath string) (*nfdv1alpha1.InstanceFeature, error) { for _, attr := range mandatoryDevAttrs { attrVal, err := readSinglePciAttribute(devPath, attr) if err != nil { - return nil, fmt.Errorf("failed to read device %s: %s", attr, err) + return nil, fmt.Errorf("failed to read device %s: %w", attr, err) } attrs[attr] = attrVal } diff --git a/source/usb/utils.go b/source/usb/utils.go index 3215a5b1c3..2fbf80e500 100644 --- a/source/usb/utils.go +++ b/source/usb/utils.go @@ -43,7 +43,7 @@ var devAttrFileMap = map[string]string{ func readSingleUsbSysfsAttribute(path string) (string, error) { data, err := os.ReadFile(path) if err != nil { - return "", fmt.Errorf("failed to read device attribute %s: %v", filepath.Base(path), err) + return "", fmt.Errorf("failed to read device attribute %s: %w", filepath.Base(path), err) } attrVal := strings.TrimSpace(string(data))