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 30 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.

Signed-off-by: ivan katliarchuk <[email protected]>
…' into feat-code-cleanup-0

* refs/remotes/origin/feat-code-cleanup-0:
  fix(cloudflare): custom hostnames edge-cases causing duplicates (kubernetes-sigs#5183)
  feat(banner): standardize user agent and output (kubernetes-sigs#5154)
  feat: IDNA awareness in the zone finder (kubernetes-sigs#5147)
  chore(deps): bump the dev-dependencies group across 1 directory with 20 updates
  chore(deps): bump renovatebot/github-action
  feat(chart): Update image to v0.16.1
  fix: correct route53 iam
  chore(deps): bump renovatebot/github-action
  feat(ovh): major rewriting of the provider (kubernetes-sigs#5143)
  Add Yandex Cloud Webhook
  chore(openstack designate)!: remove in-tree provider
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]>
@ivankatliarchuk ivankatliarchuk requested a review from szuecs March 28, 2025 17:37

func hasAliasFromAnnotations(annotations map[string]string) bool {
aliasAnnotation, exists := annotations[AliasKey]
return exists && aliasAnnotation == "true"
Copy link
Contributor

Choose a reason for hiding this comment

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

In Go we use "ok" as var name in these cases, please change all of them.
exists -> ok

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

)

func TestParseAnnotationFilter(t *testing.T) {
tests := []struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just FYI, so does not need to be changed: you can also omit the "tests" var and start with the for loop and have an inline struct definition. I think it looks like this (out of my head without parser or compiler):

for _, tt := range []struct{
   name string
   ...
  }{
    name: "myName",
    ...
  }, {
     name: "anotherName",
     ....
  }, {
     name: "lastName",
     ....
  }} {
    t.Run(tt.name, func(t *testing.T) {
      // body
    })
}

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'm not going to touch it, not because against, mainly due to consistency. There is not a single test with this design at the moment
Screenshot 2025-03-28 at 21 48 40

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

"sigs.k8s.io/external-dns/source/utils"

Copy link
Contributor

Choose a reason for hiding this comment

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

I would drop the whitespaces here and let gofmt/goimports group the imports

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

@@ -36,6 +36,8 @@ import (
"k8s.io/client-go/kubernetes/scheme"
"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 whitespace

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


services, err := svcInformer.Lister().Services(namespace).List(labels.Everything())
if err != nil {
log.Errorf("not able to list labels for services in namespace %s. %v", namespace, err)
Copy link
Contributor

@szuecs szuecs Mar 28, 2025

Choose a reason for hiding this comment

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

We could omit the log and log only in the caller or change the error log to a proper style:
"Failed to .." is a good prefix for error logs.
Example:

log.Errorf("Failed to list labels for services in namespace %q: %v", namespace, err)

or return error:

return fmt.Errorf("failed to list labels for services in namespace %q: %w", namespace, err)

I would prefer return the error and drop the log, because of imports.

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

Signed-off-by: ivan katliarchuk <[email protected]>
@ivankatliarchuk ivankatliarchuk requested a review from szuecs March 28, 2025 21:50
* master:
  docs(proposal): externaldns api graduation to beta (kubernetes-sigs#5079)
  chore(code-cleanup): move logic away from main.go add tests (kubernetes-sigs#5222)
  chore(deps): bump the dev-dependencies group across 1 directory with 17 updates
  chore: add se for nlb, alb in thailand region
  fix(node): logger test fixed (kubernetes-sigs#5232)
  fix(chart): add missing types for empty values (kubernetes-sigs#5207)
  docs: Fix typo: grcp → grpc.
  removing reduntant code
  renaming variable
  added new tests to handle edge case
  detailed documentation with no-expose
  added warn log
  edited docs and made new test
  docs: added documentation in node source
  fix: fixing ci lint
  fix: removing fmt.Printf
  feat: added expose internal ipv6 flag
Signed-off-by: ivan katliarchuk <[email protected]>
@ivankatliarchuk
Copy link
Contributor Author

rebased with master. just to make sure latest changes do not break anything

@Raffo
Copy link
Contributor

Raffo commented Apr 1, 2025

I'd also love to see the name "utils" go away, a relatively known blogpost on the topic: https://dave.cheney.net/2019/01/08/avoid-package-names-like-base-util-or-common

@ivankatliarchuk
Copy link
Contributor Author

@Raffo do you mean to move everything back from utils to source.go?

@szuecs
Copy link
Contributor

szuecs commented Apr 1, 2025

@Raffo do you mean to move everything back from utils to source.go?

If the only user is in source package you can put it in source/util.go

@ivankatliarchuk
Copy link
Contributor Author

Ok. I give it a try

@Raffo
Copy link
Contributor

Raffo commented Apr 1, 2025

Yeah what I really don't like is having a generic package that is called utils. It basically tells nothing about what it is supposed to do and it often becomes a bag with everything inside. I know kubernetes has adopted patterns like util and hack and so on, but those patterns are IMO pretty bad and I don't think we have to copy them only because the kube core contributors 10 years ago went with similar decisions.

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

Hi @szuecs and @Raffo utils removed

Signed-off-by: ivan katliarchuk <[email protected]>
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.

5 participants