Periodically deleting unused images in your Container Registry
- compute
- serverless
- FaaS
- containers
- container-registry
- Python
- Urllib
Overview
With intensive use, a Container Registry can quickly become cluttered and store several outdated images. This tutorial will show you how to periodically remove older images with a specific tag using Serverless Functions, the Scaleway console, and Python’s Urllib.
You may need certain IAM permissions to carry out some actions described on this page. This means:
- you are the Owner of the Scaleway Organization in which the actions will be carried out, or
- you are an IAM user of the Organization, with a policy granting you the necessary permission sets
- You have an account and are logged into the Scaleway console and have access to Registry and Functions
- You have created a Serverless Functions Namespace
- You have generated an API key
Deleting unused images
-
Log into the Scaleway console.
-
Click Functions in the Serverless section of the side menu. The Functions overview page displays.
-
Click the Functions namespace you want to use to create your Function.
-
Click Create a Function. The Function creation wizard displays.
-
From the first drop-down menu, choose to use the online code editor to edit your code directly from your browser in the box provided.
-
From the second drop-down menu, choose
python3
as your runtime. -
Delete what automatically displays in the online code editor.
-
Type the following code into the online code editor:
import urllib.requestimport osimport jsonfrom datetime import datetime, timedelta#Configurationregion=os.environ['REGION']auth_token=os.environ['X-AUTH-TOKEN']tags_to_keep=int(os.environ['TAGS-TO-KEEP']) # you can also use information passed to the event objectuser_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'headers={'X-Auth-Token':auth_token, 'User-Agent': user_agent}def handle(event, context):images_list=[]browse=Truei=1# Get the list of all images on all pageswhile browse:url=f"https://api.scaleway.com/registry/v1/regions/{region}/images?page_size=100&order_by=created_at_asc&page="+str(i)req = urllib.request.Request(url, headers=headers)with urllib.request.urlopen(req) as response:res = response.read()img_list=eval(res)["images"]i=i+1if not img_list:browse=Falseelse:images_list.extend(img_list)tags_to_delete=[]# Get all tags by image to deletefor image in images_list:url=f"https://api.scaleway.com/registry/v1/regions/{region}/images/{image['id']}/tags?page_size=100&order_by=created_at_desc"req = urllib.request.Request(url, headers=headers)with urllib.request.urlopen(req) as response:res = response.read()tags=eval(res)["tags"]i=0for tag in tags:if i<tags_to_keep:i=i+1else:tags_to_delete.append(tag)#Delete tags and return their statusmsg=[]for tag in tags_to_delete:tag_id=tag['id']url=f"https://api.scaleway.com/registry/v1/regions/{region}/tags/{tag_id}"req = urllib.request.Request(url, headers=headers,method="DELETE")with urllib.request.urlopen(req) as response:res = response.read()msg.append(json.loads(res.decode("utf-8")))return {"body": {"message":msg},"statusCode": 200,} -
Choose a name for your Function. The name must only contain alphanumeric characters.
-
Choose the resources to be allocated to your Function at runtime. These define the performance characteristics of your Function.
-
Set your scaling preferences, or leave them at default values. The Scaleway platform autoscales the number of available Instances of your Function to match the incoming load, depending on the settings you define here.
-
Define the following Environment Variables for your Function:
Key Value REGION The Region in which your Registry is located. (eg: fr-par
)TAGS-TO-KEEP The number of Image tags you want to keep -
Define the following Secrets as secret environment variables for your Function:
Key Value X-AUTH-TOKEN Your API secret key -
Set the privacy policy to Private.
-
Deploy the Function by clicking Create a Function. The list of your functions displays.
Configuring a Trigger
A trigger allows you to invoke functions by handling events coming from other sources, such as CRON.
- Click on the name of your newly created function. The functions overview displays.
- Click the Triggers tab to display the triggers configuration of your function.
- Click Create a new trigger. A pop-up displays.
- Enter the details for your new trigger:
- Enter a name for the trigger.
- Select the CRON type.
- Enter the UNIX schedule for the CRON.
Note:
For example 0 2 * * * will make the function run every day at 2 am
- Paste your JSON arguments.
- Click Create new trigger to create it. The newly created trigger displays in the list of your triggers.
You can run your Function by reaching its endpoint. Click the Function Settings tab to do so.