How to clean up Tanzu Build Service(TBS) historical builds.
search cancel

How to clean up Tanzu Build Service(TBS) historical builds.

book

Article ID: 297258

calendar_today

Updated On:

Products

VMware Tanzu Build Service

Issue/Introduction

TBS does not delete the last ten successful builds and the last ten failed builds for the purpose of providing historical logging and debugging, thus storage usage of the image repository could potentially expand significantly.


Environment

Product Version: 1.9

Resolution

If you want to change the behavior, you can refer FAQ to configure build history limit, or follow this KB to clean up historical builds. 

Usage

1. Make sure you are logged into your harbor repository from the command line (i.e. `docker login
   my-harbor.example.com`)
2. Make sure you are logged into your kubernetes cluster where TBS is running
3. Download the CLI from Tanzu Network.
4. Run the CLI. The examples here will use the short form `cleanup-builder`, replace this with the
   `cleanup-builder-OS-ARCH` for your operating system and architecture.

sh cleanup-builder --builder-history BUILDER-LIMIT REPO

     Where:
    - `REPO` is the `buildservice.kp_default_repository` on your `tap-values.yaml` or `tbs-values.yaml`
    - `BUILDER-LIMIT` is how many builder tags you want to keep (default is 5)
    (Optional) Use the `--dry-run` command to preview which tags will be kept

sh cleanup-builder --dry-run --builder-history BUILDER-LIMIT REPO

5. The expected output should look something like:

    Retrieving tags...
    275 tags
    Resolving digests...
     100% |...| (262/262, 33 it/s)
    Filtering tags...
    262 tags to delete and 13 tags to keep:
    - clusterbuilder-base
    - clusterbuilder-base-jammy
    - clusterbuilder-default
    - clusterbuilder-full
    - clusterbuilder-full-jammy
    - clusterbuilder-tiny
    - clusterbuilder-tiny-jammy
    - latest
    - 20240305150617
    - 20240305150625
    - 20240305150638
    - 20240305150654
    - 20240305150655
    Filtering in use images...
    25 images in use by ClusterStore or ClusterStack
    45 unique digests to delete
    Deleting 45 digests:
     100% |...| (45/45, 120000 it/s)
    Done


Technical details
When the CLI is invoked, it goes through the following actions:
1. Retrieve the list of all tags for the repository
2. Filter out any tag not in the form of `YYYYMMDDhhmmss`
3. For those tags, filter out the most recent X depending on the `--builder-history`
4. For the remaining tags, resolve the tag to a digest (this is due to a quirk in Harbor where
   deleting by tags is not allowed)
5. Filter out the digests for the tags that will be kept
6. Query the cluster for image digests that are currently in use (by the ClusterStack, ClusterStore, etc) and filter those out
7. Go through the list and delete the digests
This means that at the end of the process, it is possible for more than the configured number of
builders to remain. This is because those tags share the same underlying digest as a remaining tag
(i.e. if `20200301123456` and `20240301123456` has the same digest, both tags will be retained even if
one of them satisifies the removal critera).

CLI documentation
cleanup-builder
Cleanup old builders generated by Tanzu Build Service

Synopsis
This tool will find and delete old builder images generated by Tanzu Build
Service. The number of builders to keep can be configured as well as the number
of concurrent requests allowed.

cleanup-builder REPOSITORY

Examples

cleanup-builder --builder-history 10 --parallelism 20 my-harbor.example.com/tbs/ci

Options

      --builder-history int   Number of builders to keep. (default 5)
      --dry-run               Simulate clean without deleting.
  -h, --help                  help for cleanup-builder
      --parallelism int       Number of concurrent API requests allowed at any time. (default 10)