cilium/cilium
Cilium agent
Active contributors: aanm, joestringer, joamaki, gandro, christarazi
Purpose
The Cilium agent (binary cilium-agent, image quay.io/cilium/cilium) is the userspace control plane that runs as a DaemonSet on every Kubernetes node. It watches the API server and the optional kvstore, computes per-endpoint policy, programs the eBPF dataplane, manages the L7 proxy (Envoy), and exports state via REST and gRPC.
Directory layout
daemon/
├── main.go # ~15 lines: build a Hive and run it
├── cmd/
│ ├── root.go # cobra root command
│ ├── cells.go # the cell graph that defines the agent
│ ├── daemon.go # the legacy `Daemon` struct, shrinking over time
│ ├── daemon_main.go # CLI flags + DaemonConfig binding
│ ├── endpoint_restore.go # restoring endpoints after agent restart
│ └── ...
├── healthz/ # /healthz endpoint
├── infraendpoints/ # internal endpoint definitions
├── k8s/ # k8s-specific startup helpers
└── restapi/ # REST API server wiringKey abstractions
| Type | File | Role |
|---|---|---|
cmd.Agent |
daemon/cmd/cells.go |
The root cell.Module that defines the agent's cell graph. |
cmd.NewAgentCmd |
daemon/cmd/root.go |
Builds the cobra command and wires Hive into Cobra's RunE. |
Daemon |
daemon/cmd/daemon.go |
The legacy "everything bag" struct that still owns endpoint restore and the restapi. |
daemonConfig |
daemon/cmd/daemon_main.go |
Maps every CLI flag onto option.DaemonConfig and other typed configs. |
endpointRestoreState |
daemon/cmd/endpoint_restore.go |
Restores endpoints from /var/run/cilium/state/ after restart. |
How it works
graph TD
Main[daemon/main.go] -->|hive.New(cmd.Agent)| Hive
Hive -->|cells.go| K8sCells[k8s clientset cell]
Hive --> EndpointMgr[endpoint manager cell]
Hive --> PolicyRepo[policy repository cell]
Hive --> Datapath[datapath orchestrator cell]
Hive --> Envoy[envoy + proxy cell]
Hive --> Hubble[hubble cell]
Hive --> RESTAPI[REST API cell]
Hive --> Restore[endpoint restore]
K8sCells -->|watches| APIServer[kube-apiserver]
PolicyRepo -->|computes| EndpointMgr
EndpointMgr -->|regen| Datapath
Datapath -->|loader| BPF[eBPF programs]
EndpointMgr -->|xDS| Envoy
Hubble -->|monitor events| Relay[hubble-relay]Startup is driven by hive.Run(). Each cell's OnStart hook fires in dependency order:
- Boot —
pkg/version/, signal handling, pidfile, gops. - Kubernetes — clientset, watchers, informers (
pkg/k8s/). - Identity — allocator (
pkg/identity/cache/). - Policy — repository and selector cache (
pkg/policy/). - Datapath — link discovery, route table, BPF loader (
pkg/datapath/). - Endpoint manager — restore, regenerate (
pkg/endpoint/,pkg/endpointmanager/). - Envoy — proxy startup and xDS server (
pkg/envoy/,pkg/proxy/). - Hubble — local observer + gRPC server (
pkg/hubble/). - REST API + health —
daemon/restapi/,daemon/healthz/.
Shutdown reverses the order; OnStop hooks drain queues and persist endpoint state to disk.
Integration points
- Kubernetes: all CRDs and core resources via informers in
pkg/k8s/watchers/. - kvstore: optional, used by Cluster Mesh and historical paths (
pkg/kvstore/). - CNI:
plugins/cilium-cni/writes endpoint metadata todaemon/cmd/cni/. - Envoy: xDS over Unix socket; bootstrap config emitted by
pkg/envoy/. - Hubble Relay: the agent's gRPC observer endpoint (
pkg/hubble/relay/) is consumed byhubble-relay. - Operator: the operator allocates pod CIDRs and identity GC; the agent only consumes those allocations.
Entry points for modification
- To add a new agent capability, write a cell under
pkg/<name>/cell.go, then include it indaemon/cmd/cells.go. - To add a new flag, prefer cell-local config (
cell.Config(...)) overdaemonConfig. Update Helm values underinstall/kubernetes/cilium/values.yamlso it can be exposed to users. - To add a new REST endpoint, edit
api/v1/openapi.yaml, runmake generate-api, and implement the handler indaemon/restapi/.
Key source files
| File | Purpose |
|---|---|
daemon/main.go |
Entry point; builds Hive and runs the cobra command. |
daemon/cmd/cells.go |
The complete cell graph for the agent. |
daemon/cmd/daemon.go |
The legacy Daemon struct. |
daemon/cmd/daemon_main.go |
CLI flag definitions. |
daemon/cmd/endpoint_restore.go |
Endpoint state restoration. |
daemon/healthz/ |
Health probe handler. |
daemon/restapi/ |
REST API wiring. |
See systems/control-plane.md for the Hive cell model in depth, and systems/datapath.md for the agent's relationship with the BPF dataplane.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.