-
Notifications
You must be signed in to change notification settings - Fork 55
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
add optional timeout to CSI lib connection utils #19
Closed
andrewsykim
wants to merge
1
commit into
kubernetes-csi:master
from
andrewsykim:add-optional-timeout
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,7 @@ const terminationLogPath = "/dev/termination-log" | |
// For other connections, the default behavior from gRPC is used and | ||
// loss of connection is not detected reliably. | ||
func Connect(address string, options ...Option) (*grpc.ClientConn, error) { | ||
return connect(address, []grpc.DialOption{}, options) | ||
return connect(address, options) | ||
} | ||
|
||
// Option is the type of all optional parameters for Connect. | ||
|
@@ -74,6 +74,14 @@ func OnConnectionLoss(reconnect func() bool) Option { | |
} | ||
} | ||
|
||
// WithTimeout sets the connection timeout duration | ||
// If not specified, all connections will retry forever. | ||
func WithTimeout(timeout time.Duration) Option { | ||
return func(o *options) { | ||
o.timeout = timeout | ||
} | ||
} | ||
|
||
// ExitOnConnectionLoss returns callback for OnConnectionLoss() that writes | ||
// an error to /dev/termination-log and exits. | ||
func ExitOnConnectionLoss() func() bool { | ||
|
@@ -89,21 +97,28 @@ func ExitOnConnectionLoss() func() bool { | |
|
||
type options struct { | ||
reconnect func() bool | ||
timeout time.Duration | ||
} | ||
|
||
// connect is the internal implementation of Connect. It has more options to enable testing. | ||
func connect(address string, dialOptions []grpc.DialOption, connectOptions []Option) (*grpc.ClientConn, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decided to remove |
||
func connect(address string, connectOptions []Option) (*grpc.ClientConn, error) { | ||
var o options | ||
for _, option := range connectOptions { | ||
option(&o) | ||
} | ||
|
||
var dialOptions []grpc.DialOption | ||
dialOptions = append(dialOptions, | ||
grpc.WithInsecure(), // Don't use TLS, it's usually local Unix domain socket in a container. | ||
grpc.WithBackoffMaxDelay(time.Second), // Retry every second after failure. | ||
grpc.WithBlock(), // Block until connection succeeds. | ||
grpc.WithUnaryInterceptor(LogGRPC), // Log all messages. | ||
) | ||
|
||
if o.timeout != 0 { | ||
dialOptions = append(dialOptions, grpc.WithTimeout(o.timeout)) | ||
} | ||
|
||
unixPrefix := "unix://" | ||
if strings.HasPrefix(address, "/") { | ||
// It looks like filesystem path. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A more flexible solution for specifying a timeout is to add a
context.Context
parameter to theConnect
function. Then one can specify both a deadline and a timeout, depending on the situation. If we expose a timeout option at all in the API, then I would prefer the solution based on context.@andrewsykim why do you think such an option is needed?
When @jsafrane added this function, he intentionally limited the API to just what the CSI sidecars need, to ensure consistent behavior. That's why the testing is partly using an internal helper function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The connect forever option doesn't work well with the liveness probe sidecar because the health check will hang instead of returning an error.
@andrewsykim can you confirm if the Kubernetes liveness probe can appropriately handle the health check hanging?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, the default (and minimum) livenessprobe timeout on kubelet is 1s, so liveness check hanging and not returning an error would still allow the kubelet to restart the container within the timeout specified on the container spec.
If we're okay assuming this, then maybe a timeout option is not needed at all. If we do want the livenessprobe sidecar to explicitly return 500 when a timeout is reached then being able to pass in a timeout via the connection lib would be useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can update to use context instead, though we may lose some of the functionality we get passing in the timeout as a grpc dial option. Either works for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I think in that case, it is fine to have the liveness probe hang on connect(). The only downside I see, is that healthz requests may pile up on the liveness probe if the driver is forever in an unready state.
How about something like this for the liveness probe:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What functionality could get lost?
Note that gRPC already deprecated
WithTimeout
in favor ofcontext.WithTimeout
- https://godoc.org/google.golang.org/grpc#WithTimeout.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. I'll update livenessprobe to do this as soon as #17 is merged
I didn't know this, this is good to know thanks!