# nccloud/watchtower architecture

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

- Interactive diagram: https://gitdiagram.com/nccloud/watchtower
- Repository: https://github.com/nccloud/watchtower

## Overview

##### Main Components
- Kubernetes Cluster & API Server: Represents the external environment where events occur.
- Operator Manager Process: Located in “cmd/manager/main.go”. This is the entry point that boots up the operator.
- Controller(s): Implemented in the core code (e.g., pkg/controller.go) and using the controller-runtime framework. They watch for events, filter them, and initiate processing.
- Configuration & Utilities: Found under pkg/common (e.g., config.go and util.go) which manage configuration parameters, environmental settings, and helper functions.
- Event Processing Pipeline: Conceptually, this covers the filtering logic (based on user-specified criteria), template rendering for messages (using provided templates), and the code that sends events to destinations.
- External Endpoints: These include services like Slack, custom APIs, or logging endpoints where events are pushed.

##### Relationships and Interactions
- The Kubernetes API Server emits events that are captured by the Operator’s controllers.
- The Controller fetches and processes events based on the Watcher CRD specifications.
- Processed events flow through a filtering and templating pipeline (detailed in configuration examples).
- After processing, events are sent (using specified HTTP methods and endpoints) to the configured external services.
- The operator is highly configurable via environment variables and CRD configurations (as referenced in the README).

##### Architectural Patterns & Design Principles
- Usage of the controller-runtime framework, which implies a reconciliation loop pattern typical for Kubernetes operators.
- Separation of concerns: distinct components handle configuration, event capture, event filtering, templating, and communication with external systems.
- Extensibility is built-in through CRDs allowing users to add varied strategies for watching different kinds of Kubernetes resources.
- Testability is evident in the file tree by the presence of extensive mocking directories (e.g., mocks for client interfaces and network layers).

##### Relevant Technologies and Frameworks
- Go Language (as indicated by go.mod and main.go).
- Kubernetes APIs and CRD concepts.
- The controller-runtime library (core to the operator’s behavior).
- Helm for deployment (as described in the README).
- Docker for containerization (Dockerfile is provided).

##### Extra Detail
- Since the project contains mocks and tests directories, you may add a note that robust testing and mockability are integral to the system’s quality assurance.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Layer
    subgraph "External Layer"
        API["Kubernetes API Server"]:::external
        END["External Endpoints (Slack,APIs,Elasticsearch)"]:::external
    end

    %% Operator Layer
    subgraph "Operator Layer"
        MANAGER["Operator Manager Process"]:::operator
        CONTROLLER["Controller"]:::operator
        CRD["Watcher CRD"]:::operator
        subgraph "Configuration & Utilities"
            CONFIG["Configuration"]:::config
            UTIL["Utilities"]:::config
        end
        PIPELINE["Event Processing Pipeline"]:::processing
    end

    %% Connections
    API -->|"sends_events"| MANAGER
    MANAGER -->|"initiates"| CONTROLLER
    CRD -->|"defines_criteria"| CONTROLLER
    CONTROLLER -->|"processes_event"| PIPELINE
    CONFIG -->|"provides_settings"| PIPELINE
    UTIL -->|"assists_processing"| PIPELINE
    PIPELINE -->|"sends_notifications"| END

    %% Click Events
    click CRD "https://github.com/nccloud/watchtower/blob/main/deploy/crds/cloud.spaceship.com_watchers.yaml"
    click MANAGER "https://github.com/nccloud/watchtower/blob/main/cmd/manager/main.go"
    click CONTROLLER "https://github.com/nccloud/watchtower/blob/main/pkg/controller.go"
    click CONFIG "https://github.com/nccloud/watchtower/blob/main/pkg/common/config.go"
    click UTIL "https://github.com/nccloud/watchtower/blob/main/pkg/common/util.go"

    %% Styles
    classDef external fill:#f9e79f,stroke:#d68910,stroke-width:2px;
    classDef operator fill:#aed6f1,stroke:#3498db,stroke-width:2px;
    classDef processing fill:#a9dfbf,stroke:#28b463,stroke-width:2px;
    classDef config fill:#f5cba7,stroke:#e67e22,stroke-width:2px;
```
