Skip to content

Commit

Permalink
Fall back to no mount if registry misbehaves (#1406)
Browse files Browse the repository at this point in the history
Sometimes we see authentication errors if the from repo doesn't exist,
so fall back to just initiating an upload without a mount attempt.
  • Loading branch information
jonjohnsonjr committed Jul 12, 2022
1 parent 4d7b65b commit ddd39fb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/v1/remote/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ func (w *writer) initiateUpload(from, mount, origin string) (location string, mo
defer resp.Body.Close()

if err := transport.CheckError(resp, http.StatusCreated, http.StatusAccepted); err != nil {
if origin != "" && origin != w.repo.RegistryStr() {
// https://github.com/google/go-containerregistry/issues/1404
logs.Warn.Printf("retrying without mount: %v", err)
return w.initiateUpload("", "", "")
}
return "", false, err
}

Expand Down
50 changes: 50 additions & 0 deletions pkg/v1/remote/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,56 @@ func TestInitiateUploadMountsWithOrigin(t *testing.T) {
}
}

func TestInitiateUploadMountsWithOriginFallback(t *testing.T) {
img := setupImage(t)
h := mustConfigName(t, img)
expectedMountRepo := "a/different/repo"
expectedRepo := "yet/again"
expectedPath := fmt.Sprintf("/v2/%s/blobs/uploads/", expectedRepo)
expectedOrigin := "fakeOrigin"
expectedQuery := url.Values{
"mount": []string{h.String()},
"from": []string{expectedMountRepo},
"origin": []string{expectedOrigin},
}.Encode()

queries := []string{expectedQuery, ""}
queryCount := 0

serverHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("Method; got %v, want %v", r.Method, http.MethodPost)
}
if r.URL.Path != expectedPath {
t.Errorf("URL; got %v, want %v", r.URL.Path, expectedPath)
}
if r.URL.RawQuery != queries[queryCount] {
t.Errorf("RawQuery; got %v, want %v", r.URL.RawQuery, expectedQuery)
}
if queryCount == 0 {
http.Error(w, "nope", http.StatusUnauthorized)
} else {
http.Error(w, "Mounted", http.StatusCreated)
}
queryCount++
})
server := httptest.NewServer(serverHandler)

w, closer, err := setupWriterWithServer(server, expectedRepo)
if err != nil {
t.Fatalf("setupWriterWithServer() = %v", err)
}
defer closer.Close()

_, mounted, err := w.initiateUpload(expectedMountRepo, h.String(), "fakeOrigin")
if err != nil {
t.Errorf("intiateUpload() = %v", err)
}
if !mounted {
t.Error("initiateUpload() = !mounted, want mounted")
}
}

func TestDedupeLayers(t *testing.T) {
newBlob := func() io.ReadCloser { return ioutil.NopCloser(bytes.NewReader(bytes.Repeat([]byte{'a'}, 10000))) }

Expand Down

0 comments on commit ddd39fb

Please sign in to comment.