Fix dragonfly preheat error.

Signed-off-by: yunkunrao <yunkunrao@gmail.com>
This commit is contained in:
yunkunrao 2021-05-23 17:48:50 +08:00
parent 4492e47e89
commit fa8a0d09d6
2 changed files with 17 additions and 2 deletions

View File

@ -13,6 +13,8 @@ import (
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/provider/auth"
common_http "github.com/goharbor/harbor/src/common/http"
)
const (
@ -193,6 +195,10 @@ func (hc *HTTPClient) post(url string, cred *auth.Credential, body interface{},
if (res.StatusCode / 100) != 2 {
// Return the server error content in the error.
return nil, fmt.Errorf("%s '%s' error: %s %s", http.MethodPost, res.Request.URL.String(), res.Status, bytes)
} else if res.StatusCode == http.StatusAlreadyReported {
// Currently because if image was already preheated at least once, Dragonfly will return StatusAlreadyReported.
// And we should preserve http status code info to process this case later.
return bytes, &common_http.Error{Code: http.StatusAlreadyReported, Message: "status already reported"}
}
return bytes, nil

View File

@ -4,8 +4,10 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
common_http "github.com/goharbor/harbor/src/common/http"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/models/provider"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/provider/auth"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/provider/client"
@ -76,10 +78,17 @@ func (dd *DragonflyDriver) Preheat(preheatingImage *PreheatImage) (*PreheatingSt
return nil, errors.New("no image specified")
}
taskStatus := provider.PreheatingStatusPending // default
url := fmt.Sprintf("%s%s", strings.TrimSuffix(dd.instance.Endpoint, "/"), preheatEndpoint)
bytes, err := client.GetHTTPClient(dd.instance.Insecure).Post(url, dd.getCred(), preheatingImage, nil)
if err != nil {
return nil, err
if httpErr, ok := err.(*common_http.Error); ok && httpErr.Code == http.StatusAlreadyReported {
// If the resource was preheated already with empty task ID, we should set preheat status to success.
// Otherwise later querying for the task
taskStatus = provider.PreheatingStatusSuccess
} else {
return nil, err
}
}
result := &dragonflyPreheatCreateResp{}
@ -89,7 +98,7 @@ func (dd *DragonflyDriver) Preheat(preheatingImage *PreheatImage) (*PreheatingSt
return &PreheatingStatus{
TaskID: result.ID,
Status: provider.PreheatingStatusPending, // default
Status: taskStatus,
}, nil
}