Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(source): code cleanup #5189

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

ivankatliarchuk
Copy link
Contributor

@ivankatliarchuk ivankatliarchuk commented Mar 17, 2025

Description

No changes to business logic. Slicing this pull request #5186.

Moved few methods to shared/utils folder, added code re-use in some services. Increased code coverage for moved functions.

Target is to support for istio support v1alpha3 and v1. draft pull request is here gofogo#10

Checklist

  • Unit tests updated
  • End user documentation updated

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Mar 17, 2025
@ivankatliarchuk
Copy link
Contributor Author

/kind cleanup
/label tide/merge-method-squash

@k8s-ci-robot k8s-ci-robot added kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. labels Mar 17, 2025
Signed-off-by: ivan katliarchuk <[email protected]>
Signed-off-by: ivan katliarchuk <[email protected]>
Signed-off-by: ivan katliarchuk <[email protected]>
Signed-off-by: ivan katliarchuk <[email protected]>
Signed-off-by: ivan katliarchuk <[email protected]>
Signed-off-by: ivan katliarchuk <[email protected]>
Copy link
Collaborator

@mloiseleur mloiseleur left a comment

Choose a reason for hiding this comment

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

Would you please rename source/annotations/annottaions.go to source/annotations/annotations.go ?

Signed-off-by: ivan katliarchuk <[email protected]>
@ivankatliarchuk
Copy link
Contributor Author

@mloiseleur Done

@mloiseleur
Copy link
Collaborator

/lgtm

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

Cool. Let me know if needs further split. Most changes - licence and test coverage.

const (
// CloudflareProxiedKey The annotation used for determining if traffic will go through Cloudflare
CloudflareProxiedKey = cloudflare.CloudflareProxiedKey
CloudflareCustomHostnameKey = cloudflare.CloudflareCustomHostnameKey
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't cloudflare use the annotations package const?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed. Cloudflare annotations defined in annotations package

"sigs.k8s.io/external-dns/endpoint"
)

func getAliasFromAnnotations(annotations map[string]string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Get suggests that you get the alias from it, I would say has... would be more appropriate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Original code

func getAliasFromAnnotations(annotations map[string]string) bool {

}
ttlValue, err := parseTTL(ttlAnnotation)
if err != nil {
log.Warnf("%s: \"%v\" is not a valid TTL value: %v", resource, ttlAnnotation, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO it's clearly an error. Warning should have only use of communication deprecations, in my opinion.
Maybe we should decide how we use warnings, because I think during the last couple of months there's a little abuse of warnings.

Copy link
Contributor Author

@ivankatliarchuk ivankatliarchuk Mar 19, 2025

Choose a reason for hiding this comment

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

Link to original code

func getTTLFromAnnotations(annotations map[string]string, resource string) endpoint.TTL {
ttlNotConfigured := endpoint.TTL(0)
ttlAnnotation, exists := annotations[ttlAnnotationKey]
if !exists {
return ttlNotConfigured
}
ttlValue, err := parseTTL(ttlAnnotation)
if err != nil {
log.Warnf("%s: \"%v\" is not a valid TTL value: %v", resource, ttlAnnotation, err)
return ttlNotConfigured
}
if ttlValue < ttlMinimum || ttlValue > ttlMaximum {
log.Warnf("TTL value %q must be between [%d, %d]", ttlValue, ttlMinimum, ttlMaximum)
return ttlNotConfigured
}
return endpoint.TTL(ttlValue)

And pull commit where change was introduced 17e9637#diff-cf68f602fa7c20e5341f3b83054df68ade1586a144b1eae5347e0ac47096d3aa

The Warning makes to an extend.

  • When parseTTL failes, there is a Warning that user should change TTL configuration
  • Code uses default values, external-dns works.
  • User could fix annotation or configuration when have time, and Warning will disapear

If it's an error;

  • the code most likely should throw an error instead of setting up a default
  • If there is a mistake in configuration, the external-dns is going to crash or behave differently

I need to capture all cases and provide an update for annotations proposal #5080

It was like that and I would rather keep it as is. In my opinion, I better capture this in annotation graduation proposal. Kind of all annotations should have validations, and validation should either be Error or Warning as example.

//
// Note: for durations like "1.5s" the fraction is omitted (resulting in 1 second
// for the example).
func parseTTL(s string) (ttlSeconds int64, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why define return variables in the signature if you don't use the feature?
Please drop them it's most off the time a code smell.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do. I was just moved things around, it was like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

current master

func parseTTL(s string) (ttlSeconds int64, err error) {

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 actually think the error is used on the client.

If an error, provide default

ttlValue, err := parseTTL(ttlAnnotation)
if err != nil {
   return ttlNotConfigured
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pls suggest what to do. Another option is parseTTLOrDefault

@@ -35,7 +35,10 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"

antns "sigs.k8s.io/external-dns/source/annotations"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why rename? It reads simpler if you don't alias the package import here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed antns. The main reason was - annotations is quite a common package name in most of the projects.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Example method attributes

func getTargetsFromTargetAnnotation(annotations map[string]string) endpoint.Targets {
. There was shadowing. So renamed to input

"k8s.io/apimachinery/pkg/labels"
kubeinformers "k8s.io/client-go/informers"
coreinformers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"

antns "sigs.k8s.io/external-dns/source/annotations"

Copy link
Contributor

Choose a reason for hiding this comment

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

Drop the whitespace here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

source/source.go Outdated
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"

"sigs.k8s.io/external-dns/endpoint"

antns "sigs.k8s.io/external-dns/source/annotations"
"sigs.k8s.io/external-dns/source/cloudflare"
Copy link
Contributor

Choose a reason for hiding this comment

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

Source should not import cloudflare.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed import

source/source.go Outdated
CloudflareProxiedKey = "external-dns.alpha.kubernetes.io/cloudflare-proxied"
CloudflareCustomHostnameKey = "external-dns.alpha.kubernetes.io/cloudflare-custom-hostname"
// CloudflareProxiedKey The annotation used for determining if traffic will go through Cloudflare
CloudflareProxiedKey = cloudflare.CloudflareProxiedKey
Copy link
Contributor

Choose a reason for hiding this comment

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

Why redefining if we can just use the annotations package?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

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

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from mloiseleur. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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

@szuecs
Copy link
Contributor

szuecs commented Mar 19, 2025

I think it's a great idea!
There are some smells I would like to see fixed, but I think it's rather small effort to do.
Another not commented thing is that in general I think a "utils" package is most often a code smell. I know Kubernetes project did it everywhere but that's not a good excuse. If it doesn't create a dependency cycle I would prefer to put these functions into source package.

Signed-off-by: ivan katliarchuk <[email protected]>
@ivankatliarchuk
Copy link
Contributor Author

Changed as per review. I think the utils is quite common pattern. Example go-sdk or helm. This most likely more like a preferred code style. Agree that better to create a specific packages, example strings, parse and other stuff. I have no strong opinion about that.

Let me know what else to change

@ivankatliarchuk ivankatliarchuk requested a review from szuecs March 19, 2025 12:06
@ivankatliarchuk
Copy link
Contributor Author

ivankatliarchuk commented Mar 20, 2025

Probably worth to share once again the motivation of why some of the functions moved to utils.

I have a follow-up pull request to address several Istio bugs and add support for v1alpha, v1beta, and v1 versions. While all code could reside in the
source folder, I've chosen to isolate the sources by source vendor and version. This approach enhances visibility into version-specific patterns and improvements, facilitates easier version support, and allows for more focused unit testing.

Screenshot 2025-03-20 at 16 30 28

is not going to be exact folder structure, just an example at the moment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants