Clean images in Harbor by policies
Clean images in Harbor by policies.
Image | Project | Repo | Tag |
---|---|---|---|
library/busybox:1.30.0 | library | busybox | 1.30.0 |
release/devops/tools:v1.0 | release | devops/tools | v1.0 |
Number policy will retain latest N tags for each repo and remove other old ones. Latest tags are determined by images’ creation time.
numberPolicy:
number: 5
This policy takes only one argument, the number of tags to retain.
Regex policy removes images that match the given repo and tag regex patterns. A tag will be removed only when following conditions are all satisfied:
Regex here are Golang
supported regex. For example .*
matches all.
regexPolicy:
repos: [".*"]
tags: [".*-alpha.*", "dev"]
The above policy config will remove tags from all repos that are ‘dev’ or contain ‘alpha’. For example,
This policy works depends on Harbor’s access log. It collects images that are recently touched (pull, push, delete), and remove all other images that are not touched recently. It takes a time in second to configure the time period.
notTouchedPolicy:
time: 604800
$ make image VERSION=latest
You can also pull one from DockerHub.
$ docker pull k8sdevops/harbor-cleaner:v0.4.0
# Host of the Harbor
host: https://dev.cargo.io
# Version of the Harbor, e.g. 1.7, 1.4.0
version: 1.7
# Admin account
auth:
user: admin
password: Pwd123456
# Projects list to clean images for, it you want to clean images for all
# projects, leave it empty.
projects: []
# Policy to clean images
policy:
# Policy type, e.g. "number", "regex", "recentlyNotTouched"
type: number
# Number policy: to retain the latest N tags for each repo
# This configure takes effect only when 'policy.type' is set to 'number'
numberPolicy:
number: 5
# Regex policy: only clean images that match the given repo patterns and tag patterns
# This configure takes effect only when 'policy.type' is set to 'regex'
regexPolicy:
# Regex to match repos, a repo will be regarded as matched when it matches any regex in the list
repos: [".*"]
# Regex to match tags, a tag will be regarded as matched when it matches any regex in the list
tags: [".*-alpha.*", "dev"]
# Recently not touched policy: clean images that not touched within the given time period
# This configure takes effect only when 'policy.type' is set to 'recentlyNotTouched'
notTouchedPolicy:
# Time in second that to check for images
time: 604800
# Tags that should be retained anyway, '?', '*' supported.
retainTags: []
# Trigger for the cleanup, if you only want to run cleanup once, remove the 'trigger' part or leave
# the 'trigger.cron' empty
trigger:
# Cron expression to trigger the cleanup, for example "0 0 * * *", leave it empty will disable the
# trigger and fallback to run cleanup once. Note: you may need to quote the cron expression with double quote.
# Time zone of the cron depends on the running environment, if run in docker container, it's UTC time.
cron:
# For Harbor version v1.9+, you should configure the XSRF protection. For other version, keep the default values.
xsrf:
# Refer to 'EnableXSRF' in Harbor config file 'common/config/core/app.conf'.
enabled: true
# Refer to 'XSRFKey' in Harbor config file 'common/config/core/app.conf'.
key: T20zVqpLbDDlQGVIiiwDtAAtsm8bSRjHBJSMyejG
In the policy part, exact one of numberPolicy
, regexPolicy
, notTouchedPolicy
should be configured according to the policy type.
$ docker run -it --rm \
-v <your-config-file>:/workspace/config.yaml \
k8sdevops/harbor-cleaner:latest --dryrun=true
$ docker run -it --rm \
-v <your-config-file>:/workspace/config.yaml \
k8sdevops/harbor-cleaner:latest
Configure the cron trigger and run harbor cleaner container in background.
# Trigger for the cleanup, if you only want to run cleanup once, remove the 'trigger' part or leave
# the 'trigger.cron' empty
trigger:
# Cron expression to trigger the cleanup, for example "0 0 * * *", leave it empty will disable the
# trigger and fallback to run cleanup once.
cron: 0 0 * * *
$ docker run -d --name=harbor-cleaner --rm \
-v <your-config-file>:/workspace/config.yaml \
k8sdevops/harbor-cleaner:latest
Harbor 2.x not supported yet.