diff --git a/docs/4-implementation/k8s/node.md b/docs/4-implementation/k8s/node.md index e0e803b98..c340b1a60 100644 --- a/docs/4-implementation/k8s/node.md +++ b/docs/4-implementation/k8s/node.md @@ -56,6 +56,12 @@ cluster of its own beyond that one main node, and it replicates no service. trusted, so the node reaches its pods. The node-to-node ports are open to the addresses in `deploy.k8s.nodeAddresses` only, which is empty by default ([Joining nodes](#joining-nodes)). +- **NetworkManager:** a host that runs it tells it to leave `cni0`, the + `flannel*` devices and the pods' `veth*` ends alone, since it otherwise + rewrites the routes of the interfaces the CNI creates in the default + namespace. A host without NetworkManager gets no setting. A joining node + gets the same list. `veth*` also matches other veth pairs with that prefix, + such as podman's. - **NetworkPolicy:** k3s's own controller (kube-router, iptables) enforces NetworkPolicy objects; the node doesn't turn it off. The namespace has one policy, `default-deny-ingress`, that selects every pod and admits nobody, so diff --git a/nix/host-modules/k8s/node.nix b/nix/host-modules/k8s/node.nix index be99b6937..ae7a3db1e 100644 --- a/nix/host-modules/k8s/node.nix +++ b/nix/host-modules/k8s/node.nix @@ -515,6 +515,19 @@ in ]; }) + # NetworkManager manages every interface it finds unless told not to, and + # rewrites the routes and addresses of the ones the cluster creates: `cni0` + # (the bridge CNI), `flannel-wg` and the other `flannel*` devices, and the + # host end of each pod's veth pair (`veth*`). The same applies on a joining + # node, which runs the same CNI. A host without NetworkManager is left alone. + (lib.mkIf ((cfg.enable || cfg.join != null) && config.networking.networkmanager.enable) { + networking.networkmanager.unmanaged = [ + "interface-name:cni0" + "interface-name:flannel*" + "interface-name:veth*" + ]; + }) + # An agent of another host's cluster. The server's manifests and units are # not here: the pods it runs come from the main node's API. (lib.mkIf (cfg.join != null && !cfg.enable) { diff --git a/nix/module-eval/k8s.nix b/nix/module-eval/k8s.nix index 6ec569647..9b8db07dc 100644 --- a/nix/module-eval/k8s.nix +++ b/nix/module-eval/k8s.nix @@ -58,6 +58,16 @@ let } ); + # The node with NetworkManager on, a host that has no node with it on, and + # (below, `joined`) an agent with it on. `allLocal` is the node with it off. + networkManagerOn = { + networking.networkmanager.enable = true; + }; + nodeWithNm = hiveWith [ networkManagerOn ] ( + lib.recursiveUpdate { deploy.hive-controller.enable = true; } nodeSettings + ); + noClusterWithNm = hiveWith [ networkManagerOn ] { deploy.hive-controller.enable = false; }; + # Two hives on a store host with the node: the policy of each differs by the # join token. Shares nothing of `onCluster`'s controller, which lists the # hives. @@ -81,7 +91,7 @@ let # A host that joins that cluster: an agent with the store identity its join # token is read under. The only fixture without `deploy.k8s.enable`, which # is what it claims about. - joined = hiveWith [ semiSpreadWg ] { + joined = hiveWith [ semiSpreadWg networkManagerOn ] { deploy.k8s.join.server = "192.168.1.10"; deploy.k8s.address = "192.168.1.11"; deploy.hive-controller.tls = { @@ -461,6 +471,28 @@ let && refuses "overlap the hive bridge" && refuses "overlap each other"; } + { + # NetworkManager rewrites the routes of the interfaces the CNI makes, so + # it is told to leave `cni0`, flannel's devices and the pods' veth ends + # alone, on the node and on an agent. The literals are spelled here, not + # read from the module. Each absence arm has its presence control: the + # node and the agent have the list, so the lookup finds it where it is. + name = "NetworkManager leaves the pod network's interfaces unmanaged on a node and on an agent, and is untouched where it is off or where there is no cluster"; + ok = + let + expected = [ + "interface-name:cni0" + "interface-name:flannel*" + "interface-name:veth*" + ]; + in + nodeWithNm.networking.networkmanager.unmanaged == expected + && joined.networking.networkmanager.unmanaged == expected + && !allLocal.networking.networkmanager.enable + && allLocal.networking.networkmanager.unmanaged == [ ] + && noClusterWithNm.networking.networkmanager.enable + && noClusterWithNm.networking.networkmanager.unmanaged == [ ]; + } { # An agent takes the backend from its server, and `--flannel-backend` is # a server flag that an agent refuses, so the agent's flags carry none.