diff --git a/README.md b/README.md index bf21147..c1d927e 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ We run our own private registry on a server with limited storage and it was only URL of registry (default "http://localhost:5000") -repo string matching repositories (allows regexp) (default ".*") +-nrepo string + matching everything but this repo (allows regexp) (default empty) -repos int number of repositories to garbage collect (default 5) -tag string diff --git a/main.go b/main.go index 419324a..8a66c33 100644 --- a/main.go +++ b/main.go @@ -32,7 +32,7 @@ var ( // Base URL of registry registryURL *string // Regexps for filtering repositories and tags - repoRegexpStr, tagRegexpStr, negTagRegexpStr *string + repoRegexpStr, negRepoRegexpStr, tagRegexpStr, negTagRegexpStr *string // Maximum age of image to consider for deletion day, month, year *int // Max number of repositories to be fetched from registry @@ -47,7 +47,7 @@ var ( ver *bool // Compiled regexps - repoRegexp, tagRegexp, negTagRegexp *regexp.Regexp + repoRegexp, negRepoRegexp, tagRegexp, negTagRegexp *regexp.Regexp // Skip insecure TLS insecure *bool // Username and password @@ -72,6 +72,8 @@ func init() { year = flag.Int("year", 0, "max age in days") // Regexp for images (default = .*) repoRegexpStr = flag.String("repo", ".*", "matching repositories (allows regexp)") + // Negative regexp for images (default = empty) + negRepoRegexpStr = flag.String("nrepo", "", "matching repositories (allows regexp)") // Regexp for tags (default = .*) tagRegexpStr = flag.String("tag", ".*", "matching tags (allows regexp)") // Negative regexp for tags (default = empty) @@ -101,6 +103,9 @@ func main() { // Compile regular expressions repoRegexp = regexp.MustCompile(*repoRegexpStr) + if *negRepoRegexpStr != "" { + negRepoRegexp = regexp.MustCompile(*negRepoRegexpStr) + } tagRegexp = regexp.MustCompile(*tagRegexpStr) if *negTagRegexpStr != "" { negTagRegexp = regexp.MustCompile(*negTagRegexpStr) @@ -165,6 +170,15 @@ func main() { continue } + // Check for the negative regexp for repos as well. + if negRepoRegexp != nil { + negRepoMatch := negRepoRegexp.MatchString(entry) + if negRepoMatch { + logger.WithFields(log.Fields{"entry": entry}).Debug("Ignore excluded repository (-nrepo=", *negRepoRegexpStr, ")") + continue + } + } + // Establish repository object in registry repoName, err := reference.WithName(entry)