# mysql/mysql-operator architecture

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

- Interactive diagram: https://gitdiagram.com/mysql/mysql-operator
- Repository: https://github.com/mysql/mysql-operator

## Overview

##### Main Components
- Kubernetes Cluster: The deployment target where the operator and MySQL instances run.
- Custom Resource Definitions (CRDs): Definitions (e.g., InnoDBCluster) that extend the Kubernetes API to represent MySQL cluster configurations.
- MySQL Operator: The core logic (located in the “mysqloperator” directory) that reacts to changes in the CRDs and controls MySQL cluster lifecycles.
- Controllers/Submodules: Within the operator, the folder structure (e.g., “controller/innodbcluster”, “controller/backup”, “controller/cloud”, etc.) reveals modular controllers managing distinct responsibilities such as backup operations, cluster initialization, logging, and cloud integrations.
- Helm Charts and Manifests: In the “helm” directory, you have two distinct charts – one for “mysql-operator” and one for “mysql-innodbcluster” – which simplify both the operator’s and clusters’ deployment. Additionally, there are Kubernetes manifest files (in “deploy”) for applying CRDs and deployment YAMLs using kubectl.
- Docker and Build/Deployment Scripts: Scripts like “build.sh”, “gen_dockerfile.sh”, and Dockerfiles in “docker-build” and “docker-deps” indicate containerization and build processes.
- Testing Infrastructure: A robust “tests” directory with unit, integration (CI/CD) and end-to-end test scenarios ensures that operator logic is verified against various cluster scenarios.

##### Relationships and Interactions
- The operator is deployed inside a Kubernetes namespace (typically “mysql-operator”) and continuously monitors CRDs (created via manifest or Helm) for custom resources such as InnoDBCluster.
- When a CRD is applied (for instance, defining a new MySQL cluster), the operator’s controllers are triggered. For example, the ‘innodbcluster’ controllers will:
- Validate the cluster configuration.
- Spawn and manage the individual MySQL Server and MySQL Router instances (each of which may be implemented as StatefulSets or Deployments).
- Manage backup operations and cloud/authentication aspects via the “backup” and “cloud” controllers.

##### Architectural Patterns and Design Principles
- Controller Pattern: The operator follows the Kubernetes controller/reconciler paradigm to continuously reconcile desired versus actual state.
- Modularity: With controllers split into different modules (e.g., backup, cloud, innodbcluster), the design promotes separation of concerns. Each module manages different aspects of the cluster lifecycle.
- Integration with Kubernetes APIs: The dynamic interactions with CRDs, Services, StatefulSets/Deployments, and external custom configurations are crucial.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Deployment Tools
    subgraph "Deployment Tools"
        A1["Kubectl Manifests"]:::manifest
        A2["Helm Chart: MySQL Operator"]:::helm
        A3["Helm Chart: MySQL InnoDBCluster"]:::helm
        A4["Build Scripts & Docker"]:::build
        A5["Testing Infrastructure"]:::test
    end

    %% Kubernetes Cluster Container
    subgraph "Kubernetes Cluster"
        B1["Custom Resource Definitions (CRDs)"]:::crd
        B2["MySQL Operator"]:::operator
        subgraph "MySQL Operator Components"
            C1["InnoDB Cluster Controller"]:::controller
            C2["Backup Controller"]:::controller
            C3["Cloud/Auth Controller"]:::controller
            C4["Utility Controllers"]:::controller
        end
        B3["MySQL Servers"]:::database
        B4["MySQL Router"]:::database
    end

    %% Relationships and Data Flow
    A1 -->|"applies"| B1
    A2 -->|"deploys"| B1
    A3 -->|"deploys"| B1

    B1 -->|"triggers"| B2
    B2 -->|"manages"| C1
    B2 -->|"manages"| C2
    B2 -->|"manages"| C3
    B2 -->|"supports"| C4

    B2 -->|"orchestrates"| B3
    B2 -->|"orchestrates"| B4

    A4 -->|"builds"| B2
    A5 -->|"tests"| B2

    %% Click Events
    click A1 "https://github.com/mysql/mysql-operator/blob/trunk/deploy/deploy-crds.yaml"
    click A2 "https://github.com/mysql/mysql-operator/tree/trunk/helm/mysql-operator/"
    click A3 "https://github.com/mysql/mysql-operator/tree/trunk/helm/mysql-innodbcluster/"
    click A4 "https://github.com/mysql/mysql-operator/blob/trunk/build.sh"
    click A5 "https://github.com/mysql/mysql-operator/tree/trunk/tests/"
    click B1 "https://github.com/mysql/mysql-operator/blob/trunk/helm/mysql-operator/crds/crd.yaml"
    click B2 "https://github.com/mysql/mysql-operator/tree/trunk/mysqloperator/"
    click C1 "https://github.com/mysql/mysql-operator/tree/trunk/mysqloperator/controller/innodbcluster/"
    click C2 "https://github.com/mysql/mysql-operator/tree/trunk/mysqloperator/controller/backup/"
    click C3 "https://github.com/mysql/mysql-operator/tree/trunk/mysqloperator/controller/cloud/"
    click C4 "https://github.com/mysql/mysql-operator/tree/trunk/mysqloperator/controller/"

    %% Styles
    classDef manifest fill:#ffdead,stroke:#8b4513,stroke-width:2px;
    classDef helm fill:#90ee90,stroke:#006400,stroke-width:2px;
    classDef build fill:#ffd700,stroke:#b8860b,stroke-width:2px;
    classDef test fill:#d3d3d3,stroke:#696969,stroke-width:2px;
    classDef crd fill:#add8e6,stroke:#00008b,stroke-width:2px;
    classDef operator fill:#ffa07a,stroke:#ff4500,stroke-width:2px;
    classDef controller fill:#ffb6c1,stroke:#ff69b4,stroke-width:2px;
    classDef database fill:#e0ffff,stroke:#008b8b,stroke-width:2px;
```
