fix(cloudflare): cache ZoneHasPaidPlan to avoid per-change API amplification#6392
Open
AndrewCharlesHay wants to merge 1 commit into
Open
Conversation
…ication ZoneHasPaidPlan is called from the long-comment truncation path in newCloudFlareChange, which runs once per record change. Each call fired a ListZones + GetZone against the operator's Cloudflare account, so a user able to set the cloudflare-record-comment annotation to a 101+ char string on N ingresses caused 2N API calls per sync cycle. Once Cloudflare rate-limited the operator's account, real DNS changes came back as SoftError and convergence stalled until the throttle lifted. Add a simple TTL cache keyed by zone (15 min default). Plan changes are billing actions, so a generous TTL is fine and it kills the amplification entirely. The cache field is optional: ZoneHasPaidPlan still works against a hand-constructed provider (nil cache) so existing tests keep compiling without modification. Fixes the rate-limit half of kubernetes-sigs#6391. The Custom Hostname allowlist (the other half) is tracked in the same issue and will follow up once the maintainer direction is agreed. Signed-off-by: Andrew Hay <andrew.hay@benchmarkanalytics.com>
Contributor
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Coverage Report for CI Build 24868400870Coverage increased (+0.03%) to 80.494%Details
Uncovered ChangesNo uncovered changes found. Coverage Regressions31 previously-covered lines in 1 file lost coverage.
Coverage Stats
💛 - Coveralls |
Member
|
we have a blueprint for cache https://github.com/kubernetes-sigs/external-dns/tree/master/provider/blueprint /hold |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses the second of the two HIGH items in #6391 (the rate-limit amplification). Leaving the Custom Hostname allowlist for a separate PR once maintainer direction on that is agreed.
What's wrong today
ZoneHasPaidPlanis called fromnewCloudFlareChangeviatrimAndValidateCommentwhenever a record comment exceeds 100 characters. Each call fires aListZones(paginated) plus aGetZoneagainst the operator's Cloudflare account, with no caching.Because the comment is set by the
external-dns.alpha.kubernetes.io/cloudflare-record-commentannotation, any user with create rights in a watched namespace can set a 101-char comment on N ingresses and cause2NCloudflare API calls per sync cycle. Once the operator's account gets rate-limited, real DNS changes come back asSoftErrorand the cluster's DNS stops converging until Cloudflare cools off.The fix
Add a small TTL-based in-memory cache keyed by zone name (TLD+1), defaulting to 15 minutes. Plan changes are billing actions by the account owner, so a generous TTL introduces no meaningful staleness and eliminates the per-change fan-out entirely.
zone_plan_cache.go— the cache itself, with anowfield overridable for deterministic TTL tests.cloudflare.go—ZoneHasPaidPlanchecks the cache first, populates it on miss.zonePlanCachefield is nil (hand-constructed provider in existing tests),ZoneHasPaidPlanfalls back to the original behavior. This keeps the existingTestZoneHasPaidPlanand other hand-constructed provider tests working unmodified.Tests
TestZonePlanCache_HitAndMiss— basic get/set.TestZonePlanCache_TTLEvicts— uses an injectable clock to advance past the TTL and confirm eviction.TestZoneHasPaidPlan_Cached— populates the cache, then breaks the underlying API and confirms repeated calls still return the cachedtrue(proves the cache actually short-circuits).TestZoneHasPaidPlan_NilCache— confirms the nil-cache path still works end-to-end.Existing
TestZoneHasPaidPlanpasses without modification.Test plan
go test ./provider/cloudflare/...go test -race ./provider/cloudflare/...gofmt -l provider/cloudflare/*.gogo build ./...Relates to #6391.