Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

devices: Use slog instead of logrus #32469

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions pkg/logging/logfields/logfields.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const (
// EndpointState is the current endpoint state
EndpointState = "endpointState"

// Error is the Go error
Error = "error"

// EventUUID is an event unique identifier
EventUUID = "eventID"

Expand Down