# netbox-community/netbox-operator architecture

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

- Interactive diagram: https://gitdiagram.com/netbox-community/netbox-operator
- Repository: https://github.com/netbox-community/netbox-operator

## Overview

##### Main components
- Kubernetes Cluster: This is where the operator runs and where the custom resources (CRs) are created.
- NetBox Operator (Controller): The core of the application, with its code located in the cmd/ and internal/controller directories. This component continuously watches and reconciles the custom resources.
- Custom Resource Definitions (CRDs): Found under the api/v1 and config/crd directories, these define resources like IpAddress, IpAddressClaim, Prefix, and PrefixClaim.
- NetBox API Integration: This logic is encapsulated in the pkg/netbox directory (api, models, utils) which handles all interaction with a remote NetBox instance.
- External NetBox Service: The actual NetBox instance (or instances) to which the operator connects to assign and manage IP addresses and prefixes.
- Supporting components:
- Configuration layers and manifests in the config directories (default, manager, RBAC, samples, etc.) that define how the operator is deployed.
- CI/CD and testing tooling (provided by GitHub workflows, tests, and kind cluster sample configurations) to ensure proper deployment and functionality.

##### Relationships and interactions
- The operator running inside the Kubernetes cluster listens for changes on Custom Resources (e.g. PrefixClaim, IpAddressClaim).
- On detecting a new, updated, or deleted CR, the operator’s reconciliation loop engages.
- There is an API gateway within the operator that communicates with the NetBox instance using the NetBox API client (pkg/netbox/api). These calls allow the operator to create, update, or delete network resources in NetBox.
- The CRDs communicate both with the Kubernetes API and the operator’s business logic that integrates with the NetBox API.
- There is also an important restoration path that uses custom fields (like preserveInNetbox and netboxOperatorRestorationHash) to ensure that state can be recovered if necessary.

##### Architectural patterns and design principles
- Operator Pattern: This is visible in the reconciliation loop and custom resource handling present in the internal/controller and cmd/main.go files.
- Separation of concerns: There is a clear separation between Kubernetes API interactions, NetBox API communications, and controller logic.
- Modularity and testability: The project uses packages (e.g., pkg/netbox, internal/controller) to separate responsibilities, along with extensive tests in the tests/e2e directory.
- Extensibility: With support for both IP addresses and prefixes (and their “claims”), the system design anticipates mixed usage scenarios and provides configuration samples and guides for advanced users.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["User/CLI"]:::user

    subgraph "Kubernetes Cluster"
        B["Kubernetes API Server (etcd)"]:::k8s
        subgraph "Custom Resources (CRDs)"
            B1["IP Addresses & Prefixes (api/v1)"]:::crd
            B2["IP/Prefix Claims (config/crd)"]:::crd
        end
    end

    subgraph "NetBox Operator"
        O1["Main (cmd/main.go)"]:::operator
        O2["Controller (internal/controller/)"]:::operator
        O1 --> O2
    end

    C["NetBox API Integration (pkg/netbox/)"]:::netbox

    D["External NetBox Service"]:::external

    subgraph "Deployment & Configuration"
        E1["Manager Config (config/manager/)"]:::config
        E2["Default Config (config/default/)"]:::config
        E3["RBAC Config (config/rbac/)"]:::config
        E4["Sample Manifests (config/samples/)"]:::config
        F1["GitHub Workflows (.github/workflows/)"]:::cicd
        F2["E2E Tests (tests/e2e/)"]:::cicd
    end

    A -->|"deploysCRs"| B
    B -->|"triggers_watch"| O1
    O2 -->|"reconciles_CR"| B
    O2 -->|"calls_API"| C
    C -->|"netbox_calls"| D

    E1 --- O1
    E2 --- O1
    E3 --- O1
    E4 --- O1
    F1 --- E1
    F2 --- E1

    click O1 "https://github.com/netbox-community/netbox-operator/blob/main/cmd/main.go"
    click O2 "https://github.com/netbox-community/netbox-operator/tree/main/internal/controller/"
    click B1 "https://github.com/netbox-community/netbox-operator/tree/main/api/v1/"
    click B2 "https://github.com/netbox-community/netbox-operator/tree/main/config/crd/"
    click C "https://github.com/netbox-community/netbox-operator/tree/main/pkg/netbox/"
    click E1 "https://github.com/netbox-community/netbox-operator/tree/main/config/manager/"
    click E2 "https://github.com/netbox-community/netbox-operator/tree/main/config/default/"
    click E3 "https://github.com/netbox-community/netbox-operator/tree/main/config/rbac/"
    click E4 "https://github.com/netbox-community/netbox-operator/tree/main/config/samples/"
    click F1 "https://github.com/netbox-community/netbox-operator/tree/main/.github/workflows/"
    click F2 "https://github.com/netbox-community/netbox-operator/tree/main/tests/e2e/"

    classDef user fill:#f9c,stroke:#333,stroke-width:2px;
    classDef k8s fill:#ccf,stroke:#333,stroke-width:2px;
    classDef crd fill:#bbf,stroke:#333,stroke-width:2px;
    classDef operator fill:#9f9,stroke:#333,stroke-width:2px;
    classDef netbox fill:#fd8,stroke:#333,stroke-width:2px;
    classDef external fill:#fdd,stroke:#333,stroke-width:2px;
    classDef config fill:#cff,stroke:#333,stroke-width:2px;
    classDef cicd fill:#fcf,stroke:#333,stroke-width:2px;
```
