From 3b6e2591b86e65081845e1fe9c5f7c53ce6695ef Mon Sep 17 00:00:00 2001 From: Salah Al Saleh Date: Mon, 5 Feb 2024 13:16:27 -0800 Subject: [PATCH] Periodic image garbage collection (#129) * Add periodic image garbage collection --- kustomize/overlays/dev/registry.yaml | 37 ++++++++++++++++++++++++++-- registry/Dockerfile | 5 ++-- registry/entrypoint.sh | 4 +++ registry/garbage-collect.sh | 5 ++++ 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100755 registry/garbage-collect.sh diff --git a/kustomize/overlays/dev/registry.yaml b/kustomize/overlays/dev/registry.yaml index 5d2508b..edcf887 100644 --- a/kustomize/overlays/dev/registry.yaml +++ b/kustomize/overlays/dev/registry.yaml @@ -17,11 +17,18 @@ spec: containers: - name: ttl-registry image: registry:2 + command: ["/entrypoint.sh"] ports: - containerPort: 5000 volumeMounts: - name: registry-data mountPath: /var/lib/registry + - name: registry-scripts + mountPath: /entrypoint.sh + subPath: entrypoint.sh + - name: registry-scripts + mountPath: /garbage-collect.sh + subPath: garbage-collect.sh - name: registry-config mountPath: /etc/docker/registry/config.yml subPath: config.yml @@ -40,12 +47,38 @@ spec: emptyDir: {} - name: registry-config configMap: - name: registry-config + name: ttl-registry-config + - name: registry-scripts + configMap: + name: ttl-registry-scripts + defaultMode: 0755 +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: ttl-registry-scripts + labels: + app: ttl-registry +data: + entrypoint.sh: | + #!/bin/sh + set -e + echo "0 * * * * /garbage-collect.sh >> /proc/1/fd/1 2>&1" > /etc/crontabs/root + crond + registry serve /etc/docker/registry/config.yml + garbage-collect.sh: | + #!/bin/sh + set -e + if [ ! -d "/var/lib/registry/docker" ]; then + echo "No registry data found, skipping garbage collection" + exit 0 + fi + registry garbage-collect /etc/docker/registry/config.yml --- apiVersion: v1 kind: ConfigMap metadata: - name: registry-config + name: ttl-registry-config labels: app: ttl-registry data: diff --git a/registry/Dockerfile b/registry/Dockerfile index ebfd1f1..ed08947 100644 --- a/registry/Dockerfile +++ b/registry/Dockerfile @@ -1,7 +1,8 @@ FROM registry:2.7.1 -ADD ./entrypoint.sh /heroku-entrypoint.sh +ADD ./entrypoint.sh /entrypoint.sh ADD ./config.yml /etc/docker/registry/config.yml +ADD ./garbage-collect.sh /garbage-collect.sh -ENTRYPOINT ["/heroku-entrypoint.sh"] +ENTRYPOINT ["/entrypoint.sh"] CMD ["/etc/docker/registry/config.yml"] diff --git a/registry/entrypoint.sh b/registry/entrypoint.sh index 2054ebb..5824c50 100755 --- a/registry/entrypoint.sh +++ b/registry/entrypoint.sh @@ -15,6 +15,10 @@ else chmod 0400 /etc/gcs.json fi +# Run garbage collection job in background +echo "0 * * * * /garbage-collect.sh >> /proc/1/fd/1 2>&1" > /etc/crontabs/root +crond + case "$1" in *.yaml|*.yml) set -- registry serve "$@" ;; serve|garbage-collect|help|-*) set -- registry "$@" ;; diff --git a/registry/garbage-collect.sh b/registry/garbage-collect.sh new file mode 100755 index 0000000..2b102ac --- /dev/null +++ b/registry/garbage-collect.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +echo "Starting garbage collection..." + +registry garbage-collect /etc/docker/registry/config.yml