From b7c5c52fa2279a9ed271b45fdb3a4c5c1846c373 Mon Sep 17 00:00:00 2001 From: Roman Vynar Date: Thu, 1 Sep 2022 16:37:06 +0300 Subject: [PATCH] Implement purge_tags_keep_from_file option. Refactoring. --- config.go | 15 ++------------- registry/tasks.go | 35 +++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/config.go b/config.go index 9fc262c..d22672c 100644 --- a/config.go +++ b/config.go @@ -7,7 +7,6 @@ import ( "strings" "github.com/quiq/docker-registry-ui/registry" - "github.com/tidwall/gjson" "gopkg.in/yaml.v2" ) @@ -31,7 +30,7 @@ type configData struct { PurgeTagsKeepDays int `yaml:"purge_tags_keep_days"` PurgeTagsKeepCount int `yaml:"purge_tags_keep_count"` PurgeTagsKeepRegexp string `yaml:"purge_tags_keep_regexp"` - PurgeTagsKeepFile string `yaml:"purge_tags_keep_from_file"` + PurgeTagsKeepFromFile string `yaml:"purge_tags_keep_from_file"` PurgeTagsSchedule string `yaml:"purge_tags_schedule"` PurgeConfig *registry.PurgeTagsConfig @@ -77,17 +76,7 @@ func readConfig(configFile string) *configData { KeepDays: config.PurgeTagsKeepDays, KeepMinCount: config.PurgeTagsKeepCount, KeepTagRegexp: config.PurgeTagsKeepRegexp, + KeepFromFile: config.PurgeTagsKeepFromFile, } - if config.PurgeTagsKeepFile != "" { - if _, err := os.Stat(config.PurgeTagsKeepFile); os.IsNotExist(err) { - panic(err) - } - data, err := ioutil.ReadFile(config.PurgeTagsKeepFile) - if err != nil { - panic(err) - } - config.PurgeConfig.KeepTagsFromFile = gjson.ParseBytes(data) - } - return &config } diff --git a/registry/tasks.go b/registry/tasks.go index b608893..5c2f644 100644 --- a/registry/tasks.go +++ b/registry/tasks.go @@ -2,7 +2,9 @@ package registry import ( "fmt" + "io/ioutil" "math" + "os" "regexp" "sort" "time" @@ -11,11 +13,11 @@ import ( ) type PurgeTagsConfig struct { - DryRun bool - KeepDays int - KeepMinCount int - KeepTagRegexp string - KeepTagsFromFile gjson.Result + DryRun bool + KeepDays int + KeepMinCount int + KeepTagRegexp string + KeepFromFile string } type tagData struct { @@ -48,6 +50,23 @@ func (p timeSlice) Swap(i, j int) { // PurgeOldTags purge old tags. func PurgeOldTags(client *Client, config *PurgeTagsConfig) { logger := SetupLogging("registry.tasks.PurgeOldTags") + + var keepTagsFromFile gjson.Result + if config.KeepFromFile != "" { + if _, err := os.Stat(config.KeepFromFile); os.IsNotExist(err) { + logger.Warnf("Cannot open %s: %s", config.KeepFromFile, err) + logger.Error("Not purging anything!") + return + } + data, err := ioutil.ReadFile(config.KeepFromFile) + if err != nil { + logger.Warnf("Cannot read %s: %s", config.KeepFromFile, err) + logger.Error("Not purging anything!") + return + } + keepTagsFromFile = gjson.ParseBytes(data) + } + dryRunText := "" if config.DryRun { logger.Warn("Dry-run mode enabled.") @@ -88,8 +107,8 @@ func PurgeOldTags(client *Client, config *PurgeTagsConfig) { if config.KeepTagRegexp != "" { logger.Infof("Keeping tags matching regexp: %s", config.KeepTagRegexp) } - if config.KeepTagsFromFile.IsObject() { - logger.Infof("Keeping tags for repos from the file: %+v", config.KeepTagsFromFile) + if config.KeepFromFile != "" { + logger.Infof("Keeping tags for repos from the file: %+v", keepTagsFromFile) } purgeTags := map[string][]string{} keepTags := map[string][]string{} @@ -100,7 +119,7 @@ func PurgeOldTags(client *Client, config *PurgeTagsConfig) { // Prep the list of tags to preserve if defined in the file tagsFromFile := []string{} - for _, i := range config.KeepTagsFromFile.Get(repo).Array() { + for _, i := range keepTagsFromFile.Get(repo).Array() { tagsFromFile = append(tagsFromFile, i.String()) }