Skip to content

Conversation

@gtannous-spec
Copy link

Add sa_file Change Detection for Authentication Secret Updates

Summary

Implement automatic detection of PTP authentication file (sa_file) changes to enable ptp4l process restarts without pod restarts when authentication secrets are updated.

Motivation

When PTP authentication secrets are updated in Kubernetes, the mounted sa_file content changes but linuxptp-daemon had no mechanism to detect this, requiring manual intervention or pod restarts to apply new authentication keys.

Changes

New Components

  • saFileInfo struct: Tracks authentication file path and hash for each profile
  • saFileTracker map: Thread-safe tracking of all sa_file paths across profiles
  • 5-second ticker: Periodic check for sa_file content changes

Modified Functions

  • Run(): Added sa_file monitoring ticker alongside existing ConfigMap change detection
  • applyNodePTPProfiles(): Calls updateSaFileTracking() after profiles are applied

New Functions

  • computeFileHash(): Generic SHA256 hash computation (works with any file content/format)
  • extractSaFileFromConf(): Parses sa_file path from ptp4lConf global section
  • checkSaFileChanges(): Compares current file hash with stored hash for all tracked files
  • updateSaFileTracking(): Initializes/updates tracking when profiles change

Behavior

  • Every 5 seconds, checks if tracked sa_file content changed (via SHA256 hash)
  • When change detected, triggers applyNodePTPProfiles() to restart ptp4l process
  • Uses same restart mechanism as ConfigMap changes (no pod restart)

Detection Timeline

  • Kubernetes kubelet sync: ~60 seconds (cluster setting)
  • Our check interval: 0-5 seconds (ticker polling)
  • Process restart: 3-5 seconds
  • Total delay: ~60-70 seconds for authentication changes to apply

Benefits

  • ✅ No pod restarts for authentication secret updates
  • ✅ Consistent with ConfigMap change handling
  • ✅ Generic (works with any authentication format)
  • ✅ Thread-safe for multiple profiles
  • ✅ Simple ticker-based implementation

@github-actions
Copy link

Thanks for your PR,
Best regards.

chronydProcessName, // there can be only one chronyd process in the system
}

// saFileInfo tracks authentication file information for a profile
Copy link
Collaborator

Choose a reason for hiding this comment

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

What specifically is sa short for in this context?

Copy link
Author

Choose a reason for hiding this comment

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

security association, sa_file is an option inside the ptp4lconf's global section where we can add a filepath to mount a specific secret inside the linuxptp-daemon-container.
basically I converted some of the code from the ptpconfig_controller here because the file watcher forces a restart on the pods in case a change is detected on the mounted secret, so instead we're having a restart on the ptp4l process.

}
}

return changed
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps you could return the new hash that way you don't have to recompute them again in the update function?

- Replace 5-second ticker with fsnotify event-driven monitoring
- Add fsnotify watcher initialization with graceful degradation
- Implement restart flood protection using atomic.Bool flag
- Filter fsnotify events for Write/Create and ignore hidden files
- Setup directory watches for Kubernetes symlink compatibility
- Optimize hash computation by reusing hashes for unchanged file paths
- Handle fsnotify channel closures and errors gracefully
- Add watcher cleanup on daemon shutdown
@github-actions github-actions bot removed the ok-to-test ok to test label Nov 26, 2025
}
// Filter for relevant events (Write, Create - Kubernetes atomic updates)
// Ignore events on temporary/hidden files
if event.Op&(fsnotify.Write|fsnotify.Create) == 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should delete also be handled as a change?

Copy link
Author

Choose a reason for hiding this comment

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

If you mean "deleting the sa_file" then the controller actually removes the volumeMount in case the user delete it from the ptp4lconf or manually from the file system of the container. which forces a restart on the daemonset.

case err, ok := <-watcherErrors:
// fsnotify watcher error
if !ok {
watcherErrors = nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should you recreate the watcher here?

Copy link
Author

Choose a reason for hiding this comment

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

we should keep recreate until watcher is not having error


// Setup fsnotify watch on directory (not file, due to Kubernetes symlinks)
if dn.saFileWatcher != nil {
dirPath := filepath.Dir(saFilePath)
Copy link
Collaborator

Choose a reason for hiding this comment

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

There should be location where all sa_files are mounted. This path should controlled by the ptp-operator so is known ahead of time.

Instead of relying on the ptpconfig it should just recursively watch all dirs within that path.

That way theirs no need to have this late binding approach it can just be there from the start.

}
glog.Info("PtpConfig change detected, restarting PTP processes")
dn.restartInProgress.Store(true)
err := dn.applyNodePTPProfiles()
Copy link
Collaborator

Choose a reason for hiding this comment

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

These are not run as goroutines so they are blocking meaning there is no need for this flag.

Copy link
Author

Choose a reason for hiding this comment

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

That's right, there is no concurrent access.. I will fix that

@josephdrichard josephdrichard added the ok-to-test ok to test label Nov 26, 2025
@github-actions github-actions bot removed the ok-to-test ok to test label Nov 26, 2025
- Add PTP_SEC_FOLDER constant for /etc/ptp-secret-mount/
- Watch security mount folder from daemon startup instead of per-profile
- Remove saFileTracker, saFileMutex, and saFileInfo (no longer needed)
- Remove checkSaFileChanges, updateSaFileTracking, extractSaFileFromConf
- Simplify fsnotify handler: any file change triggers PTP restart
- Eliminates late binding complexity since mount path is known ahead of time
…profiles

Add extractAuthSettingsForPhc2sys function to parse sa_file, spp, and active_key_id from ptp4lConf [global] section. For grandmaster clock types, automatically inject these authentication settings into the phc2sys configuration to ensure consistent TLV authentication across PTP processes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants