Implement purge_tags_keep_from_file option. Refactoring.

This commit is contained in:
Roman Vynar 2022-09-01 16:37:06 +03:00
parent 6d0fc3d913
commit b7c5c52fa2
2 changed files with 29 additions and 21 deletions

View File

@ -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
}

View File

@ -2,7 +2,9 @@ package registry
import (
"fmt"
"io/ioutil"
"math"
"os"
"regexp"
"sort"
"time"
@ -15,7 +17,7 @@ type PurgeTagsConfig struct {
KeepDays int
KeepMinCount int
KeepTagRegexp string
KeepTagsFromFile gjson.Result
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())
}