def delete_old_tags(self): from azure.containerregistry import ( ContainerRegistryClient, ContainerRepositoryClient, TagOrder ) from azure.identity import DefaultAzureCredential # [START list_repositories] account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] credential = DefaultAzureCredential() client = ContainerRegistryClient(account_url, credential) for repository in client.list_repositories(): repository_client = ContainerRepositoryClient(account_url, repository, credential) # [END list_repositories] # [START list_tags] tag_count = 0 for tag in repository_client.list_tags(order_by=TagOrder.LAST_UPDATE_TIME_DESCENDING): tag_count += 1 if tag_count > 3: repository_client.delete_tag(tag.name) # [END list_tags] client.close()
def delete_images(self): # [START list_repository_names] audience = "https://management.azure.com" account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] credential = DefaultAzureCredential() client = ContainerRegistryClient(account_url, credential, audience=audience) for repository in client.list_repository_names(): print(repository) # [END list_repository_names] # [START list_manifest_properties] # Keep the three most recent images, delete everything else manifest_count = 0 for manifest in client.list_manifest_properties( repository, order_by=ManifestOrder.LAST_UPDATE_TIME_DESCENDING): manifest_count += 1 if manifest_count > 3: client.delete_manifest(repository, manifest.digest) # [END list_manifest_properties] client.close()
def list_tags(self): # Create a new ContainerRegistryClient audience = "https://management.azure.com" account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] credential = DefaultAzureCredential() client = ContainerRegistryClient(account_url, credential, audience=audience) manifest = client.get_manifest_properties("library/hello-world", "latest") print(manifest.repository_name + ": ") for tag in manifest.tags: print(tag + "\n") client.close()
def set_image_properties(self): # Create a new ContainerRegistryClient account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] audience = "https://management.azure.com" credential = DefaultAzureCredential() client = ContainerRegistryClient(account_url, credential, audience=audience) # [START update_manifest_properties] # Set permissions on the v1 image's "latest" tag client.update_manifest_properties("library/hello-world", "latest", can_write=False, can_delete=False) # [END update_manifest_properties] # After this update, if someone were to push an update to `myacr.azurecr.io\hello-world:v1`, it would fail. # It's worth noting that if this image also had another tag, such as `latest`, and that tag did not have # permissions set to prevent reads or deletes, the image could still be overwritten. For example, # if someone were to push an update to `<registry endpoint>\hello-world:latest` # (which references the same image), it would succeed. client.close()
def delete_old_tags(self): from azure.containerregistry import ContainerRegistryClient, TagOrder from azure.identity import DefaultAzureCredential # [START list_repository_names] account_url = os.environ["CONTAINERREGISTRY_ENDPOINT"] credential = DefaultAzureCredential() client = ContainerRegistryClient(account_url, credential) for repository in client.list_repository_names(): print(repository) # [END list_repository_names] # [START list_tag_properties] # Keep the three most recent tags, delete everything else tag_count = 0 tags = client.list_tag_properties( repository, order_by=TagOrder.LAST_UPDATE_TIME_DESCENDING) for tag in tags[3:]: client.delete_tag(repository, tag.name) # [END list_tag_properties] client.close()