From ef362fb136a122ad3fd12591cb013ee274ac0861 Mon Sep 17 00:00:00 2001 From: Gaius Date: Thu, 12 Nov 2026 06:02:12 +0000 Subject: [PATCH 1/2] chore: bump dragonfly deps to v2.5.4-rc.1 and client image to v1.5.13 Update the Go module dependency on d7y.io/dragonfly/v2 to v2.5.4-rc.1 in both the client-request library and the e2e tests, and bump the Rust dragonfly-client-request crate to 1.14.1 (including the README example). The e2e chart config now uses scheduler v2.5.4-rc.1 and seed client v1.5.13, and drops the explicit `backend.enableCacheTemporaryRedirect` override, relying on the client's default instead. Signed-off-by: Gaius --- client-request/go/go.mod | 2 +- client-request/go/go.sum | 4 ++-- client-request/rust/Cargo.lock | 2 +- client-request/rust/Cargo.toml | 2 +- client-request/rust/README.md | 2 +- test/e2e/driver/rust/Cargo.lock | 2 +- test/e2e/go.mod | 2 +- test/e2e/go.sum | 4 ++-- test/testdata/charts/config.yaml | 6 ++---- 9 files changed, 12 insertions(+), 14 deletions(-) diff --git a/client-request/go/go.mod b/client-request/go/go.mod index ba54403..1973cc1 100644 --- a/client-request/go/go.mod +++ b/client-request/go/go.mod @@ -4,7 +4,7 @@ go 1.25.0 require ( d7y.io/api/v2 v2.3.10 - d7y.io/dragonfly/v2 v2.5.4-rc.0 + d7y.io/dragonfly/v2 v2.5.4-rc.1 d7y.io/hashring v0.3.0 github.com/cenkalti/backoff/v5 v5.0.3 github.com/containerd/platforms v0.2.1 diff --git a/client-request/go/go.sum b/client-request/go/go.sum index 991fe29..8b06634 100644 --- a/client-request/go/go.sum +++ b/client-request/go/go.sum @@ -1,7 +1,7 @@ d7y.io/api/v2 v2.3.10 h1:JFC6944ETIURMfLlaH0inH5NnZtzyAGU/mjHFJUkMOk= d7y.io/api/v2 v2.3.10/go.mod h1:hQOqfvk0SSuSUZ25VrF/L4dlP4JnREaUiUui07muLws= -d7y.io/dragonfly/v2 v2.5.4-rc.0 h1:DPU98OoEkC+TPCDExNz7UW8Mts/VeIykifXWHAZ8kxU= -d7y.io/dragonfly/v2 v2.5.4-rc.0/go.mod h1:DEbHajC5YnA9IMY3umQj59KauToyB58b5s7ZRU8DqGU= +d7y.io/dragonfly/v2 v2.5.4-rc.1 h1:I/me+EbhF0NgBZR6A/lwCOkZJ03BNFSfPeS6seGWliI= +d7y.io/dragonfly/v2 v2.5.4-rc.1/go.mod h1:DEbHajC5YnA9IMY3umQj59KauToyB58b5s7ZRU8DqGU= d7y.io/hashring v0.3.0 h1:qGIkSNa/g6hSFYV2mBE+mZU16NzzMiCKobti072ACL0= d7y.io/hashring v0.3.0/go.mod h1:NY2/+76/hCcBzcTEJiqJG2AX0gzlSq+YJJgSJyZWd1c= filippo.io/edwards25519 v1.2.0 h1:crnVqOiS4jqYleHd9vaKZ+HKtHfllngJIiOpNpoJsjo= diff --git a/client-request/rust/Cargo.lock b/client-request/rust/Cargo.lock index d5ee674..dd2f453 100644 --- a/client-request/rust/Cargo.lock +++ b/client-request/rust/Cargo.lock @@ -591,7 +591,7 @@ dependencies = [ [[package]] name = "dragonfly-client-request" -version = "1.14.0" +version = "1.14.1" dependencies = [ "async-trait", "backon", diff --git a/client-request/rust/Cargo.toml b/client-request/rust/Cargo.toml index 5430796..5234dd0 100644 --- a/client-request/rust/Cargo.toml +++ b/client-request/rust/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "dragonfly-client-request" description = "Request library for the dragonfly client" -version = "1.14.0" +version = "1.14.1" authors = ["The Dragonfly Developers"] edition = "2021" readme = "README.md" diff --git a/client-request/rust/README.md b/client-request/rust/README.md index ddf270b..6d5a0af 100644 --- a/client-request/rust/README.md +++ b/client-request/rust/README.md @@ -170,7 +170,7 @@ all its platforms unless a platform is set: ```toml [dependencies] -dragonfly-client-request = { version = "1.14.0", features = ["preheat"] } +dragonfly-client-request = { version = "1.14.1", features = ["preheat"] } ``` ```rust diff --git a/test/e2e/driver/rust/Cargo.lock b/test/e2e/driver/rust/Cargo.lock index bbd9b1c..c8553cf 100644 --- a/test/e2e/driver/rust/Cargo.lock +++ b/test/e2e/driver/rust/Cargo.lock @@ -562,7 +562,7 @@ dependencies = [ [[package]] name = "dragonfly-client-request" -version = "1.14.0" +version = "1.14.1" dependencies = [ "async-trait", "backon", diff --git a/test/e2e/go.mod b/test/e2e/go.mod index 415c845..a5fb13e 100644 --- a/test/e2e/go.mod +++ b/test/e2e/go.mod @@ -4,7 +4,7 @@ go 1.25.0 require ( d7y.io/dragonfly-sdk/client-request/go v0.0.0 - d7y.io/dragonfly/v2 v2.5.4-rc.0 + d7y.io/dragonfly/v2 v2.5.4-rc.1 github.com/onsi/ginkgo/v2 v2.34.0 github.com/onsi/gomega v1.43.1 ) diff --git a/test/e2e/go.sum b/test/e2e/go.sum index ecb4180..d199228 100644 --- a/test/e2e/go.sum +++ b/test/e2e/go.sum @@ -1,7 +1,7 @@ d7y.io/api/v2 v2.3.10 h1:JFC6944ETIURMfLlaH0inH5NnZtzyAGU/mjHFJUkMOk= d7y.io/api/v2 v2.3.10/go.mod h1:hQOqfvk0SSuSUZ25VrF/L4dlP4JnREaUiUui07muLws= -d7y.io/dragonfly/v2 v2.5.4-rc.0 h1:DPU98OoEkC+TPCDExNz7UW8Mts/VeIykifXWHAZ8kxU= -d7y.io/dragonfly/v2 v2.5.4-rc.0/go.mod h1:DEbHajC5YnA9IMY3umQj59KauToyB58b5s7ZRU8DqGU= +d7y.io/dragonfly/v2 v2.5.4-rc.1 h1:I/me+EbhF0NgBZR6A/lwCOkZJ03BNFSfPeS6seGWliI= +d7y.io/dragonfly/v2 v2.5.4-rc.1/go.mod h1:DEbHajC5YnA9IMY3umQj59KauToyB58b5s7ZRU8DqGU= d7y.io/hashring v0.3.0 h1:qGIkSNa/g6hSFYV2mBE+mZU16NzzMiCKobti072ACL0= d7y.io/hashring v0.3.0/go.mod h1:NY2/+76/hCcBzcTEJiqJG2AX0gzlSq+YJJgSJyZWd1c= filippo.io/edwards25519 v1.2.0 h1:crnVqOiS4jqYleHd9vaKZ+HKtHfllngJIiOpNpoJsjo= diff --git a/test/testdata/charts/config.yaml b/test/testdata/charts/config.yaml index 0f9436c..294079e 100644 --- a/test/testdata/charts/config.yaml +++ b/test/testdata/charts/config.yaml @@ -1,7 +1,7 @@ scheduler: image: repository: dragonflyoss/scheduler - tag: v2.5.4-rc.0 + tag: v2.5.4-rc.1 replicas: 1 resources: requests: @@ -21,7 +21,7 @@ seedClient: replicas: 4 image: repository: dragonflyoss/client - tag: v1.5.12 + tag: v1.5.13 resources: requests: cpu: '0' @@ -41,8 +41,6 @@ seedClient: proxy: server: requestRateLimit: 4000 - backend: - enableCacheTemporaryRedirect: false client: enable: false From bd6b9475cab53cddcabfacc30d672b4ee46122ec Mon Sep 17 00:00:00 2001 From: Gaius Date: Thu, 12 Nov 2026 09:09:45 +0000 Subject: [PATCH 2/2] fix(client-request): authorize preheat targets per task and surface backend errors Resolve the registry token when each preheat target starts, via a shared auth client in Go and `client.auth` per task in Rust, so a long preheat no longer fails when the bearer token expires. Registries without a bearer challenge now work: basic credentials are sent as a Basic header, and anonymous registries get no Authorization header instead of an error. The Go `downloadTask` now reads the dfdaemon backend error from the `grpc-status-details-bin` trailer and returns a `BackendError`, so a definitive answer like 404 is not retried, matching the Rust SDK. `PreheatImage` and `StatImage` return the context cause when resolving the image fails because the context ended, and `DeleteImage` on a ended context returns the peers that already deleted the image alongside the error instead of a nil response. Bump dragonfly to v2.5.4-rc.2 in the Go modules and e2e chart config. Signed-off-by: Gaius --- client-request/go/delete.go | 8 +- client-request/go/delete_test.go | 4 +- client-request/go/go.mod | 2 +- client-request/go/go.sum | 4 +- client-request/go/preheat.go | 54 +++++++++-- client-request/go/preheat_test.go | 150 ++++++++++++++++++++++++++++-- client-request/go/request_test.go | 17 ++-- client-request/go/stat.go | 4 + client-request/rust/Cargo.lock | 1 + client-request/rust/Cargo.toml | 3 +- client-request/rust/src/lib.rs | 57 ++++++++---- test/e2e/driver/rust/Cargo.lock | 1 + test/e2e/go.mod | 2 +- test/e2e/go.sum | 4 +- test/testdata/charts/config.yaml | 2 +- 15 files changed, 256 insertions(+), 57 deletions(-) diff --git a/client-request/go/delete.go b/client-request/go/delete.go index 77db066..d84eb94 100644 --- a/client-request/go/delete.go +++ b/client-request/go/delete.go @@ -80,7 +80,8 @@ func (p *Proxy) Delete(ctx context.Context, req *DeleteRequest) error { // DeleteImage deletes a preheated OCI image from every healthy seed peer by the // dfdaemon delete image API, for all its platforms or the specified one. A task // still referenced by another image or platform is retained. A seed peer that -// fails is left out of the response, an ended context fails the request. +// fails is left out of the response, an ended context fails the request with +// the seed peers that already deleted the image. func (p *Proxy) DeleteImage(ctx context.Context, req *DeleteImageRequest) (*DeleteImageResponse, error) { if err := req.validate(); err != nil { return nil, err @@ -135,11 +136,8 @@ func (p *Proxy) DeleteImage(ctx context.Context, req *DeleteImageRequest) (*Dele // A seed peer that fails is left out of the response. _ = g.Wait() - if err := context.Cause(ctx); err != nil { - return nil, err - } - return &DeleteImageResponse{Peers: slices.DeleteFunc(peers, func(peer *PeerDeletedImage) bool { return peer == nil })}, nil + return &DeleteImageResponse{Peers: slices.DeleteFunc(peers, func(peer *PeerDeletedImage) bool { return peer == nil })}, context.Cause(ctx) } // deleteTask has the seed peer delete the task, retrying a transient failure on diff --git a/client-request/go/delete_test.go b/client-request/go/delete_test.go index 58c7335..63e43fd 100644 --- a/client-request/go/delete_test.go +++ b/client-request/go/delete_test.go @@ -330,7 +330,7 @@ func TestDeleteImageFailsWhenTheContextEnds(t *testing.T) { expect: func(t *testing.T, resp *DeleteImageResponse, err error) { assert := assert.New(t) assert.ErrorIs(err, context.Canceled) - assert.Nil(resp) + assert.Empty(resp.Peers) }, }, { @@ -339,7 +339,7 @@ func TestDeleteImageFailsWhenTheContextEnds(t *testing.T) { expect: func(t *testing.T, resp *DeleteImageResponse, err error) { assert := assert.New(t) assert.ErrorIs(err, context.DeadlineExceeded) - assert.Nil(resp) + assert.Empty(resp.Peers) }, }, } diff --git a/client-request/go/go.mod b/client-request/go/go.mod index 1973cc1..58ea99d 100644 --- a/client-request/go/go.mod +++ b/client-request/go/go.mod @@ -4,7 +4,7 @@ go 1.25.0 require ( d7y.io/api/v2 v2.3.10 - d7y.io/dragonfly/v2 v2.5.4-rc.1 + d7y.io/dragonfly/v2 v2.5.4-rc.2 d7y.io/hashring v0.3.0 github.com/cenkalti/backoff/v5 v5.0.3 github.com/containerd/platforms v0.2.1 diff --git a/client-request/go/go.sum b/client-request/go/go.sum index 8b06634..18430e5 100644 --- a/client-request/go/go.sum +++ b/client-request/go/go.sum @@ -1,7 +1,7 @@ d7y.io/api/v2 v2.3.10 h1:JFC6944ETIURMfLlaH0inH5NnZtzyAGU/mjHFJUkMOk= d7y.io/api/v2 v2.3.10/go.mod h1:hQOqfvk0SSuSUZ25VrF/L4dlP4JnREaUiUui07muLws= -d7y.io/dragonfly/v2 v2.5.4-rc.1 h1:I/me+EbhF0NgBZR6A/lwCOkZJ03BNFSfPeS6seGWliI= -d7y.io/dragonfly/v2 v2.5.4-rc.1/go.mod h1:DEbHajC5YnA9IMY3umQj59KauToyB58b5s7ZRU8DqGU= +d7y.io/dragonfly/v2 v2.5.4-rc.2 h1:HMTVQMNeIVITWwFvBDp7JWpzyakezBhT4/Y7YPyHWms= +d7y.io/dragonfly/v2 v2.5.4-rc.2/go.mod h1:DEbHajC5YnA9IMY3umQj59KauToyB58b5s7ZRU8DqGU= d7y.io/hashring v0.3.0 h1:qGIkSNa/g6hSFYV2mBE+mZU16NzzMiCKobti072ACL0= d7y.io/hashring v0.3.0/go.mod h1:NY2/+76/hCcBzcTEJiqJG2AX0gzlSq+YJJgSJyZWd1c= filippo.io/edwards25519 v1.2.0 h1:crnVqOiS4jqYleHd9vaKZ+HKtHfllngJIiOpNpoJsjo= diff --git a/client-request/go/preheat.go b/client-request/go/preheat.go index 7cbb5ac..a56d4aa 100644 --- a/client-request/go/preheat.go +++ b/client-request/go/preheat.go @@ -18,6 +18,7 @@ package request import ( "context" + "encoding/json" "errors" "fmt" "io" @@ -31,7 +32,9 @@ import ( commonv2 "d7y.io/api/v2/pkg/apis/common/v2" dfdaemonv2 "d7y.io/api/v2/pkg/apis/dfdaemon/v2" + errordetailsv2 "d7y.io/api/v2/pkg/apis/errordetails/v2" "d7y.io/dragonfly/v2/pkg/idgen" + nethttp "d7y.io/dragonfly/v2/pkg/net/http" "d7y.io/dragonfly/v2/pkg/net/ip" "d7y.io/dragonfly/v2/pkg/oci" "github.com/docker/distribution" @@ -41,6 +44,10 @@ import ( "google.golang.org/protobuf/types/known/durationpb" ) +// grpcStatusDetailsBinHeader is the trailer of the details of the gRPC status, +// the same header as Status::GRPC_STATUS_DETAILS of tonic. +const grpcStatusDetailsBinHeader = "grpc-status-details-bin" + // Preheat preheats a file by downloading it to the replicas of seed peers, or // to all seed peers with ScopeAllSeedPeers, via the Dragonfly. It triggers // every selected seed peer to download the file by the dfdaemon download task @@ -137,23 +144,27 @@ func (p *Proxy) PreheatImage(ctx context.Context, req *PreheatImageRequest) erro opts = append(opts, oci.WithPlatform(*req.platform)) } - manifests, token, err := oci.Resolve(ctx, ref, opts...) + manifests, _, err := oci.Resolve(ctx, ref, opts...) if err != nil { + if cause := context.Cause(ctx); cause != nil { + return cause + } + return fmt.Errorf("%w: %v", ErrInternal, err) } - if token == "" { - return fmt.Errorf("%w: registry did not return authentication token", ErrInternal) + // Authorize the targets with one client, which keeps the token of the + // registry and renews it when it expires during a long preheat. + authClient, err := oci.NewAuthClient(ref, p.registryClient, req.username, req.password) + if err != nil { + return fmt.Errorf("%w: failed to authenticate with registry: %v", ErrInternal, err) } - // Build authorization header for blob downloads through the Dragonfly. - header := make(http.Header) - header.Set("Authorization", token) - // Build the accept header with the manifest media types for downloading // manifests, so the registry returns the exact representation matching the // digest. - manifestHeader := header.Clone() + header := make(http.Header) + manifestHeader := make(http.Header) manifestHeader.Set("Accept", strings.Join(distribution.ManifestMediaTypes(), ",")) // Collect the manifests and blobs of the matched platforms. A blob shared @@ -215,13 +226,24 @@ func (p *Proxy) PreheatImage(ctx context.Context, req *PreheatImageRequest) erro return fmt.Errorf("%w: insufficient seed peers for %d replicas, %d available", ErrInternal, req.replicas, len(seedPeers)) } + // Authorize the target when it starts, the registry may need no authorization. + authorization, err := authClient.Authorization() + if err != nil { + return fmt.Errorf("%w: failed to authenticate with registry: %v", ErrInternal, err) + } + + requestHeader := target.header.Clone() + if authorization != "" { + requestHeader.Set("Authorization", authorization) + } + // Construct the download task request with the image. download := &commonv2.Download{ Url: target.url, Type: commonv2.TaskType_STANDARD, Priority: priority, FilteredQueryParams: req.filteredQueryParams, - RequestHeader: headerToMap(target.header), + RequestHeader: headerToMap(requestHeader), PieceLength: req.pieceLength, Timeout: durationpb.New(req.timeout), RemoteIp: &remoteIP, @@ -290,7 +312,19 @@ func (p *Proxy) downloadTask(ctx context.Context, peer *commonv2.Host, id string return nil } - return fmt.Errorf("%w: failed to download task %s: %w", ErrInternal, id, err) + // The dfdaemon reports a failure of the source as the JSON of the + // backend error in the details of the status. The Rust SDK reads + // them by Status::details of tonic, but grpc-go only decodes the + // details of google.rpc.Status and leaves the others in the + // trailer. Read the trailer to keep the same behavior as the Rust + // SDK, so a definitive answer such as 404 is not retried. + var backend errordetailsv2.Backend + details := stream.Trailer().Get(grpcStatusDetailsBinHeader) + if len(details) != 1 || json.Unmarshal([]byte(details[0]), &backend) != nil || backend.StatusCode == nil { + return fmt.Errorf("%w: failed to download task %s: %w", ErrInternal, id, err) + } + + return &BackendError{Message: backend.GetMessage(), Header: nethttp.MapToHeader(backend.GetHeader()), StatusCode: int(backend.GetStatusCode())} } } }) diff --git a/client-request/go/preheat_test.go b/client-request/go/preheat_test.go index cc78b31..ef4edc5 100644 --- a/client-request/go/preheat_test.go +++ b/client-request/go/preheat_test.go @@ -34,6 +34,7 @@ import ( "github.com/cenkalti/backoff/v5" "github.com/stretchr/testify/assert" "google.golang.org/grpc/codes" + "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" ) @@ -166,9 +167,49 @@ func TestPreheatRetriesOnlyTransientDownloadFailures(t *testing.T) { tests := []struct { name string code codes.Code + trailer metadata.MD maxRetries uint8 expect func(t *testing.T, hits int32, err error) }{ + { + name: "backend not found", + code: codes.Internal, + trailer: metadata.Pairs("grpc-status-details-bin", `{"message":"not found","header":{"X-Served-By":"origin"},"status_code":404}`), + maxRetries: 3, + expect: func(t *testing.T, hits int32, err error) { + assert := assert.New(t) + var backendErr *BackendError + assert.ErrorAs(err, &backendErr) + assert.Equal(http.StatusNotFound, backendErr.StatusCode) + assert.Equal("not found", backendErr.Message) + assert.Equal("origin", backendErr.Header.Get("X-Served-By")) + assert.Equal(int32(1), hits) + }, + }, + { + name: "backend service unavailable", + code: codes.Internal, + trailer: metadata.Pairs("grpc-status-details-bin", `{"message":"unavailable","header":{},"status_code":503}`), + maxRetries: 1, + expect: func(t *testing.T, hits int32, err error) { + assert := assert.New(t) + var backendErr *BackendError + assert.ErrorAs(err, &backendErr) + assert.Equal(http.StatusServiceUnavailable, backendErr.StatusCode) + assert.Equal(int32(2), hits) + }, + }, + { + name: "backend without status code", + code: codes.Internal, + trailer: metadata.Pairs("grpc-status-details-bin", `{"message":"connection refused","header":{}}`), + maxRetries: 1, + expect: func(t *testing.T, hits int32, err error) { + assert := assert.New(t) + assert.ErrorIs(err, ErrInternal) + assert.Equal(int32(2), hits) + }, + }, { name: "internal", code: codes.Internal, @@ -245,8 +286,9 @@ func TestPreheatRetriesOnlyTransientDownloadFailures(t *testing.T) { t.Run(tc.name, func(t *testing.T) { var hits atomic.Int32 port := setupMockSeedPeerServer(t, &mockSeedPeer{ - downloadTaskErr: status.Error(tc.code, "download failed"), - onDownloadTask: func() { hits.Add(1) }, + downloadTaskErr: status.Error(tc.code, "download failed"), + downloadTaskTrailer: tc.trailer, + onDownloadTask: func() { hits.Add(1) }, }) endpoint := setupMockScheduler(t, []*commonv2.Host{createSeedPeerHost("seed-peer-1", port, 0)}) @@ -480,8 +522,105 @@ func TestPreheatImageDownloadsManifestsAndBlobs(t *testing.T) { }, } + downloadTaskRequests := make(chan *dfdaemonv2.DownloadTaskRequest, 8) + port := setupMockSeedPeerServer(t, &mockSeedPeer{ + downloadTask: func(req *dfdaemonv2.DownloadTaskRequest) { downloadTaskRequests <- req }, + }) + endpoint := setupMockScheduler(t, []*commonv2.Host{createSeedPeerHost("seed-peer-1", port, 0)}) + + proxy, err := New(context.Background(), endpoint) + assert.NoError(t, err) + defer proxy.Close() + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := proxy.PreheatImage(context.Background(), tc.req) + + var downloads []*commonv2.Download + for len(downloadTaskRequests) > 0 { + downloads = append(downloads, (<-downloadTaskRequests).GetDownload()) + } + tc.expect(t, downloads, err) + }) + } +} + +func TestPreheatImageWithoutBearerToken(t *testing.T) { + manifest := `{ + "schemaVersion": 2, + "mediaType": "application/vnd.docker.distribution.manifest.v2+json", + "config": { + "mediaType": "application/vnd.docker.container.image.v1+json", + "size": 7023, + "digest": "sha256:b5b2b2c507a0944348e0303114d8d93aaaa081732b86451d9bce1f432a537bc7" + }, + "layers": [] + }` + + tests := []struct { + name string + challenge string + req func(registry string) *PreheatImageRequest + expect func(t *testing.T, downloads []*commonv2.Download, err error) + }{ + { + name: "anonymous", + req: func(registry string) *PreheatImageRequest { + return NewPreheatImageRequest(registry+"/foo/bar:1.0", WithPreheatImageRequestReplicas(1)) + }, + expect: func(t *testing.T, downloads []*commonv2.Download, err error) { + assert := assert.New(t) + assert.NoError(err) + assert.Len(downloads, 2) + for _, download := range downloads { + assert.NotContains(download.GetRequestHeader(), "Authorization") + } + }, + }, + { + name: "basic credentials", + challenge: `Basic realm="registry"`, + req: func(registry string) *PreheatImageRequest { + return NewPreheatImageRequest(registry+"/foo/bar:1.0", WithPreheatImageRequestReplicas(1), WithPreheatImageRequestAuth("foo", "bar")) + }, + expect: func(t *testing.T, downloads []*commonv2.Download, err error) { + assert := assert.New(t) + assert.NoError(err) + assert.Len(downloads, 2) + for _, download := range downloads { + assert.Equal("Basic Zm9vOmJhcg==", download.GetRequestHeader()["Authorization"]) + } + }, + }, + } + for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { + listener, err := net.Listen("tcp", net.JoinHostPort(ip.IPv4.String(), "0")) + if err != nil { + t.Fatal(err) + } + + mux := http.NewServeMux() + mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) { + if tc.challenge != "" && r.Header.Get("Authorization") == "" { + w.Header().Set("WWW-Authenticate", tc.challenge) + w.WriteHeader(http.StatusUnauthorized) + return + } + + w.WriteHeader(http.StatusOK) + }) + mux.HandleFunc("/v2/foo/bar/manifests/", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/vnd.docker.distribution.manifest.v2+json") + fmt.Fprint(w, manifest) + }) + server := httptest.NewUnstartedServer(mux) + server.Listener.Close() + server.Listener = listener + server.StartTLS() + defer server.Close() + downloadTaskRequests := make(chan *dfdaemonv2.DownloadTaskRequest, 8) port := setupMockSeedPeerServer(t, &mockSeedPeer{ downloadTask: func(req *dfdaemonv2.DownloadTaskRequest) { downloadTaskRequests <- req }, @@ -492,12 +631,11 @@ func TestPreheatImageDownloadsManifestsAndBlobs(t *testing.T) { assert.NoError(t, err) defer proxy.Close() - err = proxy.PreheatImage(context.Background(), tc.req) - close(downloadTaskRequests) + err = proxy.PreheatImage(context.Background(), tc.req(listener.Addr().String())) var downloads []*commonv2.Download - for downloadTaskRequest := range downloadTaskRequests { - downloads = append(downloads, downloadTaskRequest.GetDownload()) + for len(downloadTaskRequests) > 0 { + downloads = append(downloads, (<-downloadTaskRequests).GetDownload()) } tc.expect(t, downloads, err) }) diff --git a/client-request/go/request_test.go b/client-request/go/request_test.go index bccfaa9..912d0b3 100644 --- a/client-request/go/request_test.go +++ b/client-request/go/request_test.go @@ -34,6 +34,7 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/health" healthpb "google.golang.org/grpc/health/grpc_health_v1" + "google.golang.org/grpc/metadata" "google.golang.org/protobuf/types/known/emptypb" ) @@ -68,13 +69,14 @@ func setupMockSchedulerServer(t testing.TB, scheduler *mockScheduler) string { type mockSeedPeer struct { dfdaemonv2.UnimplementedDfdaemonUploadServer - downloadTaskErr error - onDownloadTask func() - downloadTask func(*dfdaemonv2.DownloadTaskRequest) - deleteTaskErr error - onDeleteTask func() - deleteImage func(*dfdaemonv2.DeleteImageRequest) (*dfdaemonv2.DeleteImageResponse, error) - statLocalTask func(*dfdaemonv2.StatLocalTaskRequest) (*dfdaemonv2.StatLocalTaskResponse, error) + downloadTaskErr error + downloadTaskTrailer metadata.MD + onDownloadTask func() + downloadTask func(*dfdaemonv2.DownloadTaskRequest) + deleteTaskErr error + onDeleteTask func() + deleteImage func(*dfdaemonv2.DeleteImageRequest) (*dfdaemonv2.DeleteImageResponse, error) + statLocalTask func(*dfdaemonv2.StatLocalTaskRequest) (*dfdaemonv2.StatLocalTaskResponse, error) } func (s *mockSeedPeer) DownloadTask(req *dfdaemonv2.DownloadTaskRequest, stream dfdaemonv2.DfdaemonUpload_DownloadTaskServer) error { @@ -87,6 +89,7 @@ func (s *mockSeedPeer) DownloadTask(req *dfdaemonv2.DownloadTaskRequest, stream } if s.downloadTaskErr != nil { + stream.SetTrailer(s.downloadTaskTrailer) return s.downloadTaskErr } diff --git a/client-request/go/stat.go b/client-request/go/stat.go index e105077..baafc8d 100644 --- a/client-request/go/stat.go +++ b/client-request/go/stat.go @@ -61,6 +61,10 @@ func (p *Proxy) StatImage(ctx context.Context, req *StatImageRequest) (*StatImag manifests, _, err := oci.Resolve(ctx, ref, opts...) if err != nil { + if cause := context.Cause(ctx); cause != nil { + return nil, cause + } + return nil, fmt.Errorf("%w: %v", ErrInternal, err) } diff --git a/client-request/rust/Cargo.lock b/client-request/rust/Cargo.lock index dd2f453..4e9dfc9 100644 --- a/client-request/rust/Cargo.lock +++ b/client-request/rust/Cargo.lock @@ -595,6 +595,7 @@ version = "1.14.1" dependencies = [ "async-trait", "backon", + "base64", "bytes", "criterion", "dashmap", diff --git a/client-request/rust/Cargo.toml b/client-request/rust/Cargo.toml index 5234dd0..02c343f 100644 --- a/client-request/rust/Cargo.toml +++ b/client-request/rust/Cargo.toml @@ -12,12 +12,13 @@ keywords = ["container", "docker-image", "dragonfly", "dragonfly-client", "p2p"] [features] default = [] -preheat = ["dep:oci-client"] +preheat = ["dep:base64", "dep:oci-client"] [dependencies] dragonfly-api = "2.3.10" dragonfly-hashring = "0.3.0" async-trait = "0.1" +base64 = { version = "0.22", optional = true } backon = { version = "1", default-features = false, features = ["std", "tokio-sleep"] } futures = "0.3.32" hostname = "^0.4" diff --git a/client-request/rust/src/lib.rs b/client-request/rust/src/lib.rs index cf5447a..eac5f85 100644 --- a/client-request/rust/src/lib.rs +++ b/client-request/rust/src/lib.rs @@ -50,6 +50,8 @@ use std::time::Duration; use tonic::transport::Channel; use tracing::{debug, warn}; +#[cfg(feature = "preheat")] +use base64::{engine::general_purpose::STANDARD, Engine}; #[cfg(feature = "preheat")] use dragonfly_api::{ common::v2::Image, @@ -1435,26 +1437,10 @@ impl Request for Proxy { request.image ); - // Authenticate with the registry and get a bearer token. - let token = client - .auth(&reference, &auth, RegistryOperation::Pull) - .await - .map_err(|err| Error::Internal(format!("failed to authenticate with registry: {err}")))? - .ok_or_else(|| { - Error::Internal("registry did not return authentication token".to_string()) - })?; - - // Build authorization header for blob downloads through the Dragonfly. - let mut header = HeaderMap::new(); - header.insert( - AUTHORIZATION, - HeaderValue::from_str(&format!("Bearer {token}")) - .map_err(|err| Error::Internal(format!("invalid auth token: {err}")))?, - ); - // Build the accept header with the manifest media types for downloading manifests, // so the registry returns the exact representation matching the digest. - let mut manifest_header = header.clone(); + let header = HeaderMap::new(); + let mut manifest_header = HeaderMap::new(); manifest_header.insert( ACCEPT, HeaderValue::from_str(&MIME_TYPES_DISTRIBUTION_MANIFEST.join(",")) @@ -1505,6 +1491,9 @@ impl Request for Proxy { for (url, header, image) in targets { let semaphore = semaphore.clone(); let proxy = self.clone(); + let client = client.clone(); + let reference = reference.clone(); + let auth = auth.clone(); let preheat_request = PreheatRequest { url, header, @@ -1562,6 +1551,36 @@ impl Request for Proxy { ))); } + // Authorize the target when it starts, because the token of the registry + // expires during a long preheat. A registry without a bearer token is + // authorized with the basic credentials, if any. + let token = client + .auth(&reference, &auth, RegistryOperation::Pull) + .await + .map_err(|err| { + Error::Internal(format!("failed to authenticate with registry: {err}")) + })?; + + let authorization = match (token, auth) { + (Some(token), _) => Some(format!("Bearer {token}")), + (None, RegistryAuth::Basic(username, password)) => Some(format!( + "Basic {}", + STANDARD.encode(format!("{username}:{password}")) + )), + (None, RegistryAuth::Anonymous) => None, + (None, RegistryAuth::Bearer(_)) => None, + }; + + let mut request_header = preheat_request.header.clone(); + if let Some(authorization) = authorization { + request_header.insert( + AUTHORIZATION, + HeaderValue::from_str(&authorization).map_err(|err| { + Error::Internal(format!("invalid authorization: {err}")) + })?, + ); + } + // Construct the download task request with the image. let download_task_request = DownloadTaskRequest { download: Some(Download { @@ -1571,7 +1590,7 @@ impl Request for Proxy { application: preheat_request.application.clone(), priority: preheat_request.priority.unwrap_or(Priority::Level6 as i32), filtered_query_params: preheat_request.filtered_query_params.clone(), - request_header: headermap_to_hashmap(&preheat_request.header), + request_header: headermap_to_hashmap(&request_header), piece_length: preheat_request.piece_length, timeout: Some( prost_wkt_types::Duration::try_from(preheat_request.timeout) diff --git a/test/e2e/driver/rust/Cargo.lock b/test/e2e/driver/rust/Cargo.lock index c8553cf..65d1e52 100644 --- a/test/e2e/driver/rust/Cargo.lock +++ b/test/e2e/driver/rust/Cargo.lock @@ -566,6 +566,7 @@ version = "1.14.1" dependencies = [ "async-trait", "backon", + "base64", "bytes", "dashmap", "dragonfly-api", diff --git a/test/e2e/go.mod b/test/e2e/go.mod index a5fb13e..bbb3006 100644 --- a/test/e2e/go.mod +++ b/test/e2e/go.mod @@ -4,7 +4,7 @@ go 1.25.0 require ( d7y.io/dragonfly-sdk/client-request/go v0.0.0 - d7y.io/dragonfly/v2 v2.5.4-rc.1 + d7y.io/dragonfly/v2 v2.5.4-rc.2 github.com/onsi/ginkgo/v2 v2.34.0 github.com/onsi/gomega v1.43.1 ) diff --git a/test/e2e/go.sum b/test/e2e/go.sum index d199228..f4e869d 100644 --- a/test/e2e/go.sum +++ b/test/e2e/go.sum @@ -1,7 +1,7 @@ d7y.io/api/v2 v2.3.10 h1:JFC6944ETIURMfLlaH0inH5NnZtzyAGU/mjHFJUkMOk= d7y.io/api/v2 v2.3.10/go.mod h1:hQOqfvk0SSuSUZ25VrF/L4dlP4JnREaUiUui07muLws= -d7y.io/dragonfly/v2 v2.5.4-rc.1 h1:I/me+EbhF0NgBZR6A/lwCOkZJ03BNFSfPeS6seGWliI= -d7y.io/dragonfly/v2 v2.5.4-rc.1/go.mod h1:DEbHajC5YnA9IMY3umQj59KauToyB58b5s7ZRU8DqGU= +d7y.io/dragonfly/v2 v2.5.4-rc.2 h1:HMTVQMNeIVITWwFvBDp7JWpzyakezBhT4/Y7YPyHWms= +d7y.io/dragonfly/v2 v2.5.4-rc.2/go.mod h1:DEbHajC5YnA9IMY3umQj59KauToyB58b5s7ZRU8DqGU= d7y.io/hashring v0.3.0 h1:qGIkSNa/g6hSFYV2mBE+mZU16NzzMiCKobti072ACL0= d7y.io/hashring v0.3.0/go.mod h1:NY2/+76/hCcBzcTEJiqJG2AX0gzlSq+YJJgSJyZWd1c= filippo.io/edwards25519 v1.2.0 h1:crnVqOiS4jqYleHd9vaKZ+HKtHfllngJIiOpNpoJsjo= diff --git a/test/testdata/charts/config.yaml b/test/testdata/charts/config.yaml index 294079e..e10f53f 100644 --- a/test/testdata/charts/config.yaml +++ b/test/testdata/charts/config.yaml @@ -1,7 +1,7 @@ scheduler: image: repository: dragonflyoss/scheduler - tag: v2.5.4-rc.1 + tag: v2.5.4-rc.2 replicas: 1 resources: requests: