Skip to content

feat: manage an existing firewall via spec.firewall-id (node-pool firewalls) - #39

Open
aweingarten wants to merge 1 commit into
linode:mainfrom
aweingarten:feat/adopt-existing-firewall
Open

aweingarten wants to merge 1 commit into
linode:mainfrom
aweingarten:feat/adopt-existing-firewall

Conversation

@aweingarten

@aweingarten aweingarten commented Oct 14, 2026 •

Copy link
Copy Markdown

Problem

The controller can't be used on clusters whose node pools already attach a Cloud Firewall (the node pool firewall_id, set through the API, Cloud Manager or Terraform's linode_lke_node_pool). LKE attaches that firewall to every node the pool creates, and the controller fights it:

  • spec.firewall-id is declared on the CRD but never read.
  • With status.firewall-id empty, the first reconcile always calls CreateFirewall for lke-<clusterID> with every node as a device, and later reconciles add and remove devices.
  • Hand-setting status.firewall-id isn't safe either: deleting the CloudFirewall (including through the owner reference on uninstall) runs the finalizer, which deletes that firewall.

This also affects App Platform, which ships this controller as its linode-cfw app. #1 noted that importing an existing firewall ID was still needed; this implements it.

Change

With spec.firewall-id set, the controller adopts that firewall and reconciles only its rules: the defaults if defaultRules: true, plus spec.ruleset. These replace the firewall's inbound rules, outbound rules and both policies. It never creates or recreates the firewall, never manages its devices, never deletes it, and puts no finalizer on the object.

Ownership is positive. The controller only deletes, or manages the devices of, a firewall it created: one labelled lke-<clusterID> that isn't recorded as imported.

  • Annotation: the adopted firewall is recorded in the networking.linode.com/imported-firewall-id annotation. It lives in metadata, so a CRD older than the controller can't prune it.
  • Loss is safe: losing the annotation never gets a firewall deleted, because deletion requires the controller's label.

Transitions:

  • Adding firewall-id to an object whose firewall the controller created:

    • applies the rules to the adopted firewall;
    • moves each node onto it, one detach-and-attach at a time, since a Linode can only be on one Cloud Firewall;
    • then deletes the old firewall.

    Each step can be safely retried.

  • Changing firewall-id to another ID adopts the new firewall and leaves the previous one in place.

  • Removing firewall-id releases the firewall untouched.

Safety checks:

  • Explicit defaultRules: import mode requires defaultRules to be set explicitly, and defaultRules: false requires inbound rules. These are CEL rules on the CRD, checked in the controller too. An empty inbound ruleset under the DROP policy would cut the nodes off.
    • To make "unset" detectable, the schema default on defaultRules is removed. The controller still treats unset as true, so behaviour without firewall-id is unchanged.
  • Foreign devices are refused: if the firewall is attached to any device outside this cluster, it isn't adopted. A Linode in this LKE cluster that hasn't registered as a Node yet doesn't count as foreign. Linode-interface devices are matched to their parent Linode.
  • Conditions and Events: two status conditions, Imported and NodesProtected, plus Events, report adoption, refusals and unattached nodes.
  • --auto-create-default flag: controls whether the Node watch creates an empty kube-system/primary object (default true). The chart turns it off when firewall.firewallID is set, so a default object can't create a second firewall before the chart's object lands. Import-mode objects are never migrated onto the default ruleset.
  • Deletion before the rate limiter: deletion is handled before the rate limiter, and finalizer-removal errors are returned so they're retried.

Fixes to existing behaviour:

  • removeNodes: passed a Linode ID where DeleteFirewallDevice takes a device ID. It now looks up the device first.
  • Deferred status write: the end-of-reconcile write dropped a status change made in the same reconcile as a metadata change. It now writes both.
  • FirewallIsNotFound: no longer dereferences a nil *linodego.Error.

Also included:

  • CRD: firewall-id is documented and validated, there are CEL rules and status conditions, in config/crd/bases and the Helm copy.
  • Chart:
    • firewall.firewallID accepts integers only;
    • firewall.defaultRules is required when firewallID is set;
    • new autoCreateDefault value;
    • events.k8s.io RBAC.
  • Events API: events use the non-deprecated recorder.
  • README: a section on node-pool firewalls.

Questions for maintainers

  1. Shared firewalls: any firewall with non-cluster devices is refused. Would you rather support shared firewalls behind an opt-in?
  2. Removing the defaultRules schema default: it's needed to detect an unset value in import mode. Existing objects keep the true the old schema stored. Is that acceptable, or would you prefer a separate field?
  3. App Platform: linode-cfw vendors this CRD under crds/, which Helm doesn't upgrade. Against that older CRD, everything here still works except the admission-time checks. defaultRules is defaulted to true there, so the explicit-choice check only holds with the new CRD or through the chart.

Known limits

  • Not run live: I haven't run this against a live LKE or LKE Enterprise cluster. The device shapes (linode, linode_interface) and the Linode-per-firewall limit come from linodego and the API docs.
  • Interface-based nodes: on a cluster whose nodes use Linode interfaces, the switch-over attaches nodes as linode devices, which may not be valid there.
  • Uninstall: for an owned firewall, the finalizer can still be left stuck if the controller's Deployment is deleted along with the object. That's existing behaviour (cleanup of controller owned firewalls #2), and imported firewalls no longer carry a finalizer.

Testing

internal/controller/import_test.go runs envtest specs against a stateful fake Linode API, using linodego's LINODE_URL. The fake keeps labels, stored rules, device IDs and instances. The specs run against both the current CRD and the CRD from before firewall-id was supported (testdata/crd-before-import). They cover:

  • adoption, with rules applied once and not re-applied once they match;
  • interface devices;
  • a cluster Linode that hasn't registered as a Node yet;
  • unattached nodes;
  • a missing firewall;
  • foreign and shared firewalls;
  • release;
  • lost status;
  • the annotation lost while firewall-id changes;
  • switch-over from an owned firewall, including that rules are applied before nodes move;
  • re-import to a different firewall;
  • owned node removal by device ID;
  • a firewall pointed at by hand without an import marker;
  • auto-create on and off;
  • the CEL and controller checks for defaultRules and inbound rules.

There are also unit tests for deviceLinodeID and validateImportSpec.

Each protection was checked by breaking it and confirming the matching specs fail against both CRDs:

  • ownership without the label check;
  • no node move on switch-over;
  • the old device-ID bug;
  • always adding the finalizer;
  • no check for joining nodes;
  • ignoring the auto-create flag;
  • applying rules after the move.

Notes on the existing baseline:

  • The scaffolded spec in cloudfirewall_controller_test.go already fails on main (failed to get API credentails: resource name may not be empty). I left it alone.
  • golangci-lint run reports 46 issues against 48 on main, and --new-from-rev=origin/main reports none.
  • helm lint passes, and the template was rendered with and without the import values.

@aweingarten
aweingarten marked this pull request as draft October 14, 2026 00:18
@aweingarten
aweingarten marked this pull request as ready for review October 14, 2026 00:27
@aweingarten
aweingarten marked this pull request as draft October 14, 2026 02:44
@aweingarten
aweingarten force-pushed the feat/adopt-existing-firewall branch from d64d6d3 to 6a78ad1 Compare October 14, 2026 02:51
@aweingarten
aweingarten marked this pull request as ready for review October 14, 2026 02:51
spec.firewall-id was declared on the CloudFirewall CRD but never read, so
the controller always created its own lke-<clusterID> firewall and tried to
attach every node to it. That conflicts with a firewall attached through an
LKE node pool's firewall_id, which LKE attaches to every node it creates.

When spec.firewall-id is set the controller adopts that firewall and
reconciles only its rules. It never creates or recreates it, never manages
its devices, never deletes it, and puts no finalizer on the object.

Ownership is positive: the controller only deletes, or manages the devices
of, a firewall it created, i.e. one labelled lke-<clusterID> that is not
recorded as imported. The adopted firewall is recorded in the
networking.linode.com/imported-firewall-id annotation (metadata, so an
older CRD cannot prune it), but losing it can never get a firewall deleted.

Transitions:
- Adding firewall-id to an object whose firewall the controller created
  applies the rules to the imported firewall, moves each node onto it
  (detach then attach, one node at a time; a Linode can only be on one
  Cloud Firewall), then deletes the old firewall. Idempotent on retry.
- Changing firewall-id adopts the new firewall and leaves the old one.
- Removing it releases the firewall untouched.

Safety:
- Import mode requires defaultRules to be set explicitly, and
  defaultRules=false requires inbound rules (CEL rules on the CRD, checked
  in the controller too). The schema default on defaultRules is removed so
  "unset" is detectable; the controller still treats unset as true.
- A firewall attached to any device outside this cluster is refused; a
  Linode in this LKE cluster that has not registered as a Node is not
  foreign. Linode-interface devices are matched through their parent.
- Status conditions Imported and NodesProtected, plus Events, report
  adoption, refusals and unattached nodes.
- --auto-create-default (default true) gates the Node watch creating an
  empty kube-system/primary object; the chart turns it off with firewallID.
  Import-mode objects are never migrated onto the default ruleset.
- Deletion is handled before the rate limiter, the finalizer is only added
  for owned firewalls, and finalizer removal errors are returned.

Fixes to existing behaviour:
- removeNodes passed a Linode ID where DeleteFirewallDevice takes a device
  ID; devices are now looked up first.
- The deferred write no longer drops a status change made in the same
  reconcile as a metadata change.
- FirewallIsNotFound no longer dereferences a nil *linodego.Error.

Also: CRD documents and validates firewall-id (config + helm copy); chart
gains firewall.firewallID (integers only), firewall.defaultRules (required
with firewallID) and autoCreateDefault; events.k8s.io RBAC; README section.

Tests: envtest specs against a stateful fake Linode API, run against both
the current CRD and the pre-firewall-id CRD.

Refs linode#1
@aweingarten
aweingarten force-pushed the feat/adopt-existing-firewall branch from 6a78ad1 to 359786b Compare October 15, 2026 16:22
tameemakhtar added a commit to tameemakhtar/cloud-firewall-controller that referenced this pull request Nov 5, 2026
The controller already honours spec.defaultRules on the CloudFirewall
(unset or true merges the compiled default rules, false applies only
spec.ruleset), but the chart left it as a comment, so a values file
could not turn the compiled defaults off.

Render spec.defaultRules when firewall.defaultRules is set, with the
same key and template as upstream PR linode#39. When the key is not set,
nothing is rendered and the CRD default (true) applies, as before.

This branch has not been deployed

No deployments
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.

1 participant