Skip to content

Commit

Permalink
remove inotify
Browse files Browse the repository at this point in the history
  • Loading branch information
conradludgate committed Feb 4, 2025
1 parent c272e69 commit 546f869
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 49 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ require (
github.com/docker/cli v25.0.3+incompatible
github.com/docker/docker v24.0.9+incompatible
github.com/docker/libnetwork v0.8.0-dev.2.0.20210525090646-64b7a4574d14
github.com/fsnotify/fsnotify v1.7.0
github.com/go-logr/logr v1.4.1
github.com/go-logr/zapr v1.3.0
github.com/jpillora/backoff v1.0.0
Expand Down Expand Up @@ -128,6 +127,7 @@ require (
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand Down
67 changes: 19 additions & 48 deletions neonvm-runner/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"time"

"github.com/digitalocean/go-qemu/qmp"
"github.com/fsnotify/fsnotify"
"github.com/jpillora/backoff"
"github.com/samber/lo"
"go.uber.org/zap"
Expand Down Expand Up @@ -673,35 +672,20 @@ func forwardLogs(ctx context.Context, logger *zap.Logger, wg *sync.WaitGroup) {
func monitorFiles(ctx context.Context, logger *zap.Logger, wg *sync.WaitGroup, disks []vmv1.Disk) {
defer wg.Done()

notify, err := fsnotify.NewWatcher()
if err != nil {
logger.Error("failed to create inotify instance", zap.Error(err))
return
}
defer notify.Close()

secrets := make(map[string]string)
for _, disk := range disks {
if diskNeedsSynchronisation(disk) {
// secrets are mounted using the atomicwriter utility, which loads the secret directory
// into `..data`.
dataDir := fmt.Sprintf("/vm/mounts%s/..data", disk.MountPath)
if err := notify.Add(dataDir); err != nil {
logger.Error("failed to add file to inotify instance", zap.Error(err))
}
secrets[dataDir] = disk.MountPath
}
}

// Wait a bit to reduce the chance we attempt dialing before
// QEMU is started
// Faster loop for the initial upload.
// The VM might need the secrets in order for postgres to actually start up,
// so it's important we sync them as soon as the daemon is available.
for {
select {
case <-time.After(1 * time.Second):
case <-ctx.Done():
logger.Warn("QEMU shut down too soon to start forwarding logs")
}

success := true
for hostpath, guestpath := range secrets {
if err := sendFilesToNeonvmDaemon(ctx, hostpath, guestpath); err != nil {
Expand All @@ -712,58 +696,45 @@ func monitorFiles(ctx context.Context, logger *zap.Logger, wg *sync.WaitGroup, d
if success {
break
}

select {
case <-time.After(1 * time.Second):
case <-ctx.Done():
logger.Warn("QEMU shut down too soon to start forwarding logs")
}
}

ticker := time.NewTicker(5 * time.Minute)
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
case event := <-notify.Events:
guestpath, ok := secrets[event.Name]
if !ok {
// not tracking this file
// this can occur due to recursive file tracking
continue
}

// kubernetes secrets are mounted as symbolic links.
// When the link changes, there's no event. We only see the deletion
// of the file the link used to point to.
// This doesn't mean the file was actually deleted though.
if event.Op != fsnotify.Remove {
continue
}

logger.Info("mounted secrets changed", zap.String("secret_path", event.Name))
if err := notify.Add(event.Name); err != nil {
logger.Error("failed to add file to inotify instance", zap.Error(err))
}

if err := sendFilesToNeonvmDaemon(ctx, event.Name, guestpath); err != nil {
logger.Error("failed to upload file to vm guest", zap.Error(err))
}
case <-ticker.C:
// for each secret we are tracking
for hostpath, guestpath := range secrets {
// get the checksum for the pod directory
hostsum, err := util.ChecksumFlatDir(hostpath)
if err != nil {
logger.Error("failed to get file checksum from host", zap.Error(err))
logger.Error("failed to get dir checksum from host", zap.Error(err), zap.String("dir", hostpath))
continue
}

// get the checksum for the VM directory
guestsum, err := getFileChecksumFromNeonvmDaemon(ctx, guestpath)
if err != nil {
logger.Error("failed to get file checksum from guest", zap.Error(err))
logger.Error("failed to get dir checksum from guest", zap.Error(err), zap.String("dir", guestpath))
continue
}

// if not equal, update the files inside the VM.
if guestsum != hostsum {
if err = sendFilesToNeonvmDaemon(ctx, hostpath, guestpath); err != nil {
logger.Error("failed to upload file to vm guest", zap.Error(err))
logger.Error("failed to upload files to vm guest", zap.Error(err))
}
}
}
continue
}
}
}
Expand Down

0 comments on commit 546f869

Please sign in to comment.