# apsaradb/polardb-stack-operator architecture

> Architecture diagram of the GitHub repository apsaradb/polardb-stack-operator, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/apsaradb/polardb-stack-operator
- Repository: https://github.com/apsaradb/polardb-stack-operator

## Overview

PolarDB Stack is a Kubernetes operator for managing PolarDB database clusters, with shared-storage clusters as its primary documented model and local-storage support as a separate mode. Users create or modify an MPDCluster resource; reconciliation enters the appropriate state machine and workflow, which carries out lifecycle actions through application services and domain models. The product also exposes a REST API and periodically synchronizes shared-cluster status. The graph preserves documented lifecycle operations; unsampled workflow implementations are represented by their existing directories, and their internal call paths are not inferred.

Main entry points: [`pkg/business/service/shared_storage_cluster.go`](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/service/shared_storage_cluster.go) (Shared cluster service), [`pkg/controllers/mpd/mpdcluster_controller.go`](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/controllers/mpd/mpdcluster_controller.go) (Cluster reconciler), [`pkg/business/service/local_storage_cluster.go`](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/service/local_storage_cluster.go) (Local cluster service), [`pkg/business/workflow/shared`](https://github.com/apsaradb/polardb-stack-operator/tree/master/pkg/business/workflow/shared) (Shared workflows)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_control["Kubernetes control"]
  node_cluster_crd["MPDCluster resource"]
  node_reconciler["Cluster reconciler"]
  node_rest["REST API<br/>[rest.go]"]
  node_monitor["Status sync monitor"]
end

subgraph group_lifecycle["Lifecycle workflows"]
  node_state_machine["State machines"]
  node_shared_workflows["Shared workflows"]
  node_local_workflows["Local workflows"]
  node_workflow_engine["Workflow managers"]
  node_step_base["Workflow step base<br/>[wf_step_base.go]"]
end

subgraph group_domain["Business services"]
  node_shared_service["Shared cluster service"]
  node_local_service["Local cluster service"]
  node_shared_domain["Shared cluster model"]
  node_local_domain["Local cluster model"]
end

subgraph group_runtime["Runtime adapters"]
  node_shared_repo["Shared cluster repository"]
  node_local_repo["Local cluster repository"]
  node_shared_pods["Shared pod manager"]
  node_local_pods["Local pod manager"]
end

node_user(("Cluster user"))
node_k8s["Kubernetes API"]
node_engine_cm["Cluster manager"]
node_storage["Shared storage"]

node_user -->|"creates or modifies"| node_cluster_crd
node_cluster_crd -->|"stored in"| node_k8s
node_k8s -->|"triggers reconciliation"| node_reconciler
node_reconciler -->|"enters state machine"| node_state_machine
node_reconciler -->|"checks recovery"| node_workflow_engine
node_reconciler -->|"selects shared mode"| node_shared_workflows
node_reconciler -->|"selects local mode"| node_local_workflows
node_shared_workflows -->|"creates workflow"| node_workflow_engine
node_shared_workflows -->|"grows storage"| node_shared_service
node_step_base -->|"provides service"| node_shared_service
node_shared_service -->|"operates on model"| node_shared_domain
node_local_service -->|"operates on model"| node_local_domain
node_shared_service -->|"updates cluster"| node_shared_repo
node_shared_service -->|"creates pods"| node_shared_pods
node_local_service -.->|"persists cluster"| node_local_repo
node_local_service -.->|"manages pods"| node_local_pods
node_shared_domain -->|"manages instances"| node_engine_cm
node_shared_pods -->|"mounts shared volumes"| node_storage
node_monitor -->|"syncs cluster state"| node_shared_service
node_user -.->|"requests operations"| node_rest

click node_cluster_crd "https://github.com/apsaradb/polardb-stack-operator/blob/master/apis/mpd/v1/mpdcluster_types.go"
click node_reconciler "https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/controllers/mpd/mpdcluster_controller.go"
click node_state_machine "https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/wfimpl/statemachine_instance.go"
click node_shared_workflows "https://github.com/apsaradb/polardb-stack-operator/tree/master/pkg/business/workflow/shared"
click node_local_workflows "https://github.com/apsaradb/polardb-stack-operator/tree/master/pkg/business/workflow/local"
click node_workflow_engine "https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/wfimpl/wf_manager_instance.go"
click node_step_base "https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/wfimpl/wf_step_base.go"
click node_shared_service "https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/service/shared_storage_cluster.go"
click node_local_service "https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/service/local_storage_cluster.go"
click node_shared_domain "https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/domain/shared_storage_cluster.go"
click node_local_domain "https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/domain/local_storage_cluster.go"
click node_shared_repo "https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/adapter/shared_storage_cluster_repository.go"
click node_local_repo "https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/adapter/local_storage_cluster_repository.go"
click node_shared_pods "https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/adapter/shared_storage_pod_manager.go"
click node_local_pods "https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/adapter/local_storage_pod_manager.go"
click node_rest "https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/bizapis/rest.go"
click node_monitor "https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/monitor/shared_storage_cluster_status_sync_monitor.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cluster_crd,node_reconciler,node_rest,node_monitor,node_user toneBlue
class node_state_machine,node_shared_workflows,node_local_workflows,node_workflow_engine,node_step_base,node_storage toneAmber
class node_shared_service,node_local_service,node_shared_domain,node_local_domain,node_k8s toneMint
class node_shared_repo,node_local_repo,node_shared_pods,node_local_pods toneRose
class node_engine_cm toneIndigo
```

## Components

### Kubernetes control

- [MPDCluster resource](https://github.com/apsaradb/polardb-stack-operator/blob/master/apis/mpd/v1/mpdcluster_types.go): `apis/mpd/v1/mpdcluster_types.go`
- [Cluster reconciler](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/controllers/mpd/mpdcluster_controller.go): `pkg/controllers/mpd/mpdcluster_controller.go`
- [REST API](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/bizapis/rest.go): `pkg/bizapis/rest.go`
- [Status sync monitor](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/monitor/shared_storage_cluster_status_sync_monitor.go): `pkg/business/monitor/shared_storage_cluster_status_sync_monitor.go`

### Lifecycle workflows

- [State machines](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/wfimpl/statemachine_instance.go): `pkg/wfimpl/statemachine_instance.go`
- [Shared workflows](https://github.com/apsaradb/polardb-stack-operator/tree/master/pkg/business/workflow/shared): `pkg/business/workflow/shared`
- [Local workflows](https://github.com/apsaradb/polardb-stack-operator/tree/master/pkg/business/workflow/local): `pkg/business/workflow/local`
- [Workflow managers](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/wfimpl/wf_manager_instance.go): `pkg/wfimpl/wf_manager_instance.go`
- [Workflow step base](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/wfimpl/wf_step_base.go): `pkg/wfimpl/wf_step_base.go`

### Business services

- [Shared cluster service](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/service/shared_storage_cluster.go): `pkg/business/service/shared_storage_cluster.go`
- [Local cluster service](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/service/local_storage_cluster.go): `pkg/business/service/local_storage_cluster.go`
- [Shared cluster model](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/domain/shared_storage_cluster.go): `pkg/business/domain/shared_storage_cluster.go`
- [Local cluster model](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/domain/local_storage_cluster.go): `pkg/business/domain/local_storage_cluster.go`

### Runtime adapters

- [Shared cluster repository](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/adapter/shared_storage_cluster_repository.go): `pkg/business/adapter/shared_storage_cluster_repository.go`
- [Local cluster repository](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/adapter/local_storage_cluster_repository.go): `pkg/business/adapter/local_storage_cluster_repository.go`
- [Shared pod manager](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/adapter/shared_storage_pod_manager.go): `pkg/business/adapter/shared_storage_pod_manager.go`
- [Local pod manager](https://github.com/apsaradb/polardb-stack-operator/blob/master/pkg/business/adapter/local_storage_pod_manager.go): `pkg/business/adapter/local_storage_pod_manager.go`

### Other components

- **Cluster user**
- **Kubernetes API**
- **Cluster manager**
- **Shared storage**

## Connections

- Cluster user → MPDCluster resource: creates or modifies
- MPDCluster resource → Kubernetes API: stored in
- Kubernetes API → Cluster reconciler: triggers reconciliation (evidence: `pkg/controllers/mpd/mpdcluster_controller.go`)
- Cluster reconciler → State machines: enters state machine (evidence: `pkg/controllers/mpd/mpdcluster_controller.go`)
- Cluster reconciler → Workflow managers: checks recovery (evidence: `pkg/controllers/mpd/mpdcluster_controller.go`)
- Cluster reconciler → Shared workflows: selects shared mode (evidence: `pkg/controllers/mpd/mpdcluster_controller.go`)
- Cluster reconciler → Local workflows: selects local mode (evidence: `pkg/controllers/mpd/mpdcluster_controller.go`)
- Shared workflows → Workflow managers: creates workflow (evidence: `pkg/business/workflow/shared/extend_storage.go`)
- Shared workflows → Shared cluster service: grows storage (evidence: `pkg/business/workflow/shared/extend_storage.go`)
- Workflow step base → Shared cluster service: provides service (evidence: `pkg/wfimpl/wf_step_base.go`)
- Shared cluster service → Shared cluster model: operates on model (evidence: `pkg/business/service/shared_storage_cluster.go`)
- Local cluster service → Local cluster model: operates on model (evidence: `pkg/business/service/local_storage_cluster.go`)
- Shared cluster service → Shared cluster repository: updates cluster (evidence: `pkg/business/service/shared_storage_cluster.go`)
- Shared cluster service → Shared pod manager: creates pods (evidence: `pkg/business/service/shared_storage_cluster.go`)
- Local cluster service → Local cluster repository: persists cluster
- Local cluster service → Local pod manager: manages pods
- Shared cluster model → Cluster manager: manages instances (evidence: `pkg/business/domain/shared_storage_cluster.go`)
- Shared pod manager → Shared storage: mounts shared volumes (evidence: `pkg/business/adapter/shared_storage_pod_manager.go`)
- Status sync monitor → Shared cluster service: syncs cluster state (evidence: `pkg/business/monitor/shared_storage_cluster_status_sync_monitor.go`)
- Cluster user → REST API: requests operations
