From 0a83a476d89fcee41a2570ed7aa88a6452e2a723 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 31 Mar 2025 11:59:17 +0200 Subject: [PATCH] registry: v1Endpoint.ping: pass through context Signed-off-by: Sebastiaan van Stijn --- registry/search_endpoint_v1.go | 23 +++++++++++++++-------- registry/search_endpoint_v1_test.go | 2 +- registry/search_session.go | 2 +- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/registry/search_endpoint_v1.go b/registry/search_endpoint_v1.go index 409b612ed2..2cb2f66213 100644 --- a/registry/search_endpoint_v1.go +++ b/registry/search_endpoint_v1.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "encoding/json" + "errors" "net/http" "net/url" "strings" @@ -50,7 +51,10 @@ func newV1Endpoint(ctx context.Context, index *registry.IndexInfo, headers http. // Try HTTPS ping to registry endpoint.URL.Scheme = "https" - if _, err := endpoint.ping(); err != nil { + if _, err := endpoint.ping(ctx); err != nil { + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { + return nil, err + } if endpoint.IsSecure { // If registry is secure and HTTPS failed, show user the error and tell them about `--insecure-registry` // in case that's what they need. DO NOT accept unknown CA certificates, and DO NOT fall back to HTTP. @@ -60,7 +64,7 @@ func newV1Endpoint(ctx context.Context, index *registry.IndexInfo, headers http. // registry is insecure and HTTPS failed, fallback to HTTP. log.G(ctx).WithError(err).Debugf("error from registry %q marked as insecure - insecurely falling back to HTTP", endpoint) endpoint.URL.Scheme = "http" - if _, err2 := endpoint.ping(); err2 != nil { + if _, err2 := endpoint.ping(ctx); err2 != nil { return nil, invalidParamf("invalid registry endpoint %q. HTTPS attempt: %v. HTTP attempt: %v", endpoint, err, err2) } } @@ -109,7 +113,7 @@ func (e *v1Endpoint) String() string { } // ping returns a v1PingResult which indicates whether the registry is standalone or not. -func (e *v1Endpoint) ping() (v1PingResult, error) { +func (e *v1Endpoint) ping(ctx context.Context) (v1PingResult, error) { if e.String() == IndexServer { // Skip the check, we know this one is valid // (and we never want to fallback to http in case of error) @@ -117,14 +121,17 @@ func (e *v1Endpoint) ping() (v1PingResult, error) { } pingURL := e.String() + "_ping" - log.G(context.TODO()).WithField("url", pingURL).Debug("attempting v1 ping for registry endpoint") - req, err := http.NewRequest(http.MethodGet, pingURL, nil) + log.G(ctx).WithField("url", pingURL).Debug("attempting v1 ping for registry endpoint") + req, err := http.NewRequestWithContext(ctx, http.MethodGet, pingURL, nil) if err != nil { return v1PingResult{}, invalidParam(err) } resp, err := e.client.Do(req) if err != nil { + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { + return v1PingResult{}, err + } return v1PingResult{}, invalidParam(err) } @@ -136,7 +143,7 @@ func (e *v1Endpoint) ping() (v1PingResult, error) { if v == "1" || strings.EqualFold(v, "true") { info.Standalone = true } - log.G(context.TODO()).Debugf("v1PingResult.Standalone (from X-Docker-Registry-Standalone header): %t", info.Standalone) + log.G(ctx).Debugf("v1PingResult.Standalone (from X-Docker-Registry-Standalone header): %t", info.Standalone) return info, nil } @@ -146,11 +153,11 @@ func (e *v1Endpoint) ping() (v1PingResult, error) { Standalone: true, } if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { - log.G(context.TODO()).WithError(err).Debug("error unmarshaling _ping response") + log.G(ctx).WithError(err).Debug("error unmarshaling _ping response") // don't stop here. Just assume sane defaults } - log.G(context.TODO()).Debugf("v1PingResult.Standalone: %t", info.Standalone) + log.G(ctx).Debugf("v1PingResult.Standalone: %t", info.Standalone) return info, nil } diff --git a/registry/search_endpoint_v1_test.go b/registry/search_endpoint_v1_test.go index 85339aa93c..fb1c59222d 100644 --- a/registry/search_endpoint_v1_test.go +++ b/registry/search_endpoint_v1_test.go @@ -18,7 +18,7 @@ func TestV1EndpointPing(t *testing.T) { if err != nil { t.Fatal(err) } - regInfo, err := ep.ping() + regInfo, err := ep.ping(context.Background()) if err != nil { t.Fatal(err) } diff --git a/registry/search_session.go b/registry/search_session.go index 8a3ffba75c..87a3199612 100644 --- a/registry/search_session.go +++ b/registry/search_session.go @@ -179,7 +179,7 @@ func authorizeClient(client *http.Client, authConfig *registry.AuthConfig, endpo // If we're working with a standalone private registry over HTTPS, send Basic Auth headers // alongside all our requests. if endpoint.String() != IndexServer && endpoint.URL.Scheme == "https" { - info, err := endpoint.ping() + info, err := endpoint.ping(context.TODO()) if err != nil { return err }