# mirakl/terraform-google-gcr-cleaner architecture

> Architecture diagram of the GitHub repository mirakl/terraform-google-gcr-cleaner, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mirakl/terraform-google-gcr-cleaner
- Repository: https://github.com/mirakl/terraform-google-gcr-cleaner

## Overview

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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Provisioning Layer
    subgraph "Provisioning Layer"
        TM["Terraform Module (gcr-cleaner)"]:::provisioning
        CF["Configuration Files"]:::provisioning
        IAM["Service Accounts & IAM Bindings"]:::security
    end

    %% Runtime Services
    subgraph "Runtime Services"
        CR["Cloud Run Service (gcr-cleaner container)"]:::runtime
    end

    %% Scheduling Layer
    subgraph "Scheduling Layer"
        CS["Cloud Scheduler Job (Cleanup Trigger)"]:::scheduling
        AE["App Engine Application (Optional)"]:::scheduling
    end

    %% Container Registries
    subgraph "Container Registries"
        GCR["Google Container Registry (GCR)"]:::registry
        GAR["Google Artifact Registry (GAR)"]:::registry
    end

    %% Inputs
    subgraph "Inputs"
        GCRI["GCR Repositories Input"]:::config
        GARI["GAR Repositories Input"]:::config
    end

    %% Relationships
    CF -->|"feeds"| TM
    GCRI -->|"input"| TM
    GARI -->|"input"| TM
    TM -->|"provisions"| CR
    TM -->|"configures"| CS
    TM -->|"optionally provisions"| AE
    CS -->|"triggers"| CR
    IAM --- CR
    IAM --- CS
    IAM --- AE
    CR -->|"cleans"| GCR
    CR -->|"cleans"| GAR

    %% Click Events
    click TM "https://github.com/mirakl/terraform-google-gcr-cleaner/blob/main/main.tf"
    click CR "https://github.com/mirakl/terraform-google-gcr-cleaner/blob/main/main.tf"
    click CS "https://github.com/mirakl/terraform-google-gcr-cleaner/blob/main/api.tf"
    click AE "https://github.com/mirakl/terraform-google-gcr-cleaner/blob/main/api.tf"
    click IAM "https://github.com/mirakl/terraform-google-gcr-cleaner/blob/main/sa.tf"
    click CF "https://github.com/mirakl/terraform-google-gcr-cleaner/blob/main/variables.tf"

    %% Styles
    classDef provisioning fill:#FFEDD8,stroke:#d35400,stroke-width:2px;
    classDef runtime fill:#D6EAF8,stroke:#3498db,stroke-width:2px;
    classDef scheduling fill:#D5F5E3,stroke:#27ae60,stroke-width:2px;
    classDef registry fill:#F9E79F,stroke:#f1c40f,stroke-width:2px;
    classDef security fill:#FADBD8,stroke:#e74c3c,stroke-width:2px;
    classDef config fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;
```
