From fa0f2a263a3966ef3926318c902b9e289af9260a Mon Sep 17 00:00:00 2001 From: Ethan Mosbaugh Date: Wed, 9 Oct 2024 14:10:58 -0500 Subject: [PATCH] Revert "Revert "feat: run blob reaper every 72h" (#158)" This reverts commit 4366d098e02fbcb1d4cef59d095371ff5f0c117d. --- .github/workflows/main.yaml | 11 ++++++- kustomize/overlays/dev/registry.yaml | 47 +++++++--------------------- okteto.yml | 8 +++++ registry/Dockerfile | 7 +++-- registry/Dockerfile.blob-reap | 12 +++++++ registry/entrypoint.sh | 26 +++++++-------- registry/garbage-collect.sh | 5 ++- 7 files changed, 61 insertions(+), 55 deletions(-) create mode 100644 registry/Dockerfile.blob-reap diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index da38ab6..867ba05 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -11,7 +11,7 @@ jobs: env: HEROKU_API_KEY: ${{secrets.HEROKU_API_KEY}} steps: - - name: Checkout + - name: checkout uses: actions/checkout@v2 - name: install heroku cli @@ -55,3 +55,12 @@ jobs: - name: release reaper run: heroku container:release -a ttlsh-hooks reap + + - name: build blob reaper + run: docker build -f registry/Dockerfile.blob-reap -t registry.heroku.com/ttlsh-hooks/blob-reap registry + + - name: push blob reaper + run: docker push registry.heroku.com/ttlsh-hooks/blob-reap + + - name: release blob reaper + run: heroku container:release -a ttlsh-hooks blob-reap diff --git a/kustomize/overlays/dev/registry.yaml b/kustomize/overlays/dev/registry.yaml index 8d8aded..27beef5 100644 --- a/kustomize/overlays/dev/registry.yaml +++ b/kustomize/overlays/dev/registry.yaml @@ -16,19 +16,13 @@ spec: spec: containers: - name: ttl-registry - image: registry:2 - command: ["/entrypoint.sh"] + image: ttl-registry + command: ["registry", "serve", "/etc/docker/registry/config.yml"] 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 @@ -42,40 +36,21 @@ spec: path: / port: 5000 scheme: HTTP + - name: ttl-blob-reap + image: ttl-blob-reap + command: ["/garbage-collect.sh"] + volumeMounts: + - name: registry-data + mountPath: /var/lib/registry + - name: registry-config + mountPath: /etc/docker/registry/config.yml + subPath: config.yml volumes: - name: registry-data emptyDir: {} - name: registry-config configMap: 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 - # /garbage-collect.sh & - registry serve /etc/docker/registry/config.yml - garbage-collect.sh: | - #!/bin/sh - while true; do - sleep 1m - if [ ! -d "/var/lib/registry/docker" ]; then - echo "No registry data found, skipping garbage collection" - else - echo "Starting garbage collection..." - registry garbage-collect /etc/docker/registry/config.yml || true - fi - done --- apiVersion: v1 kind: ConfigMap diff --git a/okteto.yml b/okteto.yml index 2a8d697..ed270e9 100644 --- a/okteto.yml +++ b/okteto.yml @@ -1,4 +1,10 @@ build: + ttl-registry: + context: ./registry + dockerfile: ./registry/Dockerfile + ttl-blob-reap: + context: ./registry + dockerfile: ./registry/Dockerfile.blob-reap ttl-hooks: context: ./hooks dockerfile: ./hooks/Dockerfile.hooks @@ -7,6 +13,8 @@ build: dockerfile: ./hooks/Dockerfile.reap deploy: + - cd kustomize/overlays/dev && kustomize edit set image ttl-registry=${OKTETO_BUILD_TTL_REGISTRY_IMAGE} + - cd kustomize/overlays/dev && kustomize edit set image ttl-blob-reap=${OKTETO_BUILD_TTL_BLOB_REAP_IMAGE} - cd kustomize/overlays/dev && kustomize edit set image ttl-hooks=${OKTETO_BUILD_TTL_HOOKS_IMAGE} - cd kustomize/overlays/dev && kustomize edit set image ttl-reaper=${OKTETO_BUILD_TTL_REAPER_IMAGE} diff --git a/registry/Dockerfile b/registry/Dockerfile index ed08947..6f04931 100644 --- a/registry/Dockerfile +++ b/registry/Dockerfile @@ -1,8 +1,11 @@ FROM registry:2.7.1 ADD ./entrypoint.sh /entrypoint.sh -ADD ./config.yml /etc/docker/registry/config.yml +ADD ./config.yml /etc/docker/registry/config.tmpl.yml ADD ./garbage-collect.sh /garbage-collect.sh +# entrypoint.sh will copy config.tmpl.yml to config.yml and replace variables +RUN rm -f /etc/docker/registry/config.yml + ENTRYPOINT ["/entrypoint.sh"] -CMD ["/etc/docker/registry/config.yml"] +CMD ["registry", "serve", "/etc/docker/registry/config.yml"] diff --git a/registry/Dockerfile.blob-reap b/registry/Dockerfile.blob-reap new file mode 100644 index 0000000..ac9b414 --- /dev/null +++ b/registry/Dockerfile.blob-reap @@ -0,0 +1,12 @@ +# built from https://github.com/sgalsaleh/distribution/tree/custom-garbage-collect +FROM replicated/registry-blob-reap:latest + +ADD ./entrypoint.sh /entrypoint.sh +ADD ./config.yml /etc/docker/registry/config.tmpl.yml +ADD ./garbage-collect.sh /garbage-collect.sh + +# entrypoint.sh will copy config.tmpl.yml to config.yml and replace variables +RUN rm -f /etc/docker/registry/config.yml + +ENTRYPOINT ["/entrypoint.sh"] +CMD ["/garbage-collect.sh"] diff --git a/registry/entrypoint.sh b/registry/entrypoint.sh index fb74653..fcdd0fe 100755 --- a/registry/entrypoint.sh +++ b/registry/entrypoint.sh @@ -2,25 +2,21 @@ set -e -sed -i "s/__PORT__/$PORT/g" /etc/docker/registry/config.yml -sed -i "s/__HOOK_TOKEN__/$HOOK_TOKEN/g" /etc/docker/registry/config.yml -sed -i "s/__HOOK_URI__/$HOOK_URI/g" /etc/docker/registry/config.yml -sed -i "s/__REPLREG_HOST__/$REPLREG_HOST/g" /etc/docker/registry/config.yml -sed -i "s/__REPLREG_SECRET__/$REPLREG_SECRET/g" /etc/docker/registry/config.yml +# in dev we mount this file into the container, so we can ignore the template +if [ ! -f /etc/docker/registry/config.yml ]; then + cp /etc/docker/registry/config.tmpl.yml /etc/docker/registry/config.yml + sed -i "s/__PORT__/$PORT/g" /etc/docker/registry/config.yml + sed -i "s/__HOOK_TOKEN__/$HOOK_TOKEN/g" /etc/docker/registry/config.yml + sed -i "s/__HOOK_URI__/$HOOK_URI/g" /etc/docker/registry/config.yml + sed -i "s/__REPLREG_HOST__/$REPLREG_HOST/g" /etc/docker/registry/config.yml + sed -i "s/__REPLREG_SECRET__/$REPLREG_SECRET/g" /etc/docker/registry/config.yml +fi -if [[ -z "${GCS_KEY_ENCODED}" ]]; then +if [ -z "${GCS_KEY_ENCODED}" ]; then echo "Set GCS_KEY_ENCODED variable" else - echo ${GCS_KEY_ENCODED} | base64 -d > /etc/gcs.json + echo "${GCS_KEY_ENCODED}" | base64 -d > /etc/gcs.json chmod 0400 /etc/gcs.json fi -# Run garbage collection job in background -# /garbage-collect.sh & - -case "$1" in - *.yaml|*.yml) set -- registry serve "$@" ;; - serve|garbage-collect|help|-*) set -- registry "$@" ;; -esac - exec "$@" diff --git a/registry/garbage-collect.sh b/registry/garbage-collect.sh index 93c424a..40c02dd 100755 --- a/registry/garbage-collect.sh +++ b/registry/garbage-collect.sh @@ -1,7 +1,10 @@ #!/bin/sh +echo "Script started" + while true; do - sleep 1m echo "Starting garbage collection..." registry garbage-collect /etc/docker/registry/config.yml || true + echo "Garbage collection finished" + sleep 72h # every 3 days done