# richardoc/kube-audit-rest architecture

> Architecture diagram of the GitHub repository richardoc/kube-audit-rest, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/richardoc/kube-audit-rest
- Repository: https://github.com/richardoc/kube-audit-rest

## Overview

##### Major Components
- Kubernetes API Server (external) – Sends AdmissionReview requests via a ValidatingWebhook configuration.
- kube-audit-rest Service – Represented as a containerized service deployed on Kubernetes.
- HTTP Listener Module – Within the service, this component listens for webhook requests over HTTPS (using TLS certificates specified through flags).
- Event Processor – Responsible for processing and filtering the incoming AdmissionReview requests.
- Audit Writer Subsystem – Contains different writers (e.g., disk_writer, stderr_writer, and common_writer) that handle how and where audit events are written.
- Metrics Exporter – Exposes application-level metrics (using the prometheus client) and Go runtime metrics.
- Logging – Uses structured logging (with zap) that is integrated into many system modules.

##### Interactions and Relationships
- The HTTP Listener forwards inbound requests to the Event Processor.
- The Event Processor then directs processed audit events to the Audit Writer.
- Simultaneously, metrics are recorded and exposed through the Metrics Exporter, and logging is carried out throughout the components.
- Optionally, include configuration and management flows (e.g., container images – “-distroless” or “-alpine” variants – are built with Dockerfiles and deployed via Kubernetes manifests).

##### Architectural Patterns and Design Principles
- Modular Design: The system is clearly broken into discrete internal packages (each having well-defined interfaces such as iaudit_writer.go, ievent_processor.go, etc.) making it easier to understand, test, and maintain.
- Interface-driven Development: Interfaces for audit writing, metrics, HTTP listening, and event processing allow swapping or extending functionality (e.g., choosing between disk or stderr logging).
- Containerization and Kubernetes-Native Deployment: Dockerfiles, Kubernetes YAML in the k8s directory, and examples for integration with Elastic Stack detail how the service should be built and deployed.
- Observability: Integrated Prometheus metrics and structured logging emphasize monitoring and observability as core design principles.
- TLS and Security: With certificate management (and associated examples), the design ensures secure communication between the Kubernetes API server and the service.

##### Detail Relevant Technologies, Frameworks, and Libraries
- Programming Language: Go (with dependencies managed via go.mod and go.sum).
- Logging: zap for structured logging.
- Metrics and Instrumentation: Prometheus client for metrics collection.
- Containerization: Docker (with both distroless and alpine Dockerfiles).
- Kubernetes Webhook Integration: Use of ValidatingWebhookConfiguration to capture mutating/creation events from Kubernetes.

## Diagram (Mermaid)

```mermaid
flowchart TB
    API["Kubernetes API Server"]:::external

    subgraph "kube-audit-rest Service"
        direction TB
        MB["kube-audit-rest Binary"]:::service
        HL["HTTP Listener (HTTPS)"]:::module
        EP["Event Processor"]:::module
        AW["Audit Writer Subsystem"]:::module
            subgraph "Audit Writers"
                direction TB
                Disk["Disk Writer"]:::writer
                Stderr["Stderr Writer"]:::writer
                Common["Common Writer"]:::writer
            end
        Metrics["Metrics Exporter"]:::metrics
        Log["Logging (zap)"]:::module

        MB --> HL
        HL --> EP
        EP --> AW
        EP --> Metrics
        HL -.-> Log
        EP -.-> Log
        AW -.-> Log
        Metrics -.-> Log
    end

    subgraph "Deployment & Configuration"
        direction TB
        Webhook1["ValidatingWebhookConfiguration"]:::config
        Webhook2["k8s/webhook.yaml"]:::config
        DockerAlpine["Dockerfile-alpine"]:::config
        DockerDistroless["Dockerfile-distroless"]:::config
    end

    API -->|"AdmissionReview"| HL
    MB -->|"deploy"| Webhook1
    MB -->|"deploy"| Webhook2
    MB -->|"build"| DockerAlpine
    MB -->|"build"| DockerDistroless

    click MB "https://github.com/richardoc/kube-audit-rest/blob/main/cmd/kube-audit-rest/main.go"
    click HL "https://github.com/richardoc/kube-audit-rest/tree/main/internal/http_listener"
    click EP "https://github.com/richardoc/kube-audit-rest/blob/main/internal/event_processor/event_processor_impl/event_processor_impl.go"
    click Disk "https://github.com/richardoc/kube-audit-rest/blob/main/internal/audit_writer/disk_writer/disk_writer.go"
    click Stderr "https://github.com/richardoc/kube-audit-rest/blob/main/internal/audit_writer/stderr_writer/stderr_writer.go"
    click Common "https://github.com/richardoc/kube-audit-rest/blob/main/internal/audit_writer/common_writer/common_writer.go"
    click Metrics "https://github.com/richardoc/kube-audit-rest/blob/main/internal/metrics/prometheus_metrics/prometheus_metrics.go"
    click Log "https://github.com/richardoc/kube-audit-rest/blob/main/internal/common/logger.go"
    click Webhook1 "https://github.com/richardoc/kube-audit-rest/blob/main/examples/full-elastic-stack/k8s/ValidatingWebhookConfiguration.yaml"
    click Webhook2 "https://github.com/richardoc/kube-audit-rest/blob/main/k8s/webhook.yaml"
    click DockerAlpine "https://github.com/richardoc/kube-audit-rest/tree/main/Dockerfile-alpine"
    click DockerDistroless "https://github.com/richardoc/kube-audit-rest/tree/main/Dockerfile-distroless"

    classDef external fill:#ffcccc,stroke:#d03030,stroke-width:2px;
    classDef service fill:#ccffcc,stroke:#339933,stroke-width:2px;
    classDef module fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef writer fill:#ffe6cc,stroke:#cc7a00,stroke-width:2px;
    classDef metrics fill:#e6ccff,stroke:#9933cc,stroke-width:2px;
    classDef config fill:#fff2cc,stroke:#d4a017,stroke-width:2px;
```
