# kedacore/keda architecture

> Architecture diagram of the GitHub repository kedacore/keda, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kedacore/keda
- Repository: https://github.com/kedacore/keda

## Overview

##### Main Components

a. Core Components:
- KEDA Operator (Metrics Server)
- KEDA Controller
- Webhook Server
- Metrics Adapter

b. Custom Resources:
- ScaledObject
- ScaledJob
- TriggerAuthentication
- ClusterTriggerAuthentication
- CloudEventSource

c. External Integration Points:
- Kubernetes API Server
- Horizontal Pod Autoscaler (HPA)
- Event Sources/Scalers (50+ supported scalers)

##### Component Relationships

- KEDA Operator → Kubernetes API Server (watches for custom resources)
- KEDA Controller → Scalers (connects to external services)
- Metrics Adapter → HPA (provides metrics)
- Webhook Server → Custom Resources (validates CRDs)

##### Data Flow

Following flows:

##### Architectural Patterns

Patterns:
- Operator Pattern (for Kubernetes integration)
- Event-Driven Architecture
- Metrics-Based Scaling
- Plugin Architecture (for scalers)

##### Detailed Component Breakdown

Create sections showing:
a. Control Plane:
**/cmd**
- /operator    → Main KEDA operator
- /adapter     → Metrics adapter
- /webhooks    → Validation webhooks

b. Core Logic:
**/pkg**
- /scaling     → Scaling logic
- /scalers     → Individual scalers
- /provider    → Provider implementations

c. API Definitions:
**/apis**
- /keda        → Custom resource definitions
- /eventing    → Event source definitions

##### Scaler plugin system

- Component types
- Communication patterns
- Authentication flows
- Metric paths
- Event flows

This will create a comprehensive system design that accurately represents KEDA's architecture while maintaining clarity and usefulness for understanding the system.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph KEDA["KEDA System"]
        direction TB
        
        subgraph Core["Core Components"]
            direction TB
            Operator["KEDA Operator"]:::core
            Controller["KEDA Controller"]:::core
            WebhookServer["Webhook Server"]:::core
            MetricsAdapter["Metrics Adapter"]:::core
        end

        subgraph CRDs["Custom Resources"]
            direction TB
            SO["ScaledObject"]:::crd
            SJ["ScaledJob"]:::crd
            TA["TriggerAuthentication"]:::crd
            CTA["ClusterTriggerAuthentication"]:::crd
            CES["CloudEventSource"]:::crd
        end

        subgraph Handlers["Core Logic"]
            direction TB
            ScalingLogic["Scaling Logic"]:::handler
            ScalersImpl["Scalers Implementation"]:::handler
            MetricsCollector["Metrics Collection"]:::handler
            AuthHandler["Authentication Handlers"]:::handler
            EventEmitter["Event Emitter"]:::handler
        end
    end

    subgraph External["External Components"]
        direction TB
        K8sAPI["Kubernetes API Server"]:::external
        HPA["Horizontal Pod Autoscaler"]:::external
        EventSources["Event Sources/Scalers"]:::external
    end

    %% Relationships
    Operator --> K8sAPI
    Controller --> EventSources
    MetricsAdapter --> HPA
    WebhookServer --> CRDs
    EventSources --> ScalersImpl
    ScalersImpl --> MetricsAdapter
    MetricsAdapter --> HPA
    HPA --> K8sAPI
    K8sAPI --> |Pod Scaling| EventSources
    AuthHandler --> EventSources
    EventEmitter --> K8sAPI

    %% Click Events
    click Operator "https://github.com/kedacore/keda/blob/main/cmd/operator/main.go"
    click WebhookServer "https://github.com/kedacore/keda/blob/main/cmd/webhooks/main.go"
    click MetricsAdapter "https://github.com/kedacore/keda/blob/main/cmd/adapter/main.go"
    click SO "https://github.com/kedacore/keda/blob/main/apis/keda/v1alpha1/scaledobject_types.go"
    click SJ "https://github.com/kedacore/keda/blob/main/apis/keda/v1alpha1/scaledjob_types.go"
    click TA "https://github.com/kedacore/keda/blob/main/apis/keda/v1alpha1/triggerauthentication_types.go"
    click CES "https://github.com/kedacore/keda/blob/main/apis/eventing/v1alpha1/cloudeventsource_types.go"
    click ScalingLogic "https://github.com/kedacore/keda/blob/main/pkg/scaling/scale_handler.go"
    click ScalersImpl "https://github.com/kedacore/keda/blob/main/pkg/scalers/scaler.go"
    click MetricsCollector "https://github.com/kedacore/keda/blob/main/pkg/metricscollector/metricscollectors.go"
    click AuthHandler "https://github.com/kedacore/keda/blob/main/pkg/scaling/resolver/scale_resolvers.go"
    click EventEmitter "https://github.com/kedacore/keda/blob/main/pkg/eventemitter/eventemitter.go"

    %% Styles
    classDef core fill:#2374e1,stroke:#164b94,color:#fff
    classDef crd fill:#ff9900,stroke:#b36b00,color:#fff
    classDef handler fill:#37b24d,stroke:#2f9641,color:#fff
    classDef external fill:#e64980,stroke:#a13359,color:#fff

    %% Legend
    subgraph Legend
        direction TB
        L1["Core Components"]:::core
        L2["Custom Resources"]:::crd
        L3["Handlers"]:::handler
        L4["External Systems"]:::external
    end
```
