Skip to content

flowlogs: cleanup socket when creating local reporter #10276

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

Merged
merged 2 commits into from
Apr 23, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions felix/collector/local/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,22 @@ func (s *FlowServer) Run() error {
logrus.WithError(err).Error("Failed to create local socket")
return err
}
addr := s.Address()

// Try to clean up socket if it exists. Simply continue with any error. It might work fine to use the same socket.
err = cleanupLocalSocket(addr)
if err != nil {
logrus.WithError(err).WithField("address", addr).Debug("Failed to clean up local socket")
}

s.once.Do(func() {
var l net.Listener
sockAddr := s.Address()
l, err = net.Listen("unix", sockAddr)
l, err = net.Listen("unix", addr)
if err != nil {
logrus.WithError(err).Error("Failed to listen on local socket")
return
}
logrus.WithField("address", sockAddr).Info("Running local server")
logrus.WithField("address", addr).Info("Running local server")
go func() {
err = s.grpcServer.Serve(l)
if err != nil {
Expand Down Expand Up @@ -152,7 +158,10 @@ func (s *FlowServer) Watch(
}

func (s *FlowServer) Stop() {
cleanupLocalSocket(s.Address())
addr := s.Address()
if err := cleanupLocalSocket(addr); err != nil {
logrus.WithError(err).WithField("address", addr).Errorf("Failed to clean up local socket")
}
s.grpcServer.Stop()
}

Expand All @@ -173,7 +182,7 @@ func (s *FlowServer) Address() string {
}

func ensureLocalSocketDirExists(dir string) error {
logrus.Debug("Checking if local socket exists.")
logrus.Debug("Checking if local socket directory exists.")
if _, err := os.Stat(dir); os.IsNotExist(err) {
logrus.WithField("directory", dir).Debug("Local socket directory does not exist")
err := os.MkdirAll(dir, 0o600)
Expand All @@ -186,13 +195,10 @@ func ensureLocalSocketDirExists(dir string) error {
return nil
}

func cleanupLocalSocket(addr string) {
func cleanupLocalSocket(addr string) error {
_, err := os.Stat(addr)
if err != nil && os.IsNotExist(err) {
return
}
err = os.Remove(addr)
if err != nil {
logrus.WithError(err).WithField("address", addr).Errorf("Failed to remove local socket")
return nil
}
return os.Remove(addr)
}
2 changes: 1 addition & 1 deletion node/pkg/flowlogs/flowlogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func flowToString(f *types.Flow) string {
startTime.Local(), f.Key.Reporter(), f.Key.Action(),
endpointTypeToString(f.Key.SourceType()), f.Key.SourceNamespace(), f.Key.SourceName(),
endpointTypeToString(f.Key.DestType()), f.Key.DestNamespace(), f.Key.DestName(),
f.Key.DestServiceName(), f.Key.DestServiceNamespace(),
f.Key.DestServiceNamespace(), f.Key.DestServiceName(),
f.Key.Proto(), f.Key.DestPort(), f.Key.DestServicePortName(), f.Key.DestServicePort(),
f.PacketsIn, f.BytesIn, f.PacketsOut, f.BytesOut,
f.NumConnectionsStarted, f.NumConnectionsCompleted, f.NumConnectionsLive,
Expand Down