Skip to content

Commit

Permalink
Init sysctl manager with logger
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwinger233 committed Jan 14, 2024
1 parent 8db2d79 commit 68daeee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
4 changes: 4 additions & 0 deletions control/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ func NewControlPlane(
}
go dnsUpstream.InitUpstreams()

if err = InitSysctlManager(log); err != nil {
return nil, err
}

close(plane.ready)
return plane, nil
}
Expand Down
22 changes: 11 additions & 11 deletions control/sysctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ const SysctlPrefixPath = "/proc/sys/"
var sysctl *SysctlManager

type SysctlManager struct {
log *logrus.Logger
mux sync.Mutex
watcher *fsnotify.Watcher
expectations map[string]string
}

func init() {
var err error
if sysctl, err = NewSysctlManager(); err != nil {
logrus.Fatalf("failed to create sysctl manager: %v", err)
}
func InitSysctlManager(log *logrus.Logger) (err error) {
sysctl, err = NewSysctlManager(log)
return err
}

func NewSysctlManager() (*SysctlManager, error) {
func NewSysctlManager(log *logrus.Logger) (*SysctlManager, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}

manager := &SysctlManager{
log: log,
mux: sync.Mutex{},
watcher: watcher,
expectations: map[string]string{},
Expand All @@ -49,20 +49,20 @@ func (s *SysctlManager) StartWatch() {
return
}
if event.Has(fsnotify.Write) {
logrus.Tracef("sysctl write event: %+v", event)
s.log.Tracef("sysctl write event: %+v", event)
s.mux.Lock()
expected, ok := s.expectations[event.Name]
s.mux.Unlock()
if ok {
raw, err := os.ReadFile(event.Name)
if err != nil {
logrus.Errorf("failed to read sysctl file %s: %v", event.Name, err)
s.log.Errorf("failed to read sysctl file %s: %v", event.Name, err)
}
value := strings.TrimSpace(string(raw))
if value != expected {
logrus.Infof("sysctl %s has unexpected value %s, expected %s", event.Name, value, expected)
s.log.Infof("sysctl %s has unexpected value %s, expected %s", event.Name, value, expected)
if err := os.WriteFile(event.Name, []byte(expected), 0644); err != nil {
logrus.Errorf("failed to write sysctl file %s: %v", event.Name, err)
s.log.Errorf("failed to write sysctl file %s: %v", event.Name, err)
}
}
}
Expand All @@ -71,7 +71,7 @@ func (s *SysctlManager) StartWatch() {
if !ok {
return
}
logrus.Errorf("sysctl watcher error: %v", err)
s.log.Errorf("sysctl watcher error: %v", err)
}
}
}
Expand Down

0 comments on commit 68daeee

Please sign in to comment.