2022-04-27 01:00:20 -05:00
//go:build scrub
// +build scrub
package extensions
import (
"time"
2024-01-31 23:34:07 -05:00
"zotregistry.dev/zot/pkg/api/config"
"zotregistry.dev/zot/pkg/extensions/scrub"
"zotregistry.dev/zot/pkg/log"
"zotregistry.dev/zot/pkg/scheduler"
"zotregistry.dev/zot/pkg/storage"
storageTypes "zotregistry.dev/zot/pkg/storage/types"
2022-04-27 01:00:20 -05:00
)
// 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
2023-12-08 03:05:02 -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
2022-09-23 00:27:56 -05:00
}
2023-09-26 13:02:11 -05:00
generator := & taskGenerator {
imgStore : storeController . DefaultStore ,
log : log ,
2022-09-23 00:27:56 -05:00
}
2023-09-26 13:02:11 -05:00
sch . SubmitGenerator ( generator , config . Extensions . Scrub . Interval , scheduler . LowPriority )
2022-09-23 00:27:56 -05:00
if config . Storage . SubPaths != nil {
for route := range config . Storage . SubPaths {
2023-09-26 13:02:11 -05:00
generator := & taskGenerator {
imgStore : storeController . SubStore [ route ] ,
log : log ,
2022-09-23 00:27:56 -05:00
}
2023-09-26 13:02:11 -05:00
sch . SubmitGenerator ( generator , config . Extensions . Scrub . Interval , scheduler . LowPriority )
2022-04-27 01:00:20 -05:00
}
}
} else {
2023-12-08 03:05:02 -05:00
log . Info ( ) . Msg ( "scrub config not provided, skipping scrub" )
2022-09-23 00:27:56 -05:00
}
}
type taskGenerator struct {
2023-05-26 13:08:19 -05:00
imgStore storageTypes . ImageStore
2022-09-23 00:27:56 -05:00
log log . Logger
lastRepo string
done bool
}
2024-02-01 12:15:53 -05:00
func ( gen * taskGenerator ) Name ( ) string {
return "ScrubGenerator"
}
2023-07-04 03:03:29 -05:00
func ( gen * taskGenerator ) Next ( ) ( scheduler . Task , error ) {
2022-09-23 00:27:56 -05:00
repo , err := gen . imgStore . GetNextRepository ( gen . lastRepo )
2023-09-01 12:54:39 -05:00
if err != nil {
2022-09-23 00:27:56 -05:00
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
}
2023-08-07 14:55:19 -05:00
func ( gen * taskGenerator ) IsReady ( ) bool {
return true
}
2022-09-23 00:27:56 -05:00
func ( gen * taskGenerator ) Reset ( ) {
gen . lastRepo = ""
gen . done = false
2022-04-27 01:00:20 -05:00
}