Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions client-request/go/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions client-request/go/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
},
{
Expand All @@ -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)
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion client-request/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.2
d7y.io/hashring v0.3.0
github.com/cenkalti/backoff/v5 v5.0.3
github.com/containerd/platforms v0.2.1
Expand Down
4 changes: 2 additions & 2 deletions client-request/go/go.sum
Original file line number Diff line number Diff line change
@@ -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.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=
Expand Down
54 changes: 44 additions & 10 deletions client-request/go/preheat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package request

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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())}
}
}
})
Expand Down
150 changes: 144 additions & 6 deletions client-request/go/preheat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)})

Expand Down Expand Up @@ -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 },
Expand All @@ -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)
})
Expand Down
Loading
Loading