How mirakl/terraform-google-gcr-cleaner works
Overview:
Identifying the Main Components
• Terraform Module: The core of the project, represented as the orchestrator that provisions and configures all resources.
• Cloud Run Service: This is where the gcr-cleaner container is deployed. It is the main execution environment for the cleaner.
• Cloud Scheduler Job: Triggers the Cloud Run service at a scheduled time (as defined by the job schedule and configuration parameters).
• App Engine Application (Optional): Can be created conditionally to support the service’s runtime requirements.
• Service Accounts and IAM Bindings: Defined to grant proper permissions for the Cloud Run service, scheduler, and other resources.
• Google Container Registry / Google Artifact Registry: The target registries where cleanup operations are performed.
• Supporting Data Sources and Configuration Files: Such as variables, versions, and locals files which set up the Terraform module’s behavior.
• Repositories Configuration Inputs: Both "gcr_repositories" and "gar_repositories" are inputs that dictate which registries will be cleaned and under what conditions.
Relationships and Interactions
• The Terraform module (main.tf and related tf files) configures resources by reading input variables.
• The Cloud Scheduler job is configured to regularly trigger the Cloud Run service.
• The Cloud Run service runs the gcr-cleaner Docker image, performing the cleanup actions against the registries (GCR and GAR).
• IAM roles, service accounts, and associated IAM binding resources ensure that service accounts have permissions to access and modify the container registries, Cloud Run, and App Engine/Cloud Scheduler.
• Configuration files (e.g., api.tf, iam.tf, sa.tf, variables.tf) organize the setup, making the module adaptable across projects with different parameters (such as grace periods, tag filtering, and repository specifics).
Architectural Patterns and Design Principles
• Modularity: Inputs allow dynamic configuration of repository cleanups. Separate examples (complete and minimal) suggest modularity that suits various user adoption scenarios.
• Separation of Concerns: Distinct responsibilities are allocated: Cloud Run handles execution, Cloud Scheduler manages scheduling, and IAM/security is enforced via separate configuration files.
• Declarative Configuration: Each resource (such as google_cloud_run_service, google_cloud_scheduler_job) is defined declaratively and interacts based on explicit dependencies.
Relevant Technologies and Frameworks
• Terraform (with language version >=1.3.0) as the IaC tool.
• Google Cloud Platform (GCP) services, including Google Cloud Run, Cloud Scheduler, App Engine, Container Registry, and Artifact Registry.
• Docker containers (specifically the gcr-cleaner image) running on Cloud Run.
• IAM for permissions and security controls.