mirror of
https://github.com/containers/podman.git
synced 2025-09-18 07:51:22 +08:00
Merge pull request #18954 from vrothberg/fix-15828
compat API create/pull/push: fix error handling
This commit is contained in:
@ -23,6 +23,7 @@ import (
|
||||
"github.com/containers/podman/v4/pkg/domain/infra/abi"
|
||||
"github.com/containers/podman/v4/pkg/util"
|
||||
"github.com/containers/storage"
|
||||
"github.com/docker/distribution/registry/api/errcode"
|
||||
"github.com/docker/docker/pkg/jsonmessage"
|
||||
"github.com/gorilla/schema"
|
||||
"github.com/opencontainers/go-digest"
|
||||
@ -313,18 +314,24 @@ func CreateImageFromImage(w http.ResponseWriter, r *http.Request) {
|
||||
pullResChan <- pullResult{images: pulledImages, err: err}
|
||||
}()
|
||||
|
||||
enc := json.NewEncoder(w)
|
||||
enc.SetEscapeHTML(true)
|
||||
|
||||
flush := func() {
|
||||
if flusher, ok := w.(http.Flusher); ok {
|
||||
flusher.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
flush()
|
||||
|
||||
enc := json.NewEncoder(w)
|
||||
enc.SetEscapeHTML(true)
|
||||
statusWritten := false
|
||||
writeStatusCode := func(code int) {
|
||||
if !statusWritten {
|
||||
w.WriteHeader(code)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
flush()
|
||||
statusWritten = true
|
||||
}
|
||||
}
|
||||
|
||||
loop: // break out of for/select infinite loop
|
||||
for {
|
||||
@ -332,6 +339,7 @@ loop: // break out of for/select infinite loop
|
||||
report.Progress = &jsonmessage.JSONProgress{}
|
||||
select {
|
||||
case e := <-progress:
|
||||
writeStatusCode(http.StatusOK)
|
||||
switch e.Event {
|
||||
case types.ProgressEventNewArtifact:
|
||||
report.Status = "Pulling fs layer"
|
||||
@ -352,14 +360,20 @@ loop: // break out of for/select infinite loop
|
||||
flush()
|
||||
case pullRes := <-pullResChan:
|
||||
err := pullRes.err
|
||||
pulledImages := pullRes.images
|
||||
if err != nil {
|
||||
var errcd errcode.ErrorCoder
|
||||
if errors.As(err, &errcd) {
|
||||
writeStatusCode(errcd.ErrorCode().Descriptor().HTTPStatusCode)
|
||||
} else {
|
||||
writeStatusCode(http.StatusInternalServerError)
|
||||
}
|
||||
msg := err.Error()
|
||||
report.Error = &jsonmessage.JSONError{
|
||||
Message: msg,
|
||||
}
|
||||
report.ErrorMessage = msg
|
||||
} else {
|
||||
pulledImages := pullRes.images
|
||||
if len(pulledImages) > 0 {
|
||||
img := pulledImages[0].ID()
|
||||
if utils.IsLibpodRequest(r) {
|
||||
@ -374,6 +388,7 @@ loop: // break out of for/select infinite loop
|
||||
Message: msg,
|
||||
}
|
||||
report.ErrorMessage = msg
|
||||
writeStatusCode(http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
if err := enc.Encode(report); err != nil {
|
||||
|
@ -108,24 +108,36 @@ func PushImage(w http.ResponseWriter, r *http.Request) {
|
||||
destination = imageName
|
||||
}
|
||||
|
||||
enc := json.NewEncoder(w)
|
||||
enc.SetEscapeHTML(true)
|
||||
|
||||
flush := func() {}
|
||||
if flusher, ok := w.(http.Flusher); ok {
|
||||
flush = flusher.Flush
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
flush()
|
||||
|
||||
var report jsonmessage.JSONMessage
|
||||
enc := json.NewEncoder(w)
|
||||
enc.SetEscapeHTML(true)
|
||||
|
||||
report.Status = fmt.Sprintf("The push refers to repository [%s]", imageName)
|
||||
if err := enc.Encode(report); err != nil {
|
||||
logrus.Warnf("Failed to json encode error %q", err.Error())
|
||||
statusWritten := false
|
||||
writeStatusCode := func(code int) {
|
||||
if !statusWritten {
|
||||
w.WriteHeader(code)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
flush()
|
||||
statusWritten = true
|
||||
}
|
||||
}
|
||||
|
||||
referenceWritten := false
|
||||
writeReference := func() {
|
||||
if !referenceWritten {
|
||||
var report jsonmessage.JSONMessage
|
||||
report.Status = fmt.Sprintf("The push refers to repository [%s]", imageName)
|
||||
if err := enc.Encode(report); err != nil {
|
||||
logrus.Warnf("Failed to json encode error %q", err.Error())
|
||||
}
|
||||
flush()
|
||||
referenceWritten = true
|
||||
}
|
||||
}
|
||||
flush()
|
||||
|
||||
pushErrChan := make(chan error)
|
||||
var pushReport *entities.ImagePushReport
|
||||
@ -137,10 +149,12 @@ func PushImage(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
loop: // break out of for/select infinite loop
|
||||
for {
|
||||
report = jsonmessage.JSONMessage{}
|
||||
var report jsonmessage.JSONMessage
|
||||
|
||||
select {
|
||||
case e := <-options.Progress:
|
||||
writeStatusCode(http.StatusOK)
|
||||
writeReference()
|
||||
switch e.Event {
|
||||
case types.ProgressEventNewArtifact:
|
||||
report.Status = "Preparing"
|
||||
@ -165,8 +179,11 @@ loop: // break out of for/select infinite loop
|
||||
if err != nil {
|
||||
var msg string
|
||||
if errors.Is(err, storage.ErrImageUnknown) {
|
||||
// Image may have been removed in the meantime.
|
||||
writeStatusCode(http.StatusNotFound)
|
||||
msg = "An image does not exist locally with the tag: " + imageName
|
||||
} else {
|
||||
writeStatusCode(http.StatusInternalServerError)
|
||||
msg = err.Error()
|
||||
}
|
||||
report.Error = &jsonmessage.JSONError{
|
||||
@ -178,6 +195,9 @@ loop: // break out of for/select infinite loop
|
||||
}
|
||||
flush()
|
||||
break loop
|
||||
} else {
|
||||
writeStatusCode(http.StatusOK)
|
||||
writeReference() // There may not be any progess, so make sure the reference gets written
|
||||
}
|
||||
|
||||
tag := query.Tag
|
||||
|
Reference in New Issue
Block a user