Skip to content

fix(cloudflare): improve handling of rate limiting errors#5524

Merged
k8s-ci-robot merged 8 commits intokubernetes-sigs:masterfrom
Hackatosh:fix-cloudflare-rate-limit-error-handling
Jun 19, 2025
Merged

fix(cloudflare): improve handling of rate limiting errors#5524
k8s-ci-robot merged 8 commits intokubernetes-sigs:masterfrom
Hackatosh:fix-cloudflare-rate-limit-error-handling

Conversation

@Hackatosh
Copy link
Contributor

@Hackatosh Hackatosh commented Jun 13, 2025

What does it do ?

When a rate limiting error is encountered while using Cloudflare provider, external dns crashes. This PR fixes this behavior.

Motivation

Cloudflare library return a generic error when a rate limiting error is encountered (see #4876 (comment)). I added a condition to handle this type of error better and return a soft error.

An issue has been opened on Cloudflare side (cloudflare/cloudflare-go#4155) and a fix has been submitted (cloudflare/cloudflare-go#4156), but unfortunately there was no reaction from the library maintainers. That's why I try to fix the issue on external-dns side, even if it feels more like a "hack"

More

  • [ X] Yes, this PR title follows Conventional Commits
  • [X ] Yes, I added unit tests
  • [ X] Yes, I updated end user documentation accordingly

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jun 13, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot k8s-ci-robot requested a review from mloiseleur June 13, 2025 08:35
@k8s-ci-robot k8s-ci-robot added the provider Issues or PRs related to a provider label Jun 13, 2025
@k8s-ci-robot k8s-ci-robot requested a review from szuecs June 13, 2025 08:35
@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Jun 13, 2025
@k8s-ci-robot
Copy link
Contributor

Welcome @Hackatosh!

It looks like this is your first PR to kubernetes-sigs/external-dns 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/external-dns has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jun 13, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @Hackatosh. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Jun 13, 2025
@Hackatosh Hackatosh changed the title improve handling of rate limiting errors for Cloudflare provider fix(cloudflare): improve handling of rate limiting errors for Cloudflare provider Jun 13, 2025
@vflaux
Copy link
Contributor

vflaux commented Jun 13, 2025

I can't say whether this will be accepted, but regarding the code, I would suggest adding a new function isRateLimited() that checks both apiErr.ClientRateLimited() and the error message. Then, I would replace each call to apiErr.ClientRateLimited() with this new function.

@Starttoaster
Copy link

Starttoaster commented Jun 13, 2025

I agree with you about making a function for this duplicated code. But can you expand on why this wouldn't be accepted? This is a huge problem for multi-cluster multi-domain setups using external-dns, it's very likely you'll run into rate limit errors with the default polling interval. That means the only solution is to increase the polling interval with each new cluster or domain that external-dns manages records for, which I'd argue isn't a great solution or user experience.

With Cloudflare cold shouldering the fix on their end, it seems silly to not at least temporarily account for a known quirk of their API library here.

@ivankatliarchuk
Copy link
Member

@Hackatosh any chance you sign EasyCLA so that we could review it?

@ivankatliarchuk
Copy link
Member

This PR make sense, but not sure if it will be accepted as well

There was previous attempts #5233 to do something similar and similar issue #5225 (comment)

No need to close this; the idea itself makes sense. I'm very much on the fence about the order of resolution as well. Adding soft errors first feels like treating symptoms instead of the root cause.

External DSN currently abusing Cloudflare API, which is not correct. We should be good citizens and improve not just crash/no-crash, but actually start adding retry/backkoff etc to cloudflare provider.

Example

config, err = cloudflare.NewWithAPIToken(token, []cloudflare.Option{
			cloudflare.UsingRateLimit(rateLimit),
			cloudflare.UsingRetryPolicy(retry, minDelaySeconds, maxDelaySeconds), // 3 retries, min 1s delay, max 5s delay
			cloudflare.UserAgent(externaldns.UserAgent()),
		}...)

Something like that required as well. As at the moment, cloudflare API as it looks, uses some defaults

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jun 16, 2025
@Hackatosh
Copy link
Contributor Author

Hackatosh commented Jun 16, 2025

@ivankatliarchuk I have signed the CLA, you can review the PR :)

We could improve retry/backoff policy if you want. What do you think would be appropriate ? Maybe we can make this configurable using environnement variables

@ivankatliarchuk
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jun 16, 2025
@ivankatliarchuk
Copy link
Member

ivankatliarchuk commented Jun 16, 2025

I can see there were an attempt to fix #4437 and agree with the comment that the issue is with the upstream library.

As temp solution I think it should be acceptable to implement the fix.

Just follow the suggestion #5524 (comment) and make sure to add unit tests for all added lines.

We could improve retry/backoff policy if you want. What do you think would be appropriate ? Maybe we can make this configurable using environnement variables

If you have time and interest, you could try to improve retry/backoff in follow-up for sure

@ivankatliarchuk
Copy link
Member

How do you know, the fix is actually solves the problem?

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jun 16, 2025
@Hackatosh
Copy link
Contributor Author

I factorized error handling in convertCloudflareError and I added one unit test to test that the the condition I have added works correctly. The error I use for the test is identical to the one thrown by Cloudflare

To know if the fix works correctly, I am going to build external-dns with this fix and try it on a staging environnement where external-dns crashes everyday due to this issue, and see what happens. Is that OK for you ?

return provider.NewSoftError(err)
}
}
if strings.Contains(err.Error(), "exceeded available rate limit retries") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment as a remainder that this is a workaround for an issue with the cloudflare client ?
So we know that we can remove this when fixed ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment

@Hackatosh
Copy link
Contributor Author

I tried the fix on our staging environnement and got logs like this instead of crash :

{"level":"error","msg":"Failed to do run once: could not fetch records from zone, soft error\nexceeded available rate limit retries (consecutive soft errors: 1)","time":"2025-06-16T17:09:15Z"}
{"level":"error","msg":"Failed to do run once: soft error\nexceeded available rate limit retries (consecutive soft errors: 2)","time":"2025-06-16T17:11:33Z"}
{"level":"info","msg":"All records are already up to date","time":"2025-06-16T17:14:45Z"}
{"level":"info","msg":"Reconciliation succeeded after 2 consecutive soft errors","time":"2025-06-16T17:14:45Z"}

For me it works correctly :D Is it ok for you to merge this PR ? 🙏

@ivankatliarchuk
Copy link
Member

Hi @vflaux anything left on your side ?

@k8s-ci-robot
Copy link
Contributor

@vflaux: changing LGTM is restricted to collaborators

Details

In response to this:

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Hackatosh and others added 4 commits June 19, 2025 09:27
Co-authored-by: vflaux <38909103+vflaux@users.noreply.github.com>
Co-authored-by: vflaux <38909103+vflaux@users.noreply.github.com>
Co-authored-by: vflaux <38909103+vflaux@users.noreply.github.com>
@ivankatliarchuk
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 19, 2025
Copy link
Contributor

@vflaux vflaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@k8s-ci-robot
Copy link
Contributor

@vflaux: changing LGTM is restricted to collaborators

Details

In response to this:

lgtm

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@Hackatosh
Copy link
Contributor Author

Hello ! @ivankatliarchuk Does someone else need to approve this PR ? 🤔

@ivankatliarchuk
Copy link
Member

cc @mloiseleur for final review

@mloiseleur
Copy link
Collaborator

Many thanks for your review @vflaux 👍
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mloiseleur, vflaux

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 19, 2025
@mloiseleur mloiseleur changed the title fix(cloudflare): improve handling of rate limiting errors for Cloudflare provider fix(cloudflare): improve handling of rate limiting errors Jun 19, 2025
@k8s-ci-robot k8s-ci-robot merged commit 0fae060 into kubernetes-sigs:master Jun 19, 2025
14 checks passed
@Hackatosh Hackatosh deleted the fix-cloudflare-rate-limit-error-handling branch June 20, 2025 06:06
@Hackatosh
Copy link
Contributor Author

Thank you for the merge ! :)

Do you know when this fix will be released in the official image ?

@mloiseleur
Copy link
Collaborator

You can use the staging image to use it before official release.
The release of 0.18 is planned for next week, see #5545

@Hackatosh
Copy link
Contributor Author

Thank you very much !

Thank you for your reactivity on this PR, it was a nice experience to contribute :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. provider Issues or PRs related to a provider size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants