GoogleContainerTools / kaniko

Build Container Images In Kubernetes
Apache License 2.0
14.93k stars 1.45k forks source link

Kaniko Cache Clear Auto #998

Open sjkeerthi opened 4 years ago

sjkeerthi commented 4 years ago

Hi By using the Kaniko in my GitLab to build docker using to push the docker image in Google Container Register with --cache=true --cache-ttl=8h and now I could see the cache folder all the layers.

Now my question is after 8 hours when I do the next build, It will create a new cache but It will be great that if we add this features to clear the cache.

Kaniko can check the cache-ttl value and then the next trigger up it can delete the old cache in the Google Container Registry.

tejal29 commented 4 years ago

The team is also working on local cache #923
@cvgw mentioned, we look into how we could use existing tools like gcloud to prune your registry.

sjkeerthi commented 4 years ago

Local cache :) Great, If in that case when people who build since I knew about the gitlab I am thinking will that reside inside the gitlab-runner.

If in that case do we need to consider to set runner config file /cache folder to access between the runner and job while kaniko execute.

mvn-srizzari commented 4 years ago

I've been looking into this and could also use a feature like it. Keeping the cache in the registry is ideal to speed up CI jobs. Perhaps the feature is best suited in the warmer rather than executor.

The best workaround I could come up with is to:

  1. List the tags of the layers in the cache prefix
  2. Build an argument list with the image name and the tags
  3. Run the gcloud container image delete against the arguments.

So for a repository named repo and image named image:

  1. gcloud container images list-tags gcr.io/repo/image/cache --format="value(TAGS)"
  2. gcloud container images delete gcr.io/repo/image/cache:tag1 gcr.io/repo/image/cache:tag2 gcr.io/repo/image/cache:tag3 ...

A column is returned with a timestamp, so you could add logic to only remove if it's older than X.

Here's what I did in the GitLab after_sript, which runs the majority of the commands in the docker:19:03.08 image (ash shell).


      NOW="$(date +%s)"
      THRESHOLD="15552000" # 6 months, in seconds

      CACHE_LIST="$(docker run -e NOW="${NOW}" -e THRESHOLD="${THRESHOLD}" --rm google/cloud-sdk:293.0.0-alpine gcloud container images list-tags "gcr.io/repo/image/cache" --format="value[separator=','](TAGS,TIMESTAMP)")"

      if [ ! -z "${CACHE_LIST}" ]; then
        for x in ${CACHE_LIST}; do
          tag="$(echo $x | awk -F ',' '{print $1}')"
          timestamp="$(echo $x | awk -F ',' '{print $2}' | tr 'T' ' ' | xargs -I "{}" date -d {} +%s)"
          date_diff="$((${NOW} - ${timestamp}))"

          if [ "${date_diff}" -ge "${THRESHOLD}" ]; then
            # Build the gcloud image argument list
            IMAGE_STRING="${IMAGE_STRING} gcr.io/repo/image/cache:${tag}"
          fi
        done

        if [ ! -z "${IMAGE_STRING}" ]; then
          docker run --rm google/cloud-sdk:293.0.0-alpine gcloud container images delete ${IMAGE_STRING} --force-delete-tags
        fi
      fi```
meyfa commented 2 years ago

The cache is clogging up our on-premise GitLab registry. This is posing a real problem, since our hard disk and backup storage are filling up quickly. Has this perhaps been implemented in the meantime, or are there any plans to work on it? We'd very much need this feature!

lappazos commented 2 years ago

I'm joining the request - would be ideal to have a way to clean the cache, so the GCP artifact registry /cache won't have files from several months ago (TTL is 2 weeks by default)

aaron-prindle commented 1 year ago

Wanted to note that GCP's Artifact Registry has now added the concept of a "Cleanup Policy" which could help here: https://cloud.google.com/artifact-registry/docs/repositories/cleanup-policy