|
| 1 | +--- |
| 2 | +title: "Designing Lean Operators" |
| 3 | +linkTitle: "Designing Lean Operators" |
| 4 | +weight: 5 |
| 5 | +description: This guide describes good practices concepts to designing lean Operators. |
| 6 | +--- |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +One of the pitfalls that many operators are failing into is that they watch resources with high cardinality like secrets possibly in all namespaces. This has a massive impact on the memory used by the controller on big clusters. Such resources can be filtered by label or fields. The original doc design for `Filter cache ListWatch using selectors` can be accessed from [here][Filter cache ListWatch using selectors] |
| 11 | + |
| 12 | +**IMPORTANT NOTE** |
| 13 | +Requests to a client backed by a filtered cache for objects that do not match the filter will never return anything. In other words, filtered caches make the filtered-out objects invisible to the client. |
| 14 | + |
| 15 | +## How is this done ? |
| 16 | + |
| 17 | +- When creating the manager, you can override the default NewCache function |
| 18 | +- Each client.Object can be filtered with labels and fields |
| 19 | + |
| 20 | +## Examples |
| 21 | + |
| 22 | +In this scenario, the user will override the NewCache function to filter the secret object by it's label. This will return a filtered cache for objects that match the filter. |
| 23 | + |
| 24 | +```yaml |
| 25 | +mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ |
| 26 | + NewCache: cache.BuilderWithOptions(cache.Options{ |
| 27 | + SelectorsByObject: cache.SelectorsByObject{ |
| 28 | + &corev1.Secret{}: { |
| 29 | + Label: labels.SelectorFromSet(labels.Set{"app": "app-name"}), |
| 30 | + }, |
| 31 | + }, |
| 32 | + }), |
| 33 | +}) |
| 34 | +``` |
| 35 | + |
| 36 | +In this scenario, the user will override the NewCache function to filter the node object by it's field name. This will return a filtered cache for objects that match the filter. |
| 37 | + |
| 38 | +```yaml |
| 39 | +mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ |
| 40 | + NewCache: cache.BuilderWithOptions(cache.Options{ |
| 41 | + SelectorsByObject: cache.SelectorsByObject{ |
| 42 | + &corev1.Node{}: { |
| 43 | + Field: fields.SelectorFromSet(fields.Set{"metadata.name": "node01"}), |
| 44 | + }, |
| 45 | + }, |
| 46 | + }), |
| 47 | +}) |
| 48 | +``` |
| 49 | + |
| 50 | +[Filter cache ListWatch using selectors]: https://github.com/kubernetes-sigs/controller-runtime/blob/master/designs/use-selectors-at-cache.md |
0 commit comments