Skip to content

[rds/kubernetes] Share one generic lister across the four resource types - #1486

Merged
manugarg merged 5 commits into
mainfrom
k8s-lister-dedup
Sep 12, 2026
Merged

manugarg merged 5 commits into
mainfrom
k8s-lister-dedup

Conversation

@manugarg

@manugarg manugarg commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

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.

All four are now a generic resourceLister[T resourceInfo] in lister.go. Each resource type supplies only what actually differs — its metadata, and how it expands into RDS resources:

type resourceInfo interface {
	metadata() kMetadata
	resources(f *listFilters, l *logger.Logger) []*pb.Resource
}

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 with nil, 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 updateState faithfully 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

  • A name in the resource path now selects a single pod. k8s://pods/my-pod used 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_sec must be positive. An explicit 0 used to panic the refresh goroutine on time.NewTicker(0), taking the process down; it is now a config error from New(). An unset value still defaults to 60, and k8s_targets forces 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 own fqdn/relative_url labels. 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 the error return from the four constructors, which was never non-nil; documents the keys/cache invariant that listResources relies 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 the re_eval_sec rejection — 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, keep removed, runningPod accepting every phase, matches() ignoring either filter, listResources dropping 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() in kubernetes.go.

Review

A cold review of the branch raised three items, all addressed: the half-guarded NewTicker panic (now rejected in New()), the pods resource-path inconsistency (now unified), and a stale doc reference to listFilters.matchesObject left over from collapsing two identical match helpers into matches.

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.
@manugarg

Copy link
Copy Markdown
Contributor Author

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 (resources(), matches(), listResources, parseResourceList were all 90%+) — and everything the refactor actually introduced was at zero: url(), expand(), start(), and all four constructors. A wrong apiPrefix, kind, nameInPath or keep would compile, pass every existing test, and silently return no targets in production.

After: 82.6%. Everything in lister.go is covered except start()'s ticker loop.

New tests: the API path each lister requests (with and without a namespace), expand() keeping the last good result on API and parse failures, pods ignoring a name in the resource path while services honor it, and the pods lister caching only running pods. All of them go through the real constructors rather than hand-built structs, which is what makes the per-type wiring observable.

Verified by mutation rather than by the coverage number — I broke the implementation eight ways and confirmed each one fails:

mutation caught
ingresses apiPrefix → api/v1 yes
endpoints kind → pods yes
url() ignores namespace yes
expand() reverted to the old fall-through yes
pods keep filter removed yes
runningPod accepts every phase yes
matches() ignores the name filter yes
matches() ignores the labels filter yes
listResources drops the namespace filter yes
ingresses filter the object instead of expanded resources yes

One note for review: in expand(), the early return after the getURL error is not independently observable, because getURL always returns a nil body alongside its error, so the parse-error return backstops it. Removing either one alone still passes; removing both reproduces the original bug and fails. Keeping both as defense in depth — worth knowing that the first one isn't load-bearing on its own.

Still uncovered and pre-existing, not addressed here: New() and ListResources() in kubernetes.go.

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.
@manugarg
manugarg merged commit 882d3fe into main Sep 12, 2026
13 checks passed
@manugarg
manugarg deleted the k8s-lister-dedup branch September 12, 2026 06:02
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