Skip to content

Commit

Permalink
Don't use global logrus logger
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwinger233 committed Jan 15, 2024
1 parent 68daeee commit 5c4aa21
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
1 change: 1 addition & 0 deletions control/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ func NewControlPlane(
}
go dnsUpstream.InitUpstreams()

InitDaeNetns(log)
if err = InitSysctlManager(log); err != nil {
return nil, err
}
Expand Down
18 changes: 11 additions & 7 deletions control/netns_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ var (
)

type DaeNetns struct {
log *logrus.Logger

setupDone atomic.Bool
mu sync.Mutex

dae0, dae0peer netlink.Link
hostNs, daeNs netns.NsHandle
}

func init() {
daeNetns = &DaeNetns{}
func InitDaeNetns(log *logrus.Logger) {
daeNetns = &DaeNetns{
log: log,
}
}

func GetDaeNetns() *DaeNetns {
Expand Down Expand Up @@ -85,7 +89,7 @@ func (ns *DaeNetns) With(f func() error) (err error) {
}

func (ns *DaeNetns) setup() (err error) {
logrus.Trace("setting up dae netns")
ns.log.Trace("setting up dae netns")

runtime.LockOSThread()
defer runtime.UnlockOSThread()
Expand Down Expand Up @@ -286,17 +290,17 @@ func (ns *DaeNetns) monitorDae0LinkAddr() {

err := netlink.LinkSubscribe(ch, done)
if err != nil {
logrus.Errorf("failed to subscribe link updates: %v", err)
ns.log.Errorf("failed to subscribe link updates: %v", err)
}
if ns.dae0, err = netlink.LinkByName(HostVethName); err != nil {
logrus.Errorf("failed to get link dae0: %v", err)
ns.log.Errorf("failed to get link dae0: %v", err)
}
if err = ns.updateNeigh(); err != nil {
logrus.Errorf("failed to update neigh: %v", err)
ns.log.Errorf("failed to update neigh: %v", err)
}
for msg := range ch {
if msg.Link.Attrs().Name == HostVethName && !bytes.Equal(msg.Link.Attrs().HardwareAddr, ns.dae0.Attrs().HardwareAddr) {
logrus.WithField("old addr", ns.dae0.Attrs().HardwareAddr).WithField("new addr", msg.Link.Attrs().HardwareAddr).Info("dae0 link addr changed")
ns.log.WithField("old addr", ns.dae0.Attrs().HardwareAddr).WithField("new addr", msg.Link.Attrs().HardwareAddr).Info("dae0 link addr changed")
ns.dae0 = msg.Link
ns.updateNeigh()
}
Expand Down
4 changes: 0 additions & 4 deletions control/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ func sendPkt(data []byte, from netip.AddrPort, realTo, to netip.AddrPort, lConn

uConn, _, err := DefaultAnyfromPool.GetOrCreate(from.String(), AnyfromTimeout)
if err != nil && errors.Is(err, syscall.EADDRINUSE) {
logrus.WithField("from", from).
WithField("to", to).
WithField("realTo", realTo).
Trace("Port in use, fallback to use netns.")
err = GetDaeNetns().With(func() (err error) {
uConn, _, err = DefaultAnyfromPool.GetOrCreate(from.String(), AnyfromTimeout)
return err
Expand Down

0 comments on commit 5c4aa21

Please sign in to comment.