Skip to content

lima-guestagent: support external trigger via sighup notification #3766

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
42 changes: 41 additions & 1 deletion cmd/lima-guestagent/daemon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"errors"
"net"
"os"
"os/signal"
"syscall"
"time"

"github.com/mdlayher/vsock"
Expand Down Expand Up @@ -58,7 +60,45 @@ func daemonAction(cmd *cobra.Command, _ []string) error {
// without depending on `bpftrace` binary.
// The agent binary will need CAP_BPF file cap.
ticker := time.NewTicker(tick)
return ticker.C, ticker.Stop

logrus.Info("Registering SIGHUP handler for externally triggered port mapping updates")
// Sending SIGHUP allows external tools to trigger a port mapping update.
// E.g., a script reacting to Docker events can trigger a timely update.
sighupC := make(chan os.Signal, 1)
signal.Notify(sighupC, syscall.SIGHUP)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment line to explain who sends SIGHUP

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I have added a comment about the purpose of the SIGHUP mechanism and also improved the log message.


c := make(chan time.Time, 1)
go func() {
defer close(c)
defer close(sighupC)
for ticker.C != nil {
select {
case _, ok := <-sighupC:
if !ok {
logrus.Info("Sighup channel closed")
sighupC = nil
} else {
logrus.Debug("Sighup received")
c <- time.Now()
}
case now, ok := <-ticker.C:
if !ok {
ticker.C = nil
} else {
c <- now
}
}
}
logrus.Info("ticker stopped")
}()

stop := func() {
logrus.Info("stopping ticker")
ticker.Stop()
signal.Stop(sighupC)
}

return c, stop
}

agent, err := guestagent.New(newTicker, tick*20)
Expand Down
Loading