Skip to content

Commit

Permalink
Log retry error when setting MAC address
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Panattoni <[email protected]>
  • Loading branch information
zeeke committed Oct 13, 2023
1 parent 03df0d8 commit 4ef5df7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
16 changes: 12 additions & 4 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strconv"
"strings"
"time"

"github.com/k8snetworkplumbingwg/sriov-cni/pkg/logging"
)

var (
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("") })

Check failure on line 91 in pkg/utils/utils_test.go

View workflow job for this annotation

GitHub Actions / build-test (1.20.x, ubuntu-latest, linux, amd64)

not enough arguments in call to retry

Check failure on line 91 in pkg/utils/utils_test.go

View workflow job for this annotation

GitHub Actions / Lint

not enough arguments in call to retry

Check failure on line 91 in pkg/utils/utils_test.go

View workflow job for this annotation

GitHub Actions / build-test (1.20.x, ubuntu-latest, linux, ppc64le)

not enough arguments in call to retry
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 })

Check failure on line 95 in pkg/utils/utils_test.go

View workflow job for this annotation

GitHub Actions / build-test (1.20.x, ubuntu-latest, linux, amd64)

not enough arguments in call to retry

Check failure on line 95 in pkg/utils/utils_test.go

View workflow job for this annotation

GitHub Actions / Lint

not enough arguments in call to retry

Check failure on line 95 in pkg/utils/utils_test.go

View workflow job for this annotation

GitHub Actions / build-test (1.20.x, ubuntu-latest, linux, ppc64le)

not enough arguments in call to retry
Expect(err).NotTo((HaveOccurred()), "retry should not return an error")
})
})
Context("Checking SetVFEffectiveMAC function", func() {
Expand Down

0 comments on commit 4ef5df7

Please sign in to comment.