# saeed-mcu/netplan-operator architecture

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

- Interactive diagram: https://gitdiagram.com/saeed-mcu/netplan-operator
- Repository: https://github.com/saeed-mcu/netplan-operator

## Overview

##### Main Components
- Operator Controller: Highlight the main binary (cmd/main.go) that runs the control loop. This is where the operator logic (including reconciliation) lives.
- API and CRD Definitions: Represent the API (in api/v1) which defines the “NetplanConfig” type. Also include the generated deepcopy code and group/version info.
- Controller Implementation: Show the controller logic located in internal/controller, which handles changes to custom resources.
- Client & Helper Packages: Include the client package (pkg/client) and additional utility modules (pkg/config and pkg/file) that support core functionalities.
- Configuration and Manifests: Include the extensive configuration files in the config folder—CRDs under config/crd, RBAC settings in config/rbac, deployment configurations in config/manager, sample configurations under config/samples, and monitoring (prometheus & network policies). These files define how the operator is deployed and interacts with Kubernetes.
- Build and Deployment Artifacts: Note the presence of Dockerfile, Makefile, and scripts in hack/, which manage building, testing, and deploying the operator via container images and Kubernetes manifests.
- Testing: Represent the separation of testing components (e2e tests in test/e2e and utilities in test/utils).

##### Relationships and Data Flow
- When a user applies a CR (via kubectl), the Kubernetes API Server recognizes the new/updated NetplanConfig.
- The operator (running as a controller in the cluster or as a local process during development) watches these changes.
- The controller (internal/controller) receives events and triggers the reconciliation process.
- The reconciliation process may interact with helper methods from pkg/client and pkg/config to perform business logic.
- CI/CD and Build Pipeline: Optionally include a side block for the local development and build steps (using Makefile and Docker build commands) showing the flow from development -> image build -> push -> deploy.

##### Architectural Patterns & Design Principles
- Operator Pattern: Clearly state that the project follows the operator pattern (watching resources, reconciling desired vs actual state).
- Layered structure: Emphasize separation between API definitions (api/v1), business logic (internal/controller), and supporting libraries (pkg/*).
- Deployment and configuration management: The use of Kustomize in the config directories shows a commitment to environment-specific customizations and modular YAML definitions.

##### Relevant Technologies, Frameworks, and Libraries
- Go (minimum version 1.16)
- Operator-SDK: Used for initializing project scaffolding and generating APIs/controllers.
- Kubernetes APIs & CRDs: For extending k8s with custom resources.
- Kustomize: Utilized for manifest customization during deploy.
- Docker: For containerizing and deploying the operator in clusters.
- Testing frameworks: Referenced in test/e2e and the internal test suites.

##### Extra Details
- Notes near components like “Makefile”, “Dockerfile”, and “operator-sdk” initial commands as hints of the build and deployment pipeline.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Kubernetes Cluster
    subgraph "Kubernetes Cluster"
        K8sAPIServer["Kubernetes API Server"]:::external
        CRD["Custom Resource (NetplanConfig CRD)"]:::external
    end

    %% Operator Control Plane
    subgraph "Operator Control Plane"
        main["Operator Controller (cmd/main.go)"]:::operator
        apiDefs["API Definitions (api/v1)"]:::api
        ctrlImpl["Controller Implementation (internal/controller)"]:::controller
    end

    %% Client & Helper Packages
    subgraph "Client & Helper Packages"
        client["Client Package (pkg/client/client.go)"]:::helper
        configUtil["Utility Module - Config (pkg/config/config.go)"]:::helper
        fileUtil["Utility Module - File (pkg/file/file.go)"]:::helper
    end

    %% Configuration and Manifests
    subgraph "Configuration and Manifests"
        crdConf["CRDs & Manifests (config/crd)"]:::config
        rbac["RBAC Settings (config/rbac)"]:::config
        deployConf["Deployment Configs (config/manager)"]:::config
        sampleConf["Sample Configs (config/samples)"]:::config
        monitoring["Monitoring & Network Policies (config/prometheus, config/network-policy)"]:::config
    end

    %% Build and Deployment Artifacts
    subgraph "Build and Deployment Artifacts"
        docker["Dockerfile"]:::build
        makefile["Makefile"]:::build
        hack["Scripts (hack)"]:::build
    end

    %% Testing
    subgraph "Testing"
        e2e["E2E Tests (test/e2e)"]:::test
        testUtil["Testing Utilities (test/utils)"]:::test
    end

    %% CI/CD Pipeline
    subgraph "CI/CD Pipeline"
        localDev["Local Development"]:::cd
        buildImg["Build Image"]:::cd
        deployOp["Deploy to Cluster"]:::cd
    end

    %% Relationships and Data Flow
    localDev -->|"apply"| CRD
    CRD -->|"notify"| K8sAPIServer
    K8sAPIServer -->|"triggers"| main

    main -->|"reads"| apiDefs
    main -->|"calls"| ctrlImpl

    ctrlImpl -->|"uses"| client
    ctrlImpl -->|"uses"| configUtil
    ctrlImpl -->|"uses"| fileUtil

    main -->|"configured_by"| crdConf
    main -->|"secured_by"| rbac
    main -->|"deployed_with"| deployConf
    main -->|"samples"| sampleConf
    main -->|"monitored_by"| monitoring

    localDev -->|"runs"| makefile
    makefile -->|"triggers"| docker
    docker -->|"builds"| buildImg
    buildImg -->|"pushes"| deployOp
    deployOp -->|"updates"| main

    e2e -->|"validates"| main
    testUtil -->|"supports"| e2e

    %% Click Events
    click main "https://github.com/saeed-mcu/netplan-operator/blob/main/cmd/main.go"
    click apiDefs "https://github.com/saeed-mcu/netplan-operator/tree/main/api/v1"
    click ctrlImpl "https://github.com/saeed-mcu/netplan-operator/tree/main/internal/controller"
    click client "https://github.com/saeed-mcu/netplan-operator/blob/main/pkg/client/client.go"
    click configUtil "https://github.com/saeed-mcu/netplan-operator/blob/main/pkg/config/config.go"
    click fileUtil "https://github.com/saeed-mcu/netplan-operator/blob/main/pkg/file/file.go"
    click crdConf "https://github.com/saeed-mcu/netplan-operator/tree/main/config/crd"
    click rbac "https://github.com/saeed-mcu/netplan-operator/tree/main/config/rbac"
    click deployConf "https://github.com/saeed-mcu/netplan-operator/tree/main/config/manager"
    click sampleConf "https://github.com/saeed-mcu/netplan-operator/tree/main/config/samples"
    click monitoring "https://github.com/saeed-mcu/netplan-operator/tree/main/config/prometheus"
    click monitoring "https://github.com/saeed-mcu/netplan-operator/tree/main/config/network-policy"
    click docker "https://github.com/saeed-mcu/netplan-operator/tree/main/Dockerfile"
    click makefile "https://github.com/saeed-mcu/netplan-operator/tree/main/Makefile"
    click hack "https://github.com/saeed-mcu/netplan-operator/tree/main/hack"
    click e2e "https://github.com/saeed-mcu/netplan-operator/tree/main/test/e2e"
    click testUtil "https://github.com/saeed-mcu/netplan-operator/tree/main/test/utils"

    %% Styles
    classDef external fill:#f9f,stroke:#333,stroke-width:2px;
    classDef operator fill:#bbf,stroke:#333,stroke-width:2px;
    classDef api fill:#cff,stroke:#333,stroke-width:2px;
    classDef controller fill:#fcf,stroke:#333,stroke-width:2px;
    classDef helper fill:#cfc,stroke:#333,stroke-width:2px;
    classDef config fill:#ffc,stroke:#333,stroke-width:2px;
    classDef build fill:#fcc,stroke:#333,stroke-width:2px;
    classDef test fill:#ccf,stroke:#333,stroke-width:2px;
    classDef cd fill:#efe,stroke:#333,stroke-width:2px;
```
