Repository navigation
[rds/kubernetes] Share one generic lister across the four resource types - #1486
Conversation
pods, endpoints, services and ingresses each carried their own copy of the same lister: an identical struct, a URL builder differing only in the API prefix and resource kind, a parseXJSON, an expand(), and a byte-identical refresh goroutine. Replace all four with a generic resourceLister[T resourceInfo] in lister.go. Each resource type now supplies only what actually differs: its metadata, and how it expands into RDS resources. Also fix a bug that the four copies of expand() shared: on an API or parse error they logged a warning and fell through to overwrite the cache with nil, so a transient failure dropped every target for that resource type until the next successful refresh. Downstream there is no net for this, because an emptied cache reaches the RDS client as a successful response with zero resources, not as an error. expand() now keeps the last good result instead. This matters more once API calls time out (#1485), as a stall then surfaces as exactly such an error rather than hanging, which is what preserved the cache before. Behavior is otherwise preserved, including two deliberate asymmetries: - Ingresses filter on name and labels after expansion, not on the Kubernetes object, because their resource names are derived per rule and they carry their own fqdn and relative_url labels. The other types filter on the object, where a name filter matches the object name rather than the derived per-port resource names. - Pods ignore a name in the resource path ("pods/pod-a"), which the other three honor. Kept as the nameInPath field so that existing configs don't silently narrow from all pods to one. While here: drop rand.Seed, deprecated since Go 1.20 and called once per lister at startup; jitter the first refresh at full resolution instead of whole seconds; drop the unused per-type config fields, as all four config messages are empty; and drop the error return from the four constructors, which was never non-nil. The listers' test assertions are unchanged; only the call sites of the removed helpers were updated.
The existing tests pinned the filtering and expansion logic well, but
nothing covered what the shared lister actually introduced: url(),
expand(), start(), and the four constructors were all at 0%, so a wrong
api prefix, resource kind, nameInPath or keep would compile, pass every
test, and silently return no targets in production.
Add tests for:
- the API server path each lister asks for, with and without a
namespace, driven through the real constructors
- expand() keeping the last good result when the API call fails or the
response doesn't parse, and replacing it when it succeeds
- pods ignoring a name in the resource path while services honor it
- the pods lister caching only running pods
Package coverage goes from 65.2% to 82.6%, with everything in lister.go
covered except start()'s refresh loop, which is a ticker.
Verified by mutation: reverting expand() to the old fall-through, or
breaking any of the per-type fields, now fails the tests.
|
Added a second commit with the coverage this refactor was missing. Before: 65.2% package coverage, but badly distributed. The existing tests pinned the filtering and expansion logic well ( After: 82.6%. Everything in New tests: the API path each lister requests (with and without a namespace), Verified by mutation rather than by the coverage number — I broke the implementation eight ways and confirmed each one fails:
One note for review: in Still uncovered and pre-existing, not addressed here: |
A re_eval_sec of 0 in an rds_server provider config panicked the refresh goroutine on time.NewTicker(0), which is unrecoverable and takes the process down. An unset re_eval_sec defaults to 60 and k8s_targets forces 30, so only an explicit zero or a negative value reached it. Reject it in New() instead, where it surfaces as a config error at startup. The jitter guard in start() goes away with it: it covered only the rand call and not the ticker two lines below, so it read as if a non-positive interval were handled when it wasn't. Also fix a stale reference to listFilters.matchesObject, which is now listFilters.matches.
Services, endpoints and ingresses all treat a name in the resource path as a selector: "services/service-a" returns just that service. Pods ignored it and returned every pod in the cluster, so a resource_path of "k8s://pods/my-pod" silently gave back a much broader target set than it asked for. Apply the selector for every resource type, which also removes the nameInPath field that existed only to preserve the difference. This is a behavior change: a config that lists pods by a resource path with a name in it will now get that one pod rather than all of them. It is the behavior such a config was asking for, and targeting pods directly is uncommon compared to services, endpoints and ingresses.
listResources dereferences rl.cache[key] and calls metadata() on the result, which relies on every key having a cache entry. That holds because parseResourceList fills both in the same loop and expand() replaces them together under the lock, but nothing said so, and a missing entry would be a nil dereference rather than a miss. Also correct lister.go's copyright year, which came along with the header when the file was split out.
pods, endpoints, services and ingresses each carried their own copy of the same lister: an identical struct, a URL builder differing only in the API prefix and resource kind, a
parseXJSON, anexpand(), and a byte-identical refresh goroutine.All four are now a generic
resourceLister[T resourceInfo]inlister.go. Each resource type supplies only what actually differs — its metadata, and how it expands into RDS resources:Cache wipe on error
The four copies of
expand()shared a bug: on an API or parse error they logged a warning and fell through to overwrite the cache withnil, dropping every target for that resource type until the next successful refresh.There's no net for this downstream — an emptied cache reaches the RDS client as a successful response with zero resources, not as an error, so
updateStatefaithfully replaces its cache with empty.It mattered little while calls could hang, since a hang preserved the cache. It matters more once API calls time out (#1485), because a stall then surfaces as exactly this kind of fast error.
expand()now keeps the last good result.That PR and this one touch no overlapping files and can merge in either order.
Behavior changes
k8s://pods/my-podused to return every pod in the cluster; services, endpoints and ingresses have always treated the name as a selector. Pods now match them. A config using such a path gets the one pod it asked for rather than all of them. This is the only change here that can alter an existing deployment's target set.re_eval_secmust be positive. An explicit0used to panic the refresh goroutine ontime.NewTicker(0), taking the process down; it is now a config error fromNew(). An unset value still defaults to 60, andk8s_targetsforces 30, so only an explicit zero or a negative value is affected.Preserved asymmetry
Ingresses filter name/labels after expansion, not on the Kubernetes object, because their resource names are derived per rule (
<ingress>_<host>_<path>) and they carry their ownfqdn/relative_urllabels. The other types filter on the object, where a name filter matches the object name rather than derived per-port resource names. Flattening this either way would silently change target selection.Also
Drops
rand.Seed(deprecated since Go 1.20, called once per lister at startup) and jitters the first refresh at full resolution instead of whole seconds; drops the unused per-type config fields, as all four config messages are empty; drops theerrorreturn from the four constructors, which was never non-nil; documents thekeys/cacheinvariant thatlistResourcesrelies on when it dereferences a cache entry.Testing
Coverage went from 65.2% to 83.9%. The starting number was badly distributed: the existing tests covered the filtering and expansion logic well, and everything the refactor introduced —
url(),expand(),start(), all four constructors — was at zero, so a wrong api prefix or resource kind would compile, pass every test, and silently return no targets.The listers' original test assertions are unchanged, which is the evidence the refactor preserves behavior. New tests cover the API path each lister requests (with and without a namespace),
expand()keeping the last good result on API and parse failures, the resource-path selector, the running-pods filter, and there_eval_secrejection — all driven through the real constructors so the per-type wiring is observable.Verified by mutation rather than by the coverage number: breaking the implementation eleven ways (wrong api prefix, wrong kind,
url()ignoring the namespace,expand()reverted to the old fall-through,keepremoved,runningPodaccepting every phase,matches()ignoring either filter,listResourcesdropping the namespace filter or the path selector, ingresses filtering the object instead of expanded resources) fails the tests in every case.Still uncovered, pre-existing and not addressed:
ListResources()inkubernetes.go.Review
A cold review of the branch raised three items, all addressed: the half-guarded
NewTickerpanic (now rejected inNew()), the pods resource-path inconsistency (now unified), and a stale doc reference tolistFilters.matchesObjectleft over from collapsing two identical match helpers intomatches.