Skip to content

Commit 454bd58

Browse files
authored
Merge pull request containerd#10031 from ktock/fail-plain-http-2.0
remote: Fix HTTPFallback fails when pushing manifest
2 parents d29df16 + 63d5573 commit 454bd58

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

core/remotes/docker/pusher_test.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package docker
1818

1919
import (
20+
"bytes"
2021
"context"
22+
"encoding/json"
2123
"errors"
2224
"fmt"
2325
"io"
@@ -35,6 +37,7 @@ import (
3537
"github.com/containerd/errdefs"
3638
"github.com/containerd/log/logtest"
3739
"github.com/opencontainers/go-digest"
40+
ocispecv "github.com/opencontainers/image-spec/specs-go"
3841
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3942
"github.com/stretchr/testify/assert"
4043
)
@@ -181,10 +184,55 @@ func tryUpload(ctx context.Context, t *testing.T, p dockerPusher, layerContent [
181184
return err
182185
}
183186
defer cw.Close()
184-
if _, err := cw.Write(layerContent); err != nil {
187+
if err := content.Copy(ctx, cw, bytes.NewReader(layerContent), int64(len(layerContent)), desc.Digest); err != nil {
185188
return err
186189
}
187-
return cw.Commit(ctx, 0, "")
190+
191+
cContent, err := json.Marshal(ocispec.Image{})
192+
if err != nil {
193+
return err
194+
}
195+
cdesc := ocispec.Descriptor{
196+
MediaType: ocispec.MediaTypeImageConfig,
197+
Digest: digest.FromBytes(cContent),
198+
Size: int64(len(cContent)),
199+
}
200+
cwc, err := p.Writer(ctx, content.WithRef("test-1-c"), content.WithDescriptor(cdesc))
201+
if err != nil {
202+
return err
203+
}
204+
defer cwc.Close()
205+
if _, err := cwc.Write(cContent); err != nil {
206+
return err
207+
}
208+
if err := content.Copy(ctx, cwc, bytes.NewReader(cContent), int64(len(cContent)), cdesc.Digest); err != nil {
209+
return err
210+
}
211+
212+
m := ocispec.Manifest{
213+
Versioned: ocispecv.Versioned{SchemaVersion: 1},
214+
MediaType: ocispec.MediaTypeImageManifest,
215+
Config: cdesc,
216+
Layers: []ocispec.Descriptor{desc},
217+
}
218+
mContent, err := json.Marshal(m)
219+
if err != nil {
220+
return err
221+
}
222+
mdesc := ocispec.Descriptor{
223+
MediaType: ocispec.MediaTypeImageManifest,
224+
Digest: digest.FromBytes(mContent),
225+
Size: int64(len(mContent)),
226+
}
227+
cwm, err := p.Writer(ctx, content.WithRef("test-1-m"), content.WithDescriptor(mdesc))
228+
if err != nil {
229+
return err
230+
}
231+
defer cwm.Close()
232+
if err := content.Copy(ctx, cwm, bytes.NewReader(mContent), int64(len(mContent)), mdesc.Digest); err != nil {
233+
return err
234+
}
235+
return nil
188236
}
189237

190238
func samplePusher(t *testing.T) (dockerPusher, *uploadableMockRegistry, StatusTrackLocker, func()) {

core/remotes/docker/resolver.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,14 @@ func (f HTTPFallback) RoundTrip(r *http.Request) (*http.Response, error) {
732732
plainHTTPRequest := *r
733733
plainHTTPRequest.URL = &plainHTTPUrl
734734

735+
if r.Body != nil && r.GetBody != nil {
736+
body, err := r.GetBody()
737+
if err != nil {
738+
return nil, err
739+
}
740+
plainHTTPRequest.Body = body
741+
}
742+
735743
return f.RoundTripper.RoundTrip(&plainHTTPRequest)
736744
}
737745

0 commit comments

Comments
 (0)