# jovik31/tenantcni architecture

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

- Interactive diagram: https://gitdiagram.com/jovik31/tenantcni
- Repository: https://github.com/jovik31/tenantcni

## Overview

1. **What this repository is**
   - TenantCNI is a Go-based Kubernetes operator plus node-side networking component for multi-tenant pod networking.
   - It registers a custom resource definition (CRD) for `Tenant` objects and uses those objects to create isolated tenant network behavior inside a cluster.

2. **Top-level runtime split**
   - `cmd/tenantcni` is the operator/control-plane binary.
   - `cmd/tenantcnid` is the node-side daemon/binary that likely participates in CNI and per-node network setup.
   - The repository is therefore split across a Kubernetes reconciliation plane and a node networking plane.

3. **Custom API boundary**
   - The API lives under `pkg/apis/jovik31.dev/v1alpha1`.
   - The main custom resource is `Tenant`, with generated deepcopy/defaulting code, showing that tenant state is meant to be a first-class Kubernetes object.
   - `pkg/crd/register.go` handles CRD registration, making the tenant API available through the Kubernetes API server.

4. **Generated Kubernetes client stack**
   - The `pkg/client` tree is fully generated client machinery: typed clients, informers, listers, fake clients, and apply configurations.
   - This indicates the operator is built in the standard Kubernetes pattern: watch resources through shared informers, cache them with listers, and reconcile via the typed client against the API server.

5. **Controller/reconciliation layer**
   - `pkg/controller` contains the reconciliation logic, split into `add.go`, `update.go`, `delete.go`, `handlers.go`, and utility code.
   - This is the main orchestration layer that reacts to `Tenant` lifecycle events and likely creates or tears down tenant-specific network state and related Kubernetes objects.
   - The controller boundary is the core decision point between desired tenant state and actual network configuration.

6. **Network subsystem**
   - `pkg/network/backend` contains two backends: `bridge.go` and `vxlan.go`.
   - This implies the system can realize tenant networking using either Linux bridge-based attachment or VXLAN-based overlay networking.
   - `pkg/network/routing` contains `iptables.go` and `routes.go`, showing that packet forwarding and tenant isolation are enforced with host routing and firewall rules.
   - The architecture therefore spans both Kubernetes control logic and node-level Linux networking primitives.

7. **IP address management**
   - `pkg/network/ipam` is the allocation subsystem.
   - It has separate stores for `node`, `pod`, and `tenant`, plus locking support, which suggests coordinated address/lease management across cluster objects and nodes.
   - This is an important boundary: IP allocation is not incidental; it is a dedicated subsystem used to keep tenant networking consistent and prevent collisions.

8. **Kubernetes integration and node utilities**
   - `pkg/k8s/kube.go` likely wraps Kubernetes client setup and cluster interactions.
   - `pkg/log/log.go` centralizes logging.
   - These are supporting boundaries used by both the operator and node daemon to talk to the API server and emit consistent diagnostics.

9. **Operational model and data flow**
   - A tenant is created as a Kubernetes custom resource, then the controller reconciles that desired state into network resources and IPAM state.
   - The README indicates a default tenant is created automatically, while custom tenants must exist before pods are scheduled into them.
   - Pods targeting custom tenants require both an annotation and a node selector, meaning scheduling is part of the tenant selection mechanism rather than being inferred automatically.

10. **Cluster and packet-flow assumptions**
   - The README states node CIDRs must use a `/22` mask so each tenant can get the full 254 pods available in that range.
   - That tells you the implementation assumes a fixed per-node address budget and builds tenant allocation around it.
   - This is an architectural constraint worth showing in any diagram because it affects capacity and address partitioning.

11. **Deployment artifacts**
   - `manifests/operator_deploy.yaml` and `manifests/local_operator.yaml` deploy the operator.
   - `manifests/tenant_example.yaml`, `tenant_RED.yaml`, `tenant_GREEN.yaml`, and pod examples document the expected tenant and pod wiring.
   - `manifests/busybox*.yaml` are test workloads for validating tenant-specific networking behavior.

12. **Build and generation tooling**
   - The repo is Go module-based, with `Makefile`, `Dockerfile`, and code generation scripts under `scripts/`.
   - The presence of generated client/informer/lister code and deepcopy files means the API is code-generated, not handwritten.
   - This matters architecturally because the control plane depends on Kubernetes codegen conventions rather than custom RPC or bespoke control channels.

13. **External dependencies and boundaries**
   - The system depends on the Kubernetes API server, CRD support, Linux networking primitives, and likely CNI execution on nodes.
   - The network implementation uses host-level routing/iptables and optional VXLAN overlay, so the trust boundary is the node OS, not just Kubernetes objects.
   - In diagram terms, the main external edges are: Kubernetes API, node networking stack, and pod scheduling behavior.

14. **How to draw the architecture**
   - Put the Kubernetes API server at the center top.
   - Connect the operator binary to it via CRD watch/reconcile loops.
   - Show the `Tenant` CRD as the desired-state input.
   - Show the IPAM, routing, and backend modules as controller-owned subsystems.
   - Show the node daemon/CNI path on each node as the layer that realizes bridge/VXLAN/routing configuration for tenant pods.

Main entry points: [`pkg/controller`](https://github.com/jovik31/tenantcni/tree/main/pkg/controller) (Controller), [`cmd/tenantcnid/main.go`](https://github.com/jovik31/tenantcni/blob/main/cmd/tenantcnid/main.go) (tenantcnid), [`cmd/tenantcni/main.go`](https://github.com/jovik31/tenantcni/blob/main/cmd/tenantcni/main.go) (tenantcni), [`pkg/network/ipam/ipam.go`](https://github.com/jovik31/tenantcni/blob/main/pkg/network/ipam/ipam.go) (IPAM)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_control["Control plane"]
  node_n_main_operator["tenantcni<br/>operator binary<br/>[main.go]"]
  node_n_controller{{"Controller<br/>reconcile loop"}}
  node_n_k8s["Kube client<br/>api wrapper<br/>[kube.go]"]
  node_n_log["Logging<br/>[log.go]"]
end

subgraph group_g_api["Tenant API"]
  node_n_crd["CRD register<br/>api registration<br/>[register.go]"]
  node_n_tenant_api["Tenant API<br/>custom resource<br/>[types.go]"]
  node_n_clientset["Clientset<br/>generated client<br/>[clientset.go]"]
  node_n_informer["Informer<br/>generated cache<br/>[factory.go]"]
  node_n_lister["Lister<br/>generated cache<br/>[tenant.go]"]
end

subgraph group_g_node["Node networking"]
  node_n_main_daemon["tenantcnid<br/>node daemon<br/>[main.go]"]
  node_n_ipam{{"IPAM<br/>allocation manager<br/>[ipam.go]"}}
  node_n_ipam_store[("IP stores<br/>state stores")]
  node_n_backend_bridge["Bridge<br/>network backend<br/>[bridge.go]"]
  node_n_backend_vxlan["VXLAN<br/>network backend<br/>[vxlan.go]"]
  node_n_routing{{"Routing<br/>packet policy"}}
end

subgraph group_g_ops["Ops artifacts"]
  node_n_manifests["Manifests<br/>deployment assets"]
  node_n_codegen["Codegen<br/>build tooling"]
  node_n_build["Build"]
end

node_n_main_operator -->|"starts"| node_n_controller
node_n_main_operator -->|"uses"| node_n_k8s
node_n_main_operator -->|"logs"| node_n_log
node_n_main_daemon -->|"configures"| node_n_backend_bridge
node_n_main_daemon -->|"configures"| node_n_backend_vxlan
node_n_main_daemon -->|"applies"| node_n_routing
node_n_main_daemon -->|"allocates"| node_n_ipam
node_n_crd -->|"registers"| node_n_tenant_api
node_n_controller -->|"watches"| node_n_informer
node_n_controller -->|"reads"| node_n_lister
node_n_controller -->|"writes"| node_n_clientset
node_n_controller -->|"drives"| node_n_ipam
node_n_controller -->|"selects"| node_n_backend_bridge
node_n_controller -->|"selects"| node_n_backend_vxlan
node_n_controller -->|"programs"| node_n_routing
node_n_ipam -->|"persists"| node_n_ipam_store
node_n_manifests -.->|"deploys"| node_n_crd
node_n_codegen -.->|"generates"| node_n_clientset
node_n_codegen -.->|"generates"| node_n_tenant_api
node_n_build -.->|"builds"| node_n_main_operator
node_n_build -.->|"builds"| node_n_main_daemon

click node_n_main_operator "https://github.com/jovik31/tenantcni/blob/main/cmd/tenantcni/main.go"
click node_n_main_daemon "https://github.com/jovik31/tenantcni/blob/main/cmd/tenantcnid/main.go"
click node_n_controller "https://github.com/jovik31/tenantcni/tree/main/pkg/controller"
click node_n_k8s "https://github.com/jovik31/tenantcni/blob/main/pkg/k8s/kube.go"
click node_n_log "https://github.com/jovik31/tenantcni/blob/main/pkg/log/log.go"
click node_n_crd "https://github.com/jovik31/tenantcni/blob/main/pkg/crd/register.go"
click node_n_tenant_api "https://github.com/jovik31/tenantcni/blob/main/pkg/apis/jovik31.dev/v1alpha1/types.go"
click node_n_clientset "https://github.com/jovik31/tenantcni/blob/main/pkg/client/clientset/versioned/clientset.go"
click node_n_informer "https://github.com/jovik31/tenantcni/blob/main/pkg/client/informers/externalversions/factory.go"
click node_n_lister "https://github.com/jovik31/tenantcni/blob/main/pkg/client/listers/jovik31.dev/v1alpha1/tenant.go"
click node_n_ipam "https://github.com/jovik31/tenantcni/blob/main/pkg/network/ipam/ipam.go"
click node_n_ipam_store "https://github.com/jovik31/tenantcni/tree/main/pkg/network/ipam"
click node_n_backend_bridge "https://github.com/jovik31/tenantcni/blob/main/pkg/network/backend/bridge.go"
click node_n_backend_vxlan "https://github.com/jovik31/tenantcni/blob/main/pkg/network/backend/vxlan.go"
click node_n_routing "https://github.com/jovik31/tenantcni/tree/main/pkg/network/routing"
click node_n_manifests "https://github.com/jovik31/tenantcni/tree/main/manifests"
click node_n_codegen "https://github.com/jovik31/tenantcni/tree/main/scripts"
click node_n_build "https://github.com/jovik31/tenantcni/tree/main/Makefile"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n_main_operator,node_n_controller,node_n_k8s,node_n_log toneBlue
class node_n_crd,node_n_tenant_api,node_n_clientset,node_n_informer,node_n_lister toneAmber
class node_n_main_daemon,node_n_ipam,node_n_ipam_store,node_n_backend_bridge,node_n_backend_vxlan,node_n_routing toneMint
class node_n_manifests,node_n_codegen,node_n_build toneRose
```

## Components

### Control plane

Operator-side reconciliation and Kubernetes API integration.

- [tenantcni](https://github.com/jovik31/tenantcni/blob/main/cmd/tenantcni/main.go): operator binary · Control-plane entrypoint for tenant reconciliation. · `cmd/tenantcni/main.go`
- [Controller](https://github.com/jovik31/tenantcni/tree/main/pkg/controller): reconcile loop · Handles tenant add, update, delete, and event wiring. · `pkg/controller`
- [Kube client](https://github.com/jovik31/tenantcni/blob/main/pkg/k8s/kube.go): api wrapper · Shared Kubernetes client and cluster interaction helpers. · `pkg/k8s/kube.go`
- [Logging](https://github.com/jovik31/tenantcni/blob/main/pkg/log/log.go): logging · Centralized logging helpers used by both runtimes. · `pkg/log/log.go`

### Tenant API

Custom resource definition and generated Kubernetes accessors.

- [CRD register](https://github.com/jovik31/tenantcni/blob/main/pkg/crd/register.go): api registration · Registers the Tenant CRD with the API server. · `pkg/crd/register.go`
- [Tenant API](https://github.com/jovik31/tenantcni/blob/main/pkg/apis/jovik31.dev/v1alpha1/types.go): custom resource · Tenant object schema and defaults for desired tenant state. · `pkg/apis/jovik31.dev/v1alpha1/types.go`
- [Clientset](https://github.com/jovik31/tenantcni/blob/main/pkg/client/clientset/versioned/clientset.go): generated client · Typed Kubernetes client for the Tenant API. · `pkg/client/clientset/versioned/clientset.go`
- [Informer](https://github.com/jovik31/tenantcni/blob/main/pkg/client/informers/externalversions/factory.go): generated cache · Shared informer factory for watching Tenant resources. · `pkg/client/informers/externalversions/factory.go`
- [Lister](https://github.com/jovik31/tenantcni/blob/main/pkg/client/listers/jovik31.dev/v1alpha1/tenant.go): generated cache · Cached read access to Tenant objects. · `pkg/client/listers/jovik31.dev/v1alpha1/tenant.go`

### Node networking

Per-node data plane and Linux networking primitives.

- [tenantcnid](https://github.com/jovik31/tenantcni/blob/main/cmd/tenantcnid/main.go): node daemon · Node-side runtime for tenant networking setup. · `cmd/tenantcnid/main.go`
- [IPAM](https://github.com/jovik31/tenantcni/blob/main/pkg/network/ipam/ipam.go): allocation manager · Coordinates tenant, pod, and node address allocation. · `pkg/network/ipam/ipam.go`
- [IP stores](https://github.com/jovik31/tenantcni/tree/main/pkg/network/ipam): state stores · Persistent stores and locking for node, pod, and tenant allocation. · `pkg/network/ipam`
- [Bridge](https://github.com/jovik31/tenantcni/blob/main/pkg/network/backend/bridge.go): network backend · Linux bridge-based tenant attachment path. · `pkg/network/backend/bridge.go`
- [VXLAN](https://github.com/jovik31/tenantcni/blob/main/pkg/network/backend/vxlan.go): network backend · Overlay backend for tenant traffic isolation. · `pkg/network/backend/vxlan.go`
- [Routing](https://github.com/jovik31/tenantcni/tree/main/pkg/network/routing): packet policy · Routes and iptables rules that enforce forwarding and isolation. · `pkg/network/routing`

### Ops artifacts

Deployment manifests and build tooling.

- [Manifests](https://github.com/jovik31/tenantcni/tree/main/manifests): deployment assets · Operator, tenant, and workload example YAMLs. · `manifests`
- [Codegen](https://github.com/jovik31/tenantcni/tree/main/scripts): build tooling · Scripts and make-based generation for clients, defaults, and deepcopy code. · `scripts`
- [Build](https://github.com/jovik31/tenantcni/tree/main/Makefile): build config · Go module, Docker image, and make entrypoints. · `Makefile`

## Connections

- tenantcni → Controller: starts · Operator binary hosts reconciliation logic.
- tenantcni → Kube client: uses · Connects to the Kubernetes API.
- tenantcni → Logging: logs · Emits operator diagnostics.
- tenantcnid → Bridge: configures · Programs bridge-based networking on nodes.
- tenantcnid → VXLAN: configures · Programs overlay networking on nodes.
- tenantcnid → Routing: applies · Installs routing and firewall policy.
- tenantcnid → IPAM: allocates · Coordinates node-local and cluster-wide IP usage.
- CRD register → Tenant API: registers · Exposes Tenant as a Kubernetes resource.
- Controller → Informer: watches · Consumes Tenant change events.
- Controller → Lister: reads · Uses cached Tenant state for reconciliation.
- Controller → Clientset: writes · Creates and updates Kubernetes resources.
- Controller → IPAM: drives · Allocates and releases tenant networking state.
- Controller → Bridge: selects · Can realize tenant connectivity with bridge backend.
- Controller → VXLAN: selects · Can realize tenant connectivity with VXLAN backend.
- Controller → Routing: programs · Pushes forwarding and isolation rules.
- IPAM → IP stores: persists · Uses stores and locks for allocation consistency.
- Manifests → CRD register: deploys · Contains operator and Tenant example manifests.
- Codegen → Clientset: generates · Produces typed clients and informers.
- Codegen → Tenant API: generates · Produces deepcopy and defaulting code.
- Build → tenantcni: builds · Build target for operator binary.
- Build → tenantcnid: builds · Build target for node daemon binary.
