projectcalico / calico

Cloud native networking and network security
https://docs.tigera.io/calico/latest/about/
Apache License 2.0
6.04k stars 1.35k forks source link

Reduce memory used to store inactive policies/profiles #9514

Open fasaxc opened 3 days ago

fasaxc commented 3 days ago

Description

The Rule struct that is used in policies and profiles has grown very large over time. With allocation overheads, policies end up using approximately 1KB per rule, which is excessive.

In the active rules calculator, store inactive policies in a compressed format; first encoding them as JSON, then compressing with "snappy", a lightweight and high speed compression algorithm. The data is already marked up for JSON serialisation (as used by Typha to serialise the stream to Felix) so this should be very safe. Unpack the policies on demand.

In the policy resolver, avoid storing the whole policy and extract the needed metadata in a compact struct. Simplify the policy sorter by replacing nil with +Inf, which sorts exactly as we want. This avoids boxing a float; it can be stored directly in the struct instead.

Related issues/PRs

Todos

Release Note

Felix now compresses network policies and namespaces in memory, reducing RAM usage in clusters with many policies/namespaces that are not active on a given node.

Reminder for the reviewer

Make sure that this PR has the correct labels and milestone set.

Every PR needs one docs-* label.

Every PR needs one release-note-* label.

Other optional labels: