From 4ef5df74b4e60da78a3153bd4bac5afd3d18a221 Mon Sep 17 00:00:00 2001 From: Andrea Panattoni Date: Thu, 12 Oct 2023 16:57:19 +0200 Subject: [PATCH] Log retry error when setting MAC address Signed-off-by: Andrea Panattoni --- pkg/utils/utils.go | 16 ++++++++++++---- pkg/utils/utils_test.go | 10 +++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 81bcf70f3..44c562b20 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -10,6 +10,8 @@ import ( "strconv" "strings" "time" + + "github.com/k8snetworkplumbingwg/sriov-cni/pkg/logging" ) var ( @@ -310,7 +312,7 @@ func SetVFEffectiveMAC(netLinkManager NetlinkManager, netDeviceName string, macA return err } - return Retry(20, 100*time.Millisecond, func() error { + return retry("SetVFEffectiveMAC", 20, 100*time.Millisecond, func() error { if err := netLinkManager.LinkSetHardwareAddr(orgLinkObj, hwaddr); err != nil { return err } @@ -344,7 +346,7 @@ func SetVFHardwareMAC(netLinkManager NetlinkManager, pfDevice string, vfID int, return err } - return Retry(20, 100*time.Millisecond, func() error { + return retry("SetVFHardwareMAC", 20, 100*time.Millisecond, func() error { if err := netLinkManager.LinkSetVfHardwareAddr(orgLinkObj, vfID, hwaddr); err != nil { return err } @@ -390,14 +392,20 @@ func IsIPv6(ip net.IP) bool { return ip.To4() == nil && ip.To16() != nil } -// Retry retries a given function until no return error; times out after retries*sleep -func Retry(retries int, sleep time.Duration, f func() error) error { +// retry retries a given function until no return error; times out after retries*sleep. +// It logs a warning every time the given function returns an error. +func retry(callerFn string, retries int, sleep time.Duration, f func() error) error { err := error(nil) for retry := 0; retry < retries; retry++ { err = f() if err == nil { return nil } + + logging.Warning("Delegated function error", + "func", callerFn, + "error", err) + time.Sleep(sleep) } return err diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 273f02e91..b833250be 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -86,14 +86,14 @@ var _ = Describe("Utils", func() { Expect(err).To(HaveOccurred(), "Not existing VF should return an error") }) }) - Context("Checking Retry function", func() { + Context("Checking retry function", func() { It("Assuming calling function fails", func() { - err := Retry(5, 10*time.Millisecond, func() error { return errors.New("") }) - Expect(err).To((HaveOccurred()), "Retry should return an error") + err := retry(5, 10*time.Millisecond, func() error { return errors.New("") }) + Expect(err).To((HaveOccurred()), "retry should return an error") }) It("Assuming calling function does not fail", func() { - err := Retry(5, 10*time.Millisecond, func() error { return nil }) - Expect(err).NotTo((HaveOccurred()), "Retry should not return an error") + err := retry(5, 10*time.Millisecond, func() error { return nil }) + Expect(err).NotTo((HaveOccurred()), "retry should not return an error") }) }) Context("Checking SetVFEffectiveMAC function", func() {