# kong/kong-plugin architecture

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

- Interactive diagram: https://gitdiagram.com/kong/kong-plugin
- Repository: https://github.com/kong/kong-plugin

## Overview

##### Main Components

a) Kong Gateway Integration Layer
- How the plugin interfaces with Kong's core
- Plugin's lifecycle hooks
- Configuration management

b) Plugin Core Components
- Handler module (`handler.lua`)
- Schema module (`schema.lua`)
- Configuration validation layer

c) Testing Infrastructure
- Unit tests
- Integration tests
- Pongo test environment

**Layer 1: Kong Gateway (Parent System)**

**Layer 2: Plugin Registration & Configuration**

**Layer 3: Plugin Core Components**

**Layer 4: Testing & Development Infrastructure**

##### Specific Elements

a) Component Relationships:
- How the plugin handler interfaces with Kong's request/response lifecycle
- Configuration flow from schema validation to handler initialization
- Demonstrate test coverage interaction with components

- Blue: Kong core components
- Green: Plugin core modules
- Yellow: Configuration components
- Gray: Testing infrastructure

##### Key Interactions to Highlight

- Schema validation flow
- Request/response processing
- Plugin initialization sequence
- Test execution paths

##### Technical Details

- Lua module dependencies
- Version information
- Configuration schema flow
- Integration points with Kong's plugin system

##### Test coverage across components

Keep the abstraction level focused on the plugin's architecture while showing its integration points with Kong Gateway. This will help developers understand both the plugin's internal structure and its interaction with the larger Kong ecosystem.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles and classes
    classDef kong fill:#3B82F6,color:white
    classDef plugin fill:#22C55E,color:white
    classDef config fill:#EAB308,color:white
    classDef test fill:#6B7280,color:white
    classDef ci fill:#EC4899,color:white

    %% Kong Gateway Layer
    subgraph KongGateway["Kong Gateway System"]
        KongCore["Kong Core"]:::kong
        PluginSystem["Plugin System"]:::kong
    end

    %% Plugin Registration Layer
    subgraph PluginReg["Plugin Registration & Configuration"]
        PackageDef["Plugin Package Definition"]:::config
        PluginRoot["Plugin Integration Root"]:::plugin
    end

    %% Plugin Core Components
    subgraph PluginCore["Plugin Core Components"]
        Handler["Handler Module"]:::plugin
        Schema["Schema Module"]:::plugin
        ConfigVal["Configuration Validation"]:::config
    end

    %% Testing Infrastructure
    subgraph TestInfra["Testing & Development Infrastructure"]
        UnitTests["Unit Tests"]:::test
        IntegTests["Integration Tests"]:::test
        SchemaTests["Schema Validation Tests"]:::test
        PongoEnv["Pongo Test Environment"]:::test
    end

    %% CI/CD Infrastructure
    subgraph CICD["CI/CD Pipeline"]
        TestWorkflow["Test Workflow"]:::ci
        LintWorkflow["Lint Workflow"]:::ci
    end

    %% Relationships
    KongCore --> PluginSystem
    PluginSystem --> PluginRoot
    PluginRoot --> Handler
    PluginRoot --> Schema
    Schema --> ConfigVal
    Handler --> ConfigVal
    
    PackageDef --> PluginRoot
    
    UnitTests --> Handler
    UnitTests --> Schema
    IntegTests --> Handler
    SchemaTests --> Schema
    PongoEnv --> TestWorkflow
    
    TestWorkflow --> Handler
    TestWorkflow --> Schema
    LintWorkflow --> Handler
    LintWorkflow --> Schema

    %% Click events for component mapping
    click Handler "https://github.com/Kong/kong-plugin/blob/master/kong/plugins/myplugin/handler.lua"
    click Schema "https://github.com/Kong/kong-plugin/blob/master/kong/plugins/myplugin/schema.lua"
    click UnitTests "https://github.com/Kong/kong-plugin/blob/master/spec/myplugin/02-unit_spec.lua"
    click IntegTests "https://github.com/Kong/kong-plugin/blob/master/spec/myplugin/10-integration_spec.lua"
    click SchemaTests "https://github.com/Kong/kong-plugin/blob/master/spec/myplugin/01-schema_spec.lua"
    click PongoEnv "https://github.com/Kong/kong-plugin/tree/master/.pongo/pongorc"
    click TestWorkflow "https://github.com/Kong/kong-plugin/blob/master/.github/workflows/test.yml"
    click LintWorkflow "https://github.com/Kong/kong-plugin/blob/master/.github/workflows/lint.yml"
    click PackageDef "https://github.com/Kong/kong-plugin/blob/master/kong-plugin-myplugin-0.1.0-1.rockspec"
    click PluginRoot "https://github.com/Kong/kong-plugin/tree/master/kong/plugins/myplugin"

    %% Legend
    subgraph Legend["Legend"]
        L1["Kong Components"]:::kong
        L2["Plugin Components"]:::plugin
        L3["Configuration"]:::config
        L4["Testing"]:::test
        L5["CI/CD"]:::ci
    end
```
