-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fall back to http1 on failed http2 probe #16205
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
Open
linkvt
wants to merge
3
commits into
knative:main
Choose a base branch
from
linkvt:fallback-to-http1-on-failed-http2-probe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -77,114 +77,102 @@ func TCPProbe(config TCPProbeConfigOptions) error { | |
| return nil | ||
| } | ||
|
|
||
| // Returns a transport that uses HTTP/2 if it's known to be supported, and otherwise | ||
| // spoofs the request & response versions to HTTP/1.1. | ||
| func autoDowngradingTransport(opt HTTPProbeConfigOptions) http.RoundTripper { | ||
| t := pkgnet.NewProberTransport() | ||
| // proberTransport is a reusable transport optimized for health check probes. | ||
| // The transport auto-selects between HTTP/1.1 and H2C based on the request's ProtoMajor field. | ||
| var proberTransport = pkgnet.NewProberTransport() | ||
|
|
||
| // cleartextProbeTransport returns a RoundTripper that wraps the prober transport | ||
| // and sets the appropriate protocol hint for the given protocol version. | ||
| // protoMajor should be 1 for HTTP/1.1 or 2 for H2C. | ||
| func cleartextProbeTransport(protoMajor int) http.RoundTripper { | ||
| return pkgnet.RoundTripperFunc(func(r *http.Request) (*http.Response, error) { | ||
| // If the user-container can handle HTTP2, we pass through the request as-is. | ||
| // We have to set r.ProtoMajor to 2, since auto transport relies solely on it | ||
| // to decide whether to use h2c or http1.1. | ||
| if opt.MaxProtoMajor == 2 { | ||
| r.ProtoMajor = 2 | ||
| return t.RoundTrip(r) | ||
| } | ||
|
|
||
| // Otherwise, save the request HTTP version and downgrade it | ||
| // to HTTP1 before sending. | ||
| version := r.ProtoMajor | ||
| r.ProtoMajor = 1 | ||
| resp, err := t.RoundTrip(r) | ||
|
|
||
| // Restore the request & response HTTP version before sending back. | ||
| r.ProtoMajor = version | ||
| if resp != nil { | ||
| resp.ProtoMajor = version | ||
| } | ||
| return resp, err | ||
| // Set the protocol hint for the auto-selecting prober transport | ||
| r.ProtoMajor = protoMajor | ||
| return proberTransport.RoundTrip(r) | ||
| }) | ||
| } | ||
|
|
||
| var transport = func() *http.Transport { | ||
| t := http.DefaultTransport.(*http.Transport).Clone() | ||
| t.TLSClientConfig.InsecureSkipVerify = true | ||
| return t | ||
| }() | ||
|
Author
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. This transport was only used during HTTP2 detection, as we dont support https/h2 probes IMO not really needed. |
||
|
|
||
| func getURL(config HTTPProbeConfigOptions) (*url.URL, error) { | ||
| return url.Parse(string(config.Scheme) + "://" + net.JoinHostPort(config.Host, config.Port.String()) + "/" + strings.TrimPrefix(config.Path, "/")) | ||
| } | ||
|
|
||
| // http2UpgradeProbe checks that an HTTP with HTTP2 upgrade request | ||
| // connection can be understood by the address. | ||
| // Returns: the highest known proto version supported (0 if not ready or error) | ||
| func http2UpgradeProbe(config HTTPProbeConfigOptions) (int, error) { | ||
| // detectHTTPProtocolVersion detects the highest HTTP protocol version supported by the server. | ||
| // Attempts H2C first, falls back to HTTP/1 if that fails or returns non-ready status. | ||
| // Returns 2 (H2C ready), 1 (HTTP/1 ready), or 0 (not ready/error). | ||
| func detectHTTPProtocolVersion(config HTTPProbeConfigOptions) (int, error) { | ||
| // http.Client does not fallback to from h2c to http1, we need to make two requests ourselves | ||
| httpClient := &http.Client{ | ||
| Transport: transport, | ||
| Timeout: config.Timeout, | ||
| Transport: cleartextProbeTransport(2), | ||
| } | ||
|
|
||
| url, err := getURL(config) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("error constructing probe url %w", err) | ||
| } | ||
|
|
||
| // do a simple GET request as Kubernetes does, avoid non-standard methods like HEAD | ||
| //nolint:noctx // timeout is specified on the http.Client above | ||
| req, err := http.NewRequest(http.MethodOptions, url.String(), nil) | ||
| req, err := http.NewRequest(http.MethodGet, url.String(), nil) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("error constructing probe request %w", err) | ||
| } | ||
| req.Header.Add(netheader.UserAgentKey, netheader.KubeProbeUAPrefix+config.KubeMajor+"/"+config.KubeMinor) | ||
|
|
||
| // An upgrade will need to have at least these 3 headers. | ||
| // This is documented at https://tools.ietf.org/html/rfc7540#section-3.2 | ||
| req.Header.Add("Connection", "Upgrade, HTTP2-Settings") | ||
| req.Header.Add("Upgrade", "h2c") | ||
| req.Header.Add("HTTP2-Settings", "") | ||
| if res, err := httpClient.Do(req); err == nil { | ||
| defer res.Body.Close() | ||
|
|
||
| req.Header.Add(netheader.UserAgentKey, netheader.KubeProbeUAPrefix+config.KubeMajor+"/"+config.KubeMinor) | ||
| // ignore non-ready http2 responses and continue with http1, http2 might not be properly supported | ||
| if isHTTPProbeReady(res) { | ||
| return 2, nil | ||
| } | ||
| } | ||
|
|
||
| // fallback to check http1 | ||
| httpClient.Transport = cleartextProbeTransport(1) | ||
| res, err := httpClient.Do(req) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| defer res.Body.Close() | ||
|
|
||
| maxProto := 0 | ||
| defer res.Body.Close() | ||
|
|
||
| if isHTTPProbeUpgradingToH2C(res) { | ||
| maxProto = 2 | ||
| } else if isHTTPProbeReady(res) { | ||
| maxProto = 1 | ||
| } else { | ||
| return maxProto, fmt.Errorf("HTTP probe did not respond Ready, got status code: %d", res.StatusCode) | ||
| if isHTTPProbeReady(res) { | ||
| return 1, nil | ||
| } | ||
|
|
||
| return maxProto, nil | ||
| return 0, fmt.Errorf("HTTP probe did not respond Ready, got status code: %d", res.StatusCode) | ||
| } | ||
|
|
||
| // HTTPProbe checks that HTTP connection can be established to the address. | ||
| func HTTPProbe(config HTTPProbeConfigOptions) error { | ||
| if config.MaxProtoMajor == 0 { | ||
| // If we don't know if the connection supports HTTP2, we will try it. | ||
| // Once we get a non-error response, we won't try again. | ||
| // NOTE: the result is not cached right now, every probe attempts http2 detection again | ||
|
|
||
| // If maxProto is 0, container is not ready, so we don't know whether http2 is supported. | ||
| // If maxProto is 1, we know we're ready, but we also can't upgrade, so just return. | ||
| // If maxProto is 2, we know we can upgrade to http2 | ||
| maxProto, err := http2UpgradeProbe(config) | ||
| maxProto, err := detectHTTPProtocolVersion(config) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to run HTTP2 upgrade probe with error: %w", err) | ||
| return fmt.Errorf("failed to run HTTP protocol probe with error: %w", err) | ||
| } | ||
| config.MaxProtoMajor = maxProto | ||
| if config.MaxProtoMajor == 1 { | ||
| // probe already passed for HTTP/1.1 during auto detection, return early | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| httpClient := &http.Client{ | ||
| Transport: autoDowngradingTransport(config), | ||
| Transport: cleartextProbeTransport(config.MaxProtoMajor), | ||
| Timeout: config.Timeout, | ||
| } | ||
| url, err := getURL(config) | ||
| if err != nil { | ||
| return fmt.Errorf("error constructing probe url %w", err) | ||
| } | ||
|
|
||
| //nolint:noctx // timeout is specified on the http.Client above | ||
| req, err := http.NewRequest(http.MethodGet, url.String(), nil) | ||
| if err != nil { | ||
|
|
@@ -216,11 +204,6 @@ func HTTPProbe(config HTTPProbeConfigOptions) error { | |
| return nil | ||
| } | ||
|
|
||
| // isHTTPProbeUpgradingToH2C checks whether the server indicates it's switching to h2c protocol. | ||
| func isHTTPProbeUpgradingToH2C(res *http.Response) bool { | ||
| return res.StatusCode == http.StatusSwitchingProtocols && res.Header.Get("Upgrade") == "h2c" | ||
| } | ||
|
|
||
| // isHTTPProbeReady checks whether we received a successful Response | ||
| func isHTTPProbeReady(res *http.Response) bool { | ||
| // response status code between 200-399 indicates success | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
I could not come up with a reason for why this autoDowngradingTransport would be needed at all, maybe it was required some time ago but doesn't seem to be the case today.