2022-02-10 09:17:49 -05:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2022-03-24 07:49:51 -05:00
|
|
|
"context"
|
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
|
|
|
|
2022-02-10 09:17:49 -05:00
|
|
|
"zotregistry.io/zot/pkg/api"
|
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HotReloader struct {
|
|
|
|
watcher *fsnotify.Watcher
|
|
|
|
filePath string
|
|
|
|
ctlr *api.Controller
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHotReloader(ctlr *api.Controller, filePath string) (*HotReloader, error) {
|
|
|
|
// creates a new file watcher
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hotReloader := &HotReloader{
|
|
|
|
watcher: watcher,
|
|
|
|
filePath: filePath,
|
|
|
|
ctlr: ctlr,
|
|
|
|
}
|
|
|
|
|
|
|
|
return hotReloader, nil
|
|
|
|
}
|
|
|
|
|
2023-07-25 12:39:13 -05:00
|
|
|
func signalHandler(ctlr *api.Controller, sigCh chan os.Signal, ctx context.Context, cancel context.CancelFunc) {
|
|
|
|
select {
|
|
|
|
// if signal then shutdown
|
|
|
|
case sig := <-sigCh:
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ctlr.Log.Info().Interface("signal", sig).Msg("received signal")
|
|
|
|
|
|
|
|
// gracefully shutdown http server
|
|
|
|
ctlr.Shutdown() //nolint: contextcheck
|
|
|
|
|
|
|
|
close(sigCh)
|
|
|
|
// if reload then return
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initShutDownRoutine(ctlr *api.Controller, ctx context.Context, cancel context.CancelFunc) {
|
|
|
|
sigCh := make(chan os.Signal, 1)
|
|
|
|
|
|
|
|
go signalHandler(ctlr, sigCh, ctx, cancel)
|
|
|
|
|
|
|
|
// block all async signals to this server
|
|
|
|
signal.Ignore()
|
|
|
|
|
|
|
|
// handle SIGINT and SIGHUP.
|
|
|
|
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP)
|
|
|
|
}
|
|
|
|
|
2022-03-24 07:49:51 -05:00
|
|
|
func (hr *HotReloader) Start() context.Context {
|
2023-07-25 12:39:13 -05:00
|
|
|
reloadCtx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
|
2022-02-10 09:17:49 -05:00
|
|
|
done := make(chan bool)
|
2022-03-24 07:49:51 -05:00
|
|
|
|
2023-07-25 12:39:13 -05:00
|
|
|
initShutDownRoutine(hr.ctlr, reloadCtx, cancelFunc)
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
err := LoadConfiguration(newConfig, hr.filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("couldn't reload config, retry writing it.")
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2022-03-24 07:49:51 -05:00
|
|
|
// if valid config then reload
|
2023-07-25 12:39:13 -05:00
|
|
|
cancelFunc()
|
2022-02-10 09:17:49 -05:00
|
|
|
|
2022-03-24 07:49:51 -05:00
|
|
|
// create new context
|
2023-07-25 12:39:13 -05:00
|
|
|
reloadCtx, cancelFunc = context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
// init shutdown routine
|
|
|
|
initShutDownRoutine(hr.ctlr, reloadCtx, cancelFunc)
|
|
|
|
|
2022-03-24 07:49:51 -05:00
|
|
|
hr.ctlr.LoadNewConfig(reloadCtx, newConfig)
|
2022-02-10 09:17:49 -05:00
|
|
|
}
|
|
|
|
// watch for errors
|
|
|
|
case err := <-hr.watcher.Errors:
|
2023-04-27 21:44:22 -05:00
|
|
|
log.Error().Err(err).Str("config", hr.filePath).Msg("fsnotfy error while watching config")
|
2022-02-10 09:17:49 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := hr.watcher.Add(hr.filePath); err != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
log.Error().Err(err).Str("config", hr.filePath).Msg("error adding config file to FsNotify watcher")
|
2022-02-10 09:17:49 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-done
|
|
|
|
}()
|
2022-03-24 07:49:51 -05:00
|
|
|
|
|
|
|
return reloadCtx
|
2022-02-10 09:17:49 -05:00
|
|
|
}
|