2022-04-27 01:00:20 -05:00
//go:build scrub
// +build scrub
package extensions
import (
2022-09-23 00:27:56 -05:00
"errors"
"io"
2022-04-27 01:00:20 -05:00
"time"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/extensions/scrub"
"zotregistry.io/zot/pkg/log"
2022-09-23 00:27:56 -05:00
"zotregistry.io/zot/pkg/scheduler"
2022-04-27 01:00:20 -05:00
"zotregistry.io/zot/pkg/storage"
)
// EnableScrubExtension enables scrub extension.
2022-09-23 00:27:56 -05:00
func EnableScrubExtension ( config * config . Config , log log . Logger , storeController storage . StoreController ,
sch * scheduler . Scheduler ,
) {
if config . Extensions . Scrub != nil &&
2022-09-27 20:06:50 -05:00
* config . Extensions . Scrub . Enable {
2022-09-23 00:27:56 -05:00
minScrubInterval , _ := time . ParseDuration ( "2h" )
2022-04-27 01:00:20 -05:00
2022-09-23 00:27:56 -05:00
if config . Extensions . Scrub . Interval < minScrubInterval {
config . Extensions . Scrub . Interval = minScrubInterval
2022-04-27 01:00:20 -05:00
2022-09-23 00:27:56 -05:00
log . Warn ( ) . Msg ( "Scrub interval set to too-short interval < 2h, changing scrub duration to 2 hours and continuing." ) //nolint:lll // gofumpt conflicts with lll
}
generator := & taskGenerator {
imgStore : storeController . DefaultStore ,
log : log ,
}
sch . SubmitGenerator ( generator , config . Extensions . Scrub . Interval , scheduler . LowPriority )
if config . Storage . SubPaths != nil {
for route := range config . Storage . SubPaths {
generator := & taskGenerator {
imgStore : storeController . SubStore [ route ] ,
log : log ,
}
sch . SubmitGenerator ( generator , config . Extensions . Scrub . Interval , scheduler . LowPriority )
2022-04-27 01:00:20 -05:00
}
}
} else {
2022-09-23 00:27:56 -05:00
log . Info ( ) . Msg ( "Scrub config not provided, skipping scrub" )
}
}
type taskGenerator struct {
imgStore storage . ImageStore
log log . Logger
lastRepo string
done bool
}
func ( gen * taskGenerator ) GenerateTask ( ) ( scheduler . Task , error ) {
repo , err := gen . imgStore . GetNextRepository ( gen . lastRepo )
if err != nil && ! errors . Is ( err , io . EOF ) {
return nil , err
2022-04-27 01:00:20 -05:00
}
2022-09-23 00:27:56 -05:00
if repo == "" {
gen . done = true
return nil , nil
}
gen . lastRepo = repo
return scrub . NewTask ( gen . imgStore , repo , gen . log ) , nil
}
func ( gen * taskGenerator ) IsDone ( ) bool {
return gen . done
}
func ( gen * taskGenerator ) Reset ( ) {
gen . lastRepo = ""
gen . done = false
2022-04-27 01:00:20 -05:00
}