> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/cgw/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/cgw/latest/configuration/advanced-usage.md).

# Advanced usage

The Connectivity Gateway (CGW) chart has dedicated configuration and templates
for its built-in components: IPsec, BIRD, VRRP, GRE, the exporters, and PCAP.

Containers in the CGW Deployment's pod share one network namespace, as they do
in any Kubernetes pod. An additional container can therefore use the IPsec and
BIRD interfaces — including VTI and GRE tunnels — and shares the pod's routing
tables, conntrack table, and sockets. This makes the values useful for adding
network functions, but also means that ports and network state are shared.

Use the following values to add anything (see [Use cases](#use-cases) to get
inspired) else:

| Value | Rendered into |
| --- | --- |
| `additionalInitContainers` | `spec.template.spec.initContainers` of the CGW Deployment |
| `additionalContainers` | `spec.template.spec.containers` |
| `additionalVolumes` | `spec.template.spec.volumes` |
| `additionalServices` | one `Service` per entry |

These values provide insertion points for components that the CGW chart does
not configure itself. Define the component's image, permissions, volume mounts,
and any other prerequisites in the entry and its supporting Kubernetes
resources; selecting and fulfilling those requirements is outside the scope of
the CGW chart.

## Use cases

Observability beyond the shipped exporters:

- `keepalived_exporter` for the VRRP state, which the chart has no exporter for.
- `blackbox_exporter` probing HTTP, DNS or TCP *through* a tunnel.
- An IPFIX or NetFlow exporter on the VTI and GRE interfaces, feeding a
  collector outside the cluster.
- A syslog forwarder shipping the strongSwan and BIRD logs to the operator's
  existing NMS across the VPN.

Services that have to live in the gateway's network namespace to be reachable
from the remote site:

- DNS for the remote site, bound to a VTI address, or a stub resolver
  forwarding over the tunnel.
- A DHCP relay from a GRETAP segment towards a central server across the VPN.
- A TCP or UDP proxy publishing a service that is only reachable through the
  tunnel into the cluster, or the other way around.
- An SNMP agent or trap forwarder, so the operator can poll the gateway.
- An IGMP proxy or PIM for multicast over GRETAP.

Data path setup the values do not model:

- `tc` shaping, nftables rules or `ip rule` policy routing. Usually an
  `additionalInitContainers` entry, since it has to be in place before the
  tunnels come up.
- NAT64 or 464XLAT for an IPv4-only site behind an IPv6 core.
- A second, independent tunnel — a WireGuard management path alongside the
  IPsec data path, as shown below.

Operations:

- A toolbox container where cluster policy forbids `kubectl debug`, if the
  shipped [debug container](https://docs.cennso.com/components/cgw/latest/configuration/debug.md) is not enough.
- A configuration puller that syncs Git content to the BIRD configuration
  directory, then runs `birdc configure`, as the external-source counterpart to
  `bird.birdConfigWatcher`.
- Route health injection: a probe that withdraws a BIRD route when a downstream
  check fails.

## What the pod already provides

The shared namespace means a shared port space. These ports are taken by the
chart and are fixed:

| Port | Component |
| --- | --- |
| 8000 | `bird.birdConfigWatcher` |
| 9324 | `bird.birdExporter` |
| 9371 | `conntrackExporter` |
| 9427 | `pingExporter` |
| 9536 | `ipsecExporter` |

One volume exists in every pod and can be mounted without declaring it:

- `podshare`, an `emptyDir`, for handing files between containers.

>**NOTE:** The chart's own containers add capabilities per container rather
> than through a pod-wide `securityContext`, and an additional container has to
> do the same. `NET_ADMIN` is what a container needs to configure a link;
> `NET_RAW`, for raw packets, is in the runtime's default set and does not have
> to be added. See [Container privileges](https://docs.cennso.com/components/cgw/latest/configuration/container-privileges.md) for what each
> of the chart's containers holds.

## Example: a WireGuard management tunnel

A WireGuard sidecar gives the gateway a second tunnel, independent of the IPsec
data path — useful as an out-of-band management path that stays up while the
IPsec configuration is being changed, or to reach a peer that does not speak
IKEv2.

Because the interface is created in the pod's namespace, every other container
sees it. BIRD can establish a peering session over `wg0`, and the routes it
installs apply to the whole pod.

The chart containers and this sidecar start in parallel. If the BIRD
configuration refers to `wg0`, ensure its protocol retries tolerate the
interface appearing after BIRD starts.

The private key and the peers belong in a `Secret`, not in the values:

```console
$ kubectl create secret generic cgw-wireguard --from-file=wg0.conf=./wg0.conf
```

```ini
# wg0.conf
[Interface]
Address = 192.0.2.1/32
ListenPort = 51820
PrivateKey = <the gateway's private key>

[Peer]
PublicKey = <the management host's public key>
AllowedIPs = 192.0.2.2/32
PersistentKeepalive = 25
```

```yaml
additionalVolumes:
  - name: wireguard-config
    secret:
      secretName: cgw-wireguard
      # wg refuses a world-readable configuration
      defaultMode: 0400

additionalContainers:
  - name: wireguard
    image: registry.example.com/wireguard:latest
    imagePullPolicy: IfNotPresent
    command:
      - sh
      - -c
      - |
        set -e
        wg-quick up wg0
        trap 'wg-quick down wg0' TERM INT
        # `wg-quick up` returns after configuring wg0. Keep PID 1 running so
        # Kubernetes does not restart the container and flap the tunnel.
        while true; do sleep 1000; done & wait
    ports:
      - containerPort: 51820
        protocol: UDP
        name: wireguard
    securityContext:
      capabilities:
        add: ["NET_ADMIN"]
    volumeMounts:
      - name: wireguard-config
        mountPath: /etc/wireguard/wg0.conf
        subPath: wg0.conf
        readOnly: true
    resources:
      limits:
        cpu: 50m
        memory: 64Mi
```

To reach the tunnel from outside the cluster, publish the listen port:

```yaml
additionalServices:
  - name: wireguard
    spec:
      type: LoadBalancer
      ports:
        - port: 51820
          targetPort: 51820
          protocol: UDP
          name: wireguard
```

>**NOTE:** The in-kernel implementation needs the `wireguard` module to be
>available on the node. Load it through the node configuration before the CGW
>pod is scheduled; `wg-quick` does not load it. Where that is not an option,
>run a userspace implementation — `wireguard-go` or `boringtun`, selected with
>`WG_QUICK_USERSPACE_IMPLEMENTATION` — which needs `/dev/net/tun` instead.

## Example: scraping the WireGuard tunnel

The transfer counters and last handshake per peer can be made available with
[prometheus_wireguard_exporter](https://github.com/MindFlavor/prometheus_wireguard_exporter):

```yaml
additionalContainers:
  - name: wireguard-exporter
    image: registry.example.com/prometheus-wireguard-exporter:latest
    imagePullPolicy: IfNotPresent
    # -a also reports peers that never completed a handshake, which is what
    # makes a broken peer visible instead of absent
    args: ["-a", "true", "-p", "9586"]
    ports:
      - containerPort: 9586
        protocol: TCP
        name: wireguard-exporter
    securityContext:
      capabilities:
        add: ["NET_ADMIN"]
    resources:
      limits:
        cpu: 20m
        memory: 32Mi
```

Expose the port through a `Service` before Prometheus can scrape it:

```yaml
additionalServices:
  - name: wireguard-exporter
    spec:
      type: ClusterIP
      ports:
        - port: 9586
          targetPort: 9586
          protocol: TCP
          name: wireguard-exporter
```

The `CGW` chart renders that `Service`; it does not render a ServiceMonitor for
it. If this gateway is scraped by the ServiceMonitor installed by a
`cgw-support` release, add the endpoint to the *`cgw-support` chart values* for
that release. `additionalEndpoints` appends it to that ServiceMonitor's shipped
exporter endpoints:

```yaml
prometheus:
  servicemonitor:
    enabled: true
    additionalEndpoints:
      - port: wireguard-exporter
        honorLabels: true
```

Useful series once it is scraped:

- `wireguard_latest_handshake_seconds` — a peer that stops handshaking is down,
  even though the interface stays up.
- `wireguard_sent_bytes_total`, `wireguard_received_bytes_total` — per peer
  throughput.

The `endpoint` refers to the *port name* of the `Service`, so the two names must
match. An endpoint whose port name exists on no selected service produces no
target and no error — if the metrics do not appear, that mismatch is the first
thing to check.

Each metrics port should be scraped by one ServiceMonitor. If an
operator-managed ServiceMonitor owns this Service, add the
`wireguard-exporter` endpoint there instead of using `additionalEndpoints`.
If the `cgw-support` ServiceMonitor shares responsibility with another
ServiceMonitor for the standard CGW exporter ports, set
`prometheus.servicemonitor.endpoints` in the `cgw-support` chart values to
only the ports it owns. `endpoints` replaces the default endpoint list;
`additionalEndpoints` is appended to it.

## Constraints for added components

When adding or changing a component through these values, account for the
following constraints:

- **Shared port space.** Bind to a specific address where a port is ambiguous,
  and stay off the ports listed above.
- **Tunnel interfaces come and go.** A VTI is created and removed with its
  associated SA. A container that configures one at startup is wrong after the
  first rekey; watch the interface instead of assuming it.
- **The firewall does not know about the new port.** With `iptables.enabled`,
  add a rule for it to `iptables.ipv4Rules` and `iptables.ipv6Rules`. The
  chart opens the ports of its own exporters only.
- **Adding a container restarts the pod.** The deployment strategy is
  `Recreate`, and a restart tears down every SA along with the VTI and route
  setup. Plan the change like a maintenance window, not like a configuration
  reload.
- **The pod's QoS class depends on every container.** The chart's own
  containers state `cpu` and `memory` limits only, which Kubernetes also takes
  as requests, and the pod lands in the QoS class `Guaranteed`. An added
  container keeps it there only with both limits set and no lower requests;
  otherwise the whole pod becomes `Burstable`.
- **Nothing validates these values.** They are copied into the manifest as
  given. A malformed Kubernetes object is rejected by the API server; a
  runtime or container-configuration mistake can cause a `CrashLoopBackOff`.
  Check with `helm template` before applying.
