Skip to content

✨ lb: limit a Service's NodePort to its Load Balancer - #90

Merged
batistein merged 2 commits into
mainfrom
batistein/restrict-node-port-source
Nov 6, 2026
Merged

batistein merged 2 commits into
mainfrom
batistein/restrict-node-port-source

Conversation

@batistein

Copy link
Copy Markdown

Problem

A type=LoadBalancer Service gets a NodePort, and the Hetzner LB targets that NodePort on every node. The NodePort is open on every node address, not only to the LB. On clusters with public node IPs and no private network or firewall in front of the nodes, anyone can reach the Service directly on <node-ip>:<nodePort>, bypassing the LB.

Hetzner LBs have no source filter (upstream hetznercloud#218, hetznercloud#286, hetznercloud#291), so the CCM can't fix this on the LB. The Service proxy can, if spec.loadBalancerSourceRanges names the LB.

Change

New opt-in: HCLOUD_LOAD_BALANCERS_RESTRICT_NODE_PORT_SOURCE=true (cluster default) or load-balancer.hetzner.cloud/restrict-node-port-source: "true|false" (per Service, overrides the default both ways).

When enabled, EnsureLoadBalancer adds the LB's own IPv4s as /32 entries to spec.loadBalancerSourceRanges:

  • the public IPv4, if the public network is enabled;
  • the private IPs, if the LB is attached to a network.

IPv6 is not added, because the LB reaches nodes over IPv4 even for IPv6 clients.

How it behaves:

  • Spec writes need their own client. The cloud-provider service controller resets the spec before patching (servicehelper.PatchService), so a client is created in cloud.Initialize (which was empty).
  • Entries set by others are kept. The entries the CCM owns are recorded in the read-only load-balancer.hetzner.cloud/node-port-source-ranges annotation. An entry someone else already listed stays theirs.
  • The CCM's own entries are replaced or removed when needed:
    • replaced when the LB IP changes;
    • removed on opt-out;
    • removed in EnsureLoadBalancerDeleted, e.g. after a change to type=NodePort, which would otherwise stay locked to a gone LB.
  • No update loop. The update triggers one more EnsureLoadBalancer, which finds nothing to change.
  • No new RBAC. It needs services update, which the service controller already uses.

Why off by default

  • Cilium (kube-proxy replacement, bpf.lbSourceRangeAllTypes=true) applies the field to the NodePort, and only to traffic arriving on the node's network interface. In-cluster clients are unaffected.
  • kube-proxy doesn't apply it to the NodePort. It does apply it to in-cluster traffic to the LB IP, so pods calling the Service through that IP would be dropped.

Tests

  • go test ./... passes.
  • ./hack/golangci-lint.sh reports 0 issues.
  • New table tests cover:
    • range merging;
    • LB IP selection;
    • env vs annotation;
    • opt-out;
    • customer-owned entries;
    • an IP change;
    • a conflict retry;
    • cleanup after a type change or deletion;
    • one EnsureLoadBalancer end-to-end case with a fake clientset.

Not yet validated on a live cluster.

Known limit

The NodePort is open from Service creation until the LB exists and its IP has been written, usually seconds.

A LoadBalancer Service's NodePort is open on every node address. Without a
private network or firewall in front of the nodes, it is reachable directly
on each node's public IP, bypassing the Load Balancer.

With HCLOUD_LOAD_BALANCERS_RESTRICT_NODE_PORT_SOURCE=true, or the
load-balancer.hetzner.cloud/restrict-node-port-source annotation, the CCM
adds the Load Balancer's IPv4 (public, and private if attached to a network)
as /32 entries to spec.loadBalancerSourceRanges. A Service proxy that applies
the field to the NodePort (Cilium with bpf.lbSourceRangeAllTypes=true) then
drops everything else.

- Runs at the end of EnsureLoadBalancer. The cloud-provider service
  controller resets the spec before patching, so the update goes through a
  client created in cloud.Initialize.
- Keeps entries set by others. The entries the CCM owns are recorded in the
  read-only load-balancer.hetzner.cloud/node-port-source-ranges annotation,
  so it can replace them when the LB IP changes and remove them on opt-out
  or in EnsureLoadBalancerDeleted (e.g. after a change to type NodePort).
- Off by default: kube-proxy applies the field to in-cluster traffic to the
  LB IP, so enabling it there drops pods calling the Service via that IP.
@batistein
batistein requested a review from Rivaldito November 4, 2026 12:40

@Rivaldito Rivaldito left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't test, but LGTM

- Skip Services whose ranges Cilium treats as a deny-list
  (service.cilium.io/src-ranges-policy: deny); adding the LB there would
  block the LB itself. Entries added before are removed.
- Write with a JSON merge patch of only the two fields, guarded by the
  resourceVersion read, under field manager hcloud-ccm-node-port-source.
  A full-object update could drop Service fields this client does not know.
- Compare CIDRs by network, so another spelling of the LB /32 counts as the
  customer's entry instead of getting a duplicate.
- An empty restrict-node-port-source value falls back to the default.
- EnsureLoadBalancerDeleted removes the entries after deleting the LB and
  only logs a failure, so a rejected Service update cannot keep the LB or the
  finalizer around.
- Keep existing entries when the LB has no usable IPv4 instead of opening the
  NodePort.
- Docs: type changes need the field removed in the same request, deny-list
  skip, effect on client-CIDR-only lists, externalIPs and UDP, fail-open on
  an earlier sync error.
@batistein
batistein added this pull request to stack #92 November 5, 2026 19:02
@batistein
batistein merged commit bd5b657 into main Nov 6, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants