Skip to content

Commit

Permalink
devices: Convert from logrus to slog
Browse files Browse the repository at this point in the history
Use the slog.Logger in DevicesController for logging.

Signed-off-by: Jussi Maki <jussi@isovalent.com>
  • Loading branch information
joamaki committed May 13, 2024
1 parent 09da972 commit 18ed625
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
24 changes: 12 additions & 12 deletions pkg/datapath/linux/devices_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package linux
import (
"context"
"fmt"
"log/slog"
"net"
"net/netip"
"slices"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/cilium/ebpf/asm"
"github.com/cilium/hive/cell"
"github.com/cilium/statedb"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netlink/nl"
Expand Down Expand Up @@ -101,7 +101,7 @@ type devicesControllerParams struct {
cell.In

Config DevicesConfig
Log logrus.FieldLogger
Log *slog.Logger
DB statedb.Handle
DeviceTable statedb.RWTable[*tables.Device]
RouteTable statedb.RWTable[*tables.Route]
Expand All @@ -112,7 +112,7 @@ type devicesControllerParams struct {

type devicesController struct {
params devicesControllerParams
log logrus.FieldLogger
log *slog.Logger

initialized chan struct{}
filter deviceFilter
Expand Down Expand Up @@ -195,27 +195,27 @@ func (dc *devicesController) subscribeAndProcess(ctx context.Context) {
// It cancels the context to unsubscribe from netlink updates
// which stops the processing.
errorCallback := func(err error) {
dc.log.WithError(err).Warn("Netlink error received, restarting")
dc.log.Warn("Netlink error received, restarting", logfields.Error, err)

// Cancel the context to stop the subscriptions.
cancel()
}

addrUpdates := make(chan netlink.AddrUpdate)
if err := dc.params.NetlinkFuncs.AddrSubscribe(addrUpdates, ctx.Done(), errorCallback); err != nil {
dc.log.WithError(err).Warn("AddrSubscribe failed, restarting")
dc.log.Warn("AddrSubscribe failed, restarting", logfields.Error, err)
return
}
routeUpdates := make(chan netlink.RouteUpdate)
err := dc.params.NetlinkFuncs.RouteSubscribe(routeUpdates, ctx.Done(), errorCallback)
if err != nil {
dc.log.WithError(err).Warn("RouteSubscribe failed, restarting")
dc.log.Warn("RouteSubscribe failed, restarting", logfields.Error, err)
return
}
linkUpdates := make(chan netlink.LinkUpdate)
err = dc.params.NetlinkFuncs.LinkSubscribe(linkUpdates, ctx.Done(), errorCallback)
if err != nil {
dc.log.WithError(err).Warn("LinkSubscribe failed, restarting", err)
dc.log.Warn("LinkSubscribe failed, restarting", logfields.Error, err)
return
}

Expand All @@ -225,7 +225,7 @@ func (dc *devicesController) subscribeAndProcess(ctx context.Context) {
// ends and updates begin.
err = dc.initialize()
if err != nil {
dc.log.WithError(err).Warn("Initialization failed, restarting")
dc.log.Warn("Initialization failed, restarting", logfields.Error, err)
return
}

Expand Down Expand Up @@ -451,12 +451,12 @@ func (dc *devicesController) processBatch(txn statedb.WriteTxn, batch map[int][]
if u.Type == unix.RTM_NEWROUTE {
_, _, err := dc.params.RouteTable.Insert(txn, &r)
if err != nil {
dc.log.WithError(err).WithField(logfields.Route, r).Warn("Failed to insert route")
dc.log.Warn("Failed to insert route", logfields.Error, err, "route", r)
}
} else if u.Type == unix.RTM_DELROUTE {
_, _, err := dc.params.RouteTable.Delete(txn, &r)
if err != nil {
dc.log.WithError(err).WithField(logfields.Route, r).Warn("Failed to delete route")
dc.log.Warn("Failed to delete route", logfields.Error, err, "route", r)
}
}
case netlink.LinkUpdate:
Expand Down Expand Up @@ -499,13 +499,13 @@ func (dc *devicesController) processBatch(txn statedb.WriteTxn, batch map[int][]
// Create or update the device.
_, _, err := dc.params.DeviceTable.Insert(txn, d)
if err != nil {
dc.log.WithError(err).WithField(logfields.Device, d).Warn("Failed to insert route")
dc.log.Warn("Failed to insert device", logfields.Error, err, logfields.Device, d)
}
}
}
after := dc.deviceNameSet(txn)
if !before.Equal(after) {
dc.log.WithField(logfields.Devices, after.UnsortedList()).Info("Devices changed")
dc.log.Info("Devices changed", logfields.Devices, after.UnsortedList())
}
}

Expand Down
8 changes: 2 additions & 6 deletions pkg/datapath/linux/devices_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"encoding/json"
"errors"
"log/slog"
"net"
"net/netip"
"os"
Expand Down Expand Up @@ -83,9 +84,6 @@ func TestDevicesController(t *testing.T) {
for _, r := range routes {
// undefined IP will stringify as "invalid IP", turn them into "".
actualDst, actualSrc, actualGw := r.Dst.String(), addrToString(r.Src), addrToString(r.Gw)
/*fmt.Printf("%d %s %s %s -- %d %s %s %s\n",
r.LinkIndex, actualDst, actualSrc, actualGw,
linkIndex, dst, src, gw)*/
if r.LinkIndex == linkIndex && actualDst == dst && actualSrc == src &&
actualGw == gw {
return true
Expand Down Expand Up @@ -419,8 +417,6 @@ func TestDevicesController_Restarts(t *testing.T) {
devicesTable statedb.Table[*tables.Device]
)

logging.SetLogLevelToDebug()

// Is this the first subscription?
var first atomic.Bool
first.Store(true)
Expand Down Expand Up @@ -520,7 +516,7 @@ func TestDevicesController_Restarts(t *testing.T) {
},
}

tlog := hivetest.Logger(t)
tlog := hivetest.Logger(t, hivetest.LogLevel(slog.LevelDebug))
h := hive.New(
DevicesControllerCell,
cell.Provide(func() *netlinkFuncs { return &funcs }),
Expand Down

0 comments on commit 18ed625

Please sign in to comment.