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

Replace grpc.DialContext and grpc.WithBlock with grpc.NewClient #6026

Merged
merged 1 commit into from
Jan 15, 2025

Conversation

seanlaii
Copy link
Contributor

@seanlaii seanlaii commented Jan 8, 2025

What type of PR is this?
/kind deprecation

What this PR does / why we need it:

  • grpc.DialContext and grpc.WithBlock are deprecated
  • Updates client connection creation to use modern gRPC patterns
  • Leverages DNS resolver for better service discovery
  • Maintains backward compatibility while enabling future features

Which issue(s) this PR fixes:
Part of #5796

Special notes for your reviewer:
Replace grpc.DialContext and grpc.WithBlock with grpc.NewClient to follow the recommended pattern in https://github.com/grpc/grpc-go/blob/master/Documentation/anti-patterns.md.
Since we only allow users to configure the authentication setting of the grpc client without any custom dial options, I think we should be safe to perform the replacement.

Does this PR introduce a user-facing change?:

Replace `grpc.DialContext` and `grpc.WithBlock` with `grpc.NewClient` since DialContext and WithBlock are deprecated while maintaining the original functionality.

@karmada-bot karmada-bot added the kind/deprecation Categorizes issue or PR as related to a feature/enhancement marked for deprecation. label Jan 8, 2025
@karmada-bot karmada-bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jan 8, 2025
@seanlaii
Copy link
Contributor Author

seanlaii commented Jan 8, 2025

@zhzhuang-zju please help take a look. Thank you so much!

@zhzhuang-zju
Copy link
Contributor

/assign

@codecov-commenter
Copy link

codecov-commenter commented Jan 8, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 36.84211% with 12 lines in your changes missing coverage. Please review.

Project coverage is 48.36%. Comparing base (56194cb) to head (36a6a8f).
Report is 10 commits behind head on master.

Files with missing lines Patch % Lines
pkg/util/grpcconnection/config.go 36.84% 9 Missing and 3 partials ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6026      +/-   ##
==========================================
+ Coverage   48.35%   48.36%   +0.01%     
==========================================
  Files         665      666       +1     
  Lines       54831    54838       +7     
==========================================
+ Hits        26512    26521       +9     
+ Misses      26599    26597       -2     
  Partials     1720     1720              
Flag Coverage Δ
unittests 48.36% <36.84%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zhzhuang-zju
Copy link
Contributor

/retest

cc, err = createGRPCConnection(path, timeout, opts...)
if err == nil {
return cc, nil
cc, err = grpc.NewClient(path, opts...)
Copy link
Contributor

Choose a reason for hiding this comment

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

@seanlaii I found that grpc.NewClient also returns a client when path doesn't exist, with no errors reported. That's not what we expected.

Can we proceed to refine according to the method of validating grpc configuration mentioned in the document?

Some users of Dial use it as a way to validate the configuration of their system. If you wish to maintain this behavior but migrate to NewClient, you can call GetState, then Connect if the state is Idle and WaitForStateChange until the channel is connected. However, if this fails, it does not mean that your configuration was bad - it could also mean the service is not reachable by the client due to connectivity reasons.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! Thanks for the advice!

@seanlaii seanlaii force-pushed the grpc branch 3 times, most recently from 9ced984 to e611c35 Compare January 10, 2025 13:47
// grpc.DialContext is deprecated. TODO: Perhaps need to reconsider the approach in a future PR
//nolint:staticcheck
cc, err := grpc.DialContext(ctx, path, opts...)
cc, err := grpc.NewClient(path, opts...)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
cc, err := grpc.NewClient(path, opts...)
conn, err = grpc.NewClient(path, opts...)

We can use the variable defined in return types, which makes the defer function clear about which error it referring to.

Copy link
Contributor Author

@seanlaii seanlaii Jan 13, 2025

Choose a reason for hiding this comment

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

I tried it and encountered an issue: if there is an error, conn will be set to nil, resulting in nil dereference when trying to use conn.Close in defer function. Therefore, I changed it back, which also follows the implementation in here.
Please take a look. Thank you!

Copy link
Member

Choose a reason for hiding this comment

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

I tried it and encountered an issue: if there is an error, conn will be set to nil, resulting in nil dereference when trying to use conn.Close in defer function.

Nice finding! Seems you tested the abnormal case, thanks.

Copy link
Member

@RainbowMango RainbowMango left a comment

Choose a reason for hiding this comment

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

/lgtm

@chaosi-zju @zhzhuang-zju
Do you have any further comments?

// grpc.DialContext is deprecated. TODO: Perhaps need to reconsider the approach in a future PR
//nolint:staticcheck
cc, err := grpc.DialContext(ctx, path, opts...)
cc, err := grpc.NewClient(path, opts...)
Copy link
Member

Choose a reason for hiding this comment

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

I tried it and encountered an issue: if there is an error, conn will be set to nil, resulting in nil dereference when trying to use conn.Close in defer function.

Nice finding! Seems you tested the abnormal case, thanks.

@karmada-bot karmada-bot added the lgtm Indicates that a PR is ready to be merged. label Jan 14, 2025
@RainbowMango
Copy link
Member

/remove-kind deprecation
/kind cleanup

@karmada-bot karmada-bot added kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. and removed kind/deprecation Categorizes issue or PR as related to a feature/enhancement marked for deprecation. labels Jan 14, 2025
@RainbowMango RainbowMango added this to the v1.13 milestone Jan 14, 2025
@chaosi-zju
Copy link
Member

Do you have any further comments?

/lgtm, thanks @seanlaii

//nolint:staticcheck
grpc.WithBlock(),
}
var opts []grpc.DialOption
Copy link
Contributor

Choose a reason for hiding this comment

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

@seanlaii Thanks for your work! Just a suggestion, function name DialWithTimeOut can be considered to be changed to NewClient

Copy link
Member

Choose a reason for hiding this comment

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

I'm ok with the DialWithTimeOut which clearly explains actions in this function.

Copy link
Contributor

Choose a reason for hiding this comment

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

Get it, please ignore this comment

Copy link
Member

@RainbowMango RainbowMango left a comment

Choose a reason for hiding this comment

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

/approve

@karmada-bot karmada-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 15, 2025
@RainbowMango
Copy link
Member

/lgtm
/approve

PS: re-trigger prow bot

@karmada-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: RainbowMango

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

@karmada-bot karmada-bot merged commit 272447e into karmada-io:master Jan 15, 2025
21 checks passed
@seanlaii seanlaii deleted the grpc branch January 15, 2025 03:25
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. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm Indicates that a PR is ready to be merged. 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