2023-09-15 17:17:01 -05:00
|
|
|
package server
|
2022-02-10 09:17:49 -05:00
|
|
|
|
|
|
|
import (
|
2024-01-25 12:15:22 -05:00
|
|
|
"errors"
|
2023-07-25 12:39:13 -05:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2022-03-24 07:49:51 -05:00
|
|
|
|
2022-02-10 09:17:49 -05:00
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
"github.com/rs/zerolog/log"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2024-01-31 23:34:07 -05:00
|
|
|
"zotregistry.dev/zot/pkg/api"
|
|
|
|
"zotregistry.dev/zot/pkg/api/config"
|
2022-02-10 09:17:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type HotReloader struct {
|
2024-01-25 12:15:22 -05:00
|
|
|
watcher *fsnotify.Watcher
|
|
|
|
configPath string
|
|
|
|
ldapCredentialsPath string
|
|
|
|
ctlr *api.Controller
|
2022-02-10 09:17:49 -05:00
|
|
|
}
|
|
|
|
|
2024-01-25 12:15:22 -05:00
|
|
|
func NewHotReloader(ctlr *api.Controller, filePath, ldapCredentialsPath string) (*HotReloader, error) {
|
2022-02-10 09:17:49 -05:00
|
|
|
// creates a new file watcher
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hotReloader := &HotReloader{
|
2024-01-25 12:15:22 -05:00
|
|
|
watcher: watcher,
|
|
|
|
configPath: filePath,
|
|
|
|
ldapCredentialsPath: ldapCredentialsPath,
|
|
|
|
ctlr: ctlr,
|
2022-02-10 09:17:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return hotReloader, nil
|
|
|
|
}
|
|
|
|
|
2023-12-11 13:00:34 -05:00
|
|
|
func signalHandler(ctlr *api.Controller, sigCh chan os.Signal) {
|
2023-07-25 12:39:13 -05:00
|
|
|
// if signal then shutdown
|
2023-12-11 13:00:34 -05:00
|
|
|
if sig, ok := <-sigCh; ok {
|
2023-07-25 12:39:13 -05:00
|
|
|
ctlr.Log.Info().Interface("signal", sig).Msg("received signal")
|
|
|
|
|
|
|
|
// gracefully shutdown http server
|
|
|
|
ctlr.Shutdown() //nolint: contextcheck
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 13:00:34 -05:00
|
|
|
func initShutDownRoutine(ctlr *api.Controller) {
|
2023-07-25 12:39:13 -05:00
|
|
|
sigCh := make(chan os.Signal, 1)
|
|
|
|
|
2023-12-11 13:00:34 -05:00
|
|
|
go signalHandler(ctlr, sigCh)
|
2023-07-25 12:39:13 -05:00
|
|
|
|
|
|
|
// block all async signals to this server
|
|
|
|
signal.Ignore()
|
|
|
|
|
|
|
|
// handle SIGINT and SIGHUP.
|
|
|
|
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP)
|
|
|
|
}
|
|
|
|
|
2023-12-11 13:00:34 -05:00
|
|
|
func (hr *HotReloader) Start() {
|
2022-02-10 09:17:49 -05:00
|
|
|
done := make(chan bool)
|
2022-03-24 07:49:51 -05:00
|
|
|
|
2022-02-10 09:17:49 -05:00
|
|
|
// run watcher
|
|
|
|
go func() {
|
|
|
|
defer hr.watcher.Close()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// watch for events
|
|
|
|
case event := <-hr.watcher.Events:
|
|
|
|
if event.Op == fsnotify.Write {
|
|
|
|
log.Info().Msg("config file changed, trying to reload config")
|
|
|
|
|
|
|
|
newConfig := config.New()
|
|
|
|
|
2024-01-25 12:15:22 -05:00
|
|
|
err := LoadConfiguration(newConfig, hr.configPath)
|
2022-02-10 09:17:49 -05:00
|
|
|
if err != nil {
|
2023-12-08 03:05:02 -05:00
|
|
|
log.Error().Err(err).Msg("failed to reload config, retry writing it.")
|
2022-02-10 09:17:49 -05:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-01-25 12:15:22 -05:00
|
|
|
if hr.ctlr.Config.HTTP.Auth != nil && hr.ctlr.Config.HTTP.Auth.LDAP != nil &&
|
|
|
|
hr.ctlr.Config.HTTP.Auth.LDAP.CredentialsFile != newConfig.HTTP.Auth.LDAP.CredentialsFile {
|
|
|
|
err = hr.watcher.Remove(hr.ctlr.Config.HTTP.Auth.LDAP.CredentialsFile)
|
|
|
|
if err != nil && !errors.Is(err, fsnotify.ErrNonExistentWatch) {
|
|
|
|
log.Error().Err(err).Msg("failed to remove old watch for the credentials file")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = hr.watcher.Add(newConfig.HTTP.Auth.LDAP.CredentialsFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic().Err(err).Str("ldap-credentials-file", newConfig.HTTP.Auth.LDAP.CredentialsFile).
|
|
|
|
Msg("failed to watch ldap credentials file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 13:00:34 -05:00
|
|
|
// stop background tasks gracefully
|
|
|
|
hr.ctlr.StopBackgroundTasks()
|
2023-07-25 12:39:13 -05:00
|
|
|
|
2023-12-11 13:00:34 -05:00
|
|
|
// load new config
|
|
|
|
hr.ctlr.LoadNewConfig(newConfig)
|
2023-07-25 12:39:13 -05:00
|
|
|
|
2023-12-11 13:00:34 -05:00
|
|
|
// start background tasks based on new loaded config
|
|
|
|
hr.ctlr.StartBackgroundTasks()
|
2022-02-10 09:17:49 -05:00
|
|
|
}
|
|
|
|
// watch for errors
|
|
|
|
case err := <-hr.watcher.Errors:
|
2024-01-25 12:15:22 -05:00
|
|
|
log.Panic().Err(err).Str("config", hr.configPath).Msg("fsnotfy error while watching config")
|
2022-02-10 09:17:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-01-25 12:15:22 -05:00
|
|
|
if err := hr.watcher.Add(hr.configPath); err != nil {
|
|
|
|
log.Panic().Err(err).Str("config", hr.configPath).Msg("failed to add config file to fsnotity watcher")
|
|
|
|
}
|
|
|
|
|
|
|
|
if hr.ldapCredentialsPath != "" {
|
|
|
|
if err := hr.watcher.Add(hr.ldapCredentialsPath); err != nil {
|
|
|
|
log.Panic().Err(err).Str("ldap-credentials", hr.ldapCredentialsPath).
|
|
|
|
Msg("failed to add ldap-credentials to fsnotity watcher")
|
|
|
|
}
|
2022-02-10 09:17:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
<-done
|
|
|
|
}()
|
|
|
|
}
|