Skip to content

Conversation

valerian-roche
Copy link
Contributor

@valerian-roche valerian-roche commented Jul 1, 2025

What does it do ?

This PR adds transformers to the pod informers in the pod and service sources to reduce the resulting memory footprint.

Motivation

As described in the issue, the pod informers currently lead to a large memory usage of the external-dns pods. As the controller only requires a few specific fields we can leverage transformers in the informers to greatly reduce the footprint.
In our environment this reduces the average memory usage by ~10 times, and once using watch list (disabled by default) and the next client-go release, we now have a peak memory usage 10 times smaller, greatly reducing our controller footprint.

This PR does not enable watchlist (but I'll raise a PR to optionally do so) as it is currently not really valuable without a fix in the next release of client-go.

More

  • Yes, this PR title follows Conventional Commits
  • Yes, I added unit tests
  • Yes, I updated end user documentation accordingly Not applicable, this change is purely technical within the informer

Refs: #5595

@k8s-ci-robot k8s-ci-robot requested a review from mloiseleur July 1, 2025 23:12
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jul 1, 2025
@k8s-ci-robot
Copy link
Contributor

Welcome @valerian-roche!

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
Copy link
Contributor

Hi @valerian-roche. 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.

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 needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 1, 2025
@ivankatliarchuk
Copy link
Contributor

/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 Jul 2, 2025
Copy link
Contributor

@ivankatliarchuk ivankatliarchuk left a comment

Choose a reason for hiding this comment

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

There is a PR open #5583. Not yet merged, as w8 for confirmation.

I think is going to be quite nice to move transfromers under source/informers/transformers.go file. We could identify some cool patterns and parity acros sources.

source/pod.go Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder how this is going to work with FQDN template, we could do some pre-processing and attach it as a field ?

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'll take a look at what is needed here.

Copy link
Contributor Author

@valerian-roche valerian-roche Jul 7, 2025

Choose a reason for hiding this comment

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

Looking at the code I think the informer can be fully split:

  • when fqdnTemplate is unset this change will be used
  • when fqdnTemplate is set we actually do not need the pods at all, only informer events and we can drop even more content. I will update this PR to not use the transformer when the template is set for now.

@valerian-roche
Copy link
Contributor Author

There is a PR open #5583. Not yet merged, as w8 for confirmation.

I think is going to be quite nice to move transfromers under source/informers/transformers.go file. We could identify some cool patterns and parity acros sources.

The shared transformers are an interesting question as each source may use different parts of the pod. I can add one that's the superset (it will still drop a lot of the blueprint) but I'm unsure how this will evolve long term

Copy link
Contributor

@ivankatliarchuk ivankatliarchuk left a comment

Choose a reason for hiding this comment

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

Probably missing evidences that there is a memory reduction. Rest looks quite nice, going to apply similar transfomers elsewhere soon

source/pod.go Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need annotation kubectl.kubernetes.io/last-applied-configuration? Just a question, not a big deal

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good remark, we could at least drop this one. We don't use it internally so had no impact but definitely a potential memory horder for people using apply.
Will add this

source/pod.go Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

Worth to rebase with master branch. This most likely not going to behave correctly in cases when

  1. annotationFilter is present
  2. labelSelector is present

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me rebase and assess what we need to store

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 9, 2025
@valerian-roche
Copy link
Contributor Author

valerian-roche commented Jul 31, 2025

Sorry for the delay, I actually discovered a missed detail when testing the upstream client-go that is needed for this, which led me to prioritize this fix first.

Probably missing evidences that there is a memory reduction.
This PR by itself will improve "average" memory usage over time but not peak memory usage until the fix above is merged in and external-dns go.mod is updating k8s.io/client-go to a version incorporating it (which will hopefully make it for the 0.34 versions).

From internal forks, those are examples of gains with and without the gains from client-go:
without
image

  • solid is actual memory usage, and we can see a major drop when transformers are added
  • dashes is maximum memory usage on the container lifetime (and therefore the memory limit required), which does not change due to client-go ignoring the transformer during the initial list

with (different cluster hence initial state is worse)
image

  • solid also shows an improvement. It's more efficient in this cluster due to a lot of pods with very long templates, but same transformer, showing that the gains will vary significantly based on pod average footprint
  • dashes is maximum memory usage and is now barely higher than average state as client-go applies the transformer on each incoming object as it handles them (this relies on watchlist being active on the cluster, otherwise it will behave as above)

Copy link
Contributor

Choose a reason for hiding this comment

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

How this will behave in a scenario

  • someone decided to execute kubectl patch pod .... and modify/change annotaions, labels or some other value of the pod?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The informer will receive a new object that will be transformed from scratch in this case. Client-go does not alter objects after them being passed to informers

Copy link
Contributor

Choose a reason for hiding this comment

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

gotcha

@mloiseleur
Copy link
Collaborator

@valerian-roche It looks really great. Do you think you can rebase this PR ?

@ivankatliarchuk
Copy link
Contributor

I think this PR will require

  1. The pr to be merged [client-go #1415] Embed proper interface in TransformingStore to ensure DeltaFIFO and RealFIFO are implementing it kubernetes/kubernetes#133263
  2. new release for https://github.com/kubernetes/kubernetes

I think this is a fantastic solution. I'm already looking at how we can apply these improvements to our other sources as well.

Add a transformer to the pods informer of the pod and service sources.

Refs: kubernetes-sigs#5595

Signed-off-by: Valerian Roche <[email protected]>
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 5, 2025
@valerian-roche
Copy link
Contributor Author

I think this PR will require

  1. The pr to be merged [client-go #1415] Embed proper interface in TransformingStore to ensure DeltaFIFO and RealFIFO are implementing it kubernetes/kubernetes#133263
  2. new release for https://github.com/kubernetes/kubernetes

I think this is a fantastic solution. I'm already looking at how we can apply these improvements to our other sources as well.

I rebased the PR. This should be mergeable without the other PR from client-go being available, even if it will bring limited benefits for now

@ivankatliarchuk
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 5, 2025
@mloiseleur mloiseleur changed the title feat: use transformers in pod informers to reduce memory footprint feat(source): use transformers in pod informers to reduce memory footprint Aug 5, 2025
Co-authored-by: Michel Loiseleur <[email protected]>
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 8, 2025
Copy link
Contributor

@ivankatliarchuk ivankatliarchuk left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ivankatliarchuk

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

The pull request process is described here

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 Aug 9, 2025
@k8s-ci-robot k8s-ci-robot merged commit 111df9f into kubernetes-sigs:master Aug 9, 2025
14 checks passed
troll-os pushed a commit to FiligranHQ/external-dns that referenced this pull request Aug 28, 2025
…print (kubernetes-sigs#5596)

* feat: use transformers in pod informers to reduce memory footprint

Add a transformer to the pods informer of the pod and service sources.

Refs: kubernetes-sigs#5595

Signed-off-by: Valerian Roche <[email protected]>

* Do not use transformer when fqdnTemplate is set

* Update source/pod_test.go

Co-authored-by: Michel Loiseleur <[email protected]>

---------

Signed-off-by: Valerian Roche <[email protected]>
Co-authored-by: Michel Loiseleur <[email protected]>
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. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants