mirror of
https://github.com/containers/podman.git
synced 2025-06-17 23:20:59 +08:00
Merge pull request #10519 from matejvasek/use-req-ctx
Use request context instead of background
This commit is contained in:
@ -1,7 +1,6 @@
|
|||||||
package compat
|
package compat
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -12,7 +11,6 @@ import (
|
|||||||
"github.com/containers/podman/v3/libpod"
|
"github.com/containers/podman/v3/libpod"
|
||||||
"github.com/containers/podman/v3/pkg/api/handlers/utils"
|
"github.com/containers/podman/v3/pkg/api/handlers/utils"
|
||||||
"github.com/containers/podman/v3/pkg/auth"
|
"github.com/containers/podman/v3/pkg/auth"
|
||||||
"github.com/containers/podman/v3/pkg/channel"
|
|
||||||
"github.com/containers/podman/v3/pkg/domain/entities"
|
"github.com/containers/podman/v3/pkg/domain/entities"
|
||||||
"github.com/containers/podman/v3/pkg/domain/infra/abi"
|
"github.com/containers/podman/v3/pkg/domain/infra/abi"
|
||||||
"github.com/containers/storage"
|
"github.com/containers/storage"
|
||||||
@ -101,46 +99,33 @@ func PushImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
destination = imageName
|
destination = imageName
|
||||||
}
|
}
|
||||||
|
|
||||||
errorWriter := channel.NewWriter(make(chan []byte))
|
flush := func() {}
|
||||||
defer errorWriter.Close()
|
if flusher, ok := w.(http.Flusher); ok {
|
||||||
|
flush = flusher.Flush
|
||||||
statusWriter := channel.NewWriter(make(chan []byte))
|
|
||||||
defer statusWriter.Close()
|
|
||||||
|
|
||||||
runCtx, cancel := context.WithCancel(context.Background())
|
|
||||||
var failed bool
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
statusWriter.Write([]byte(fmt.Sprintf("The push refers to repository [%s]", imageName)))
|
|
||||||
|
|
||||||
err := imageEngine.Push(runCtx, imageName, destination, options)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Cause(err) != storage.ErrImageUnknown {
|
|
||||||
errorWriter.Write([]byte("An image does not exist locally with the tag: " + imageName))
|
|
||||||
} else {
|
|
||||||
errorWriter.Write([]byte(err.Error()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
flush := func() {
|
|
||||||
if flusher, ok := w.(http.Flusher); ok {
|
|
||||||
flusher.Flush()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
flush()
|
flush()
|
||||||
|
|
||||||
|
var report jsonmessage.JSONMessage
|
||||||
enc := json.NewEncoder(w)
|
enc := json.NewEncoder(w)
|
||||||
enc.SetEscapeHTML(true)
|
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())
|
||||||
|
}
|
||||||
|
flush()
|
||||||
|
|
||||||
|
pushErrChan := make(chan error)
|
||||||
|
go func() {
|
||||||
|
pushErrChan <- imageEngine.Push(r.Context(), imageName, destination, options)
|
||||||
|
}()
|
||||||
|
|
||||||
loop: // break out of for/select infinite loop
|
loop: // break out of for/select infinite loop
|
||||||
for {
|
for {
|
||||||
var report jsonmessage.JSONMessage
|
report = jsonmessage.JSONMessage{}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case e := <-options.Progress:
|
case e := <-options.Progress:
|
||||||
@ -159,44 +144,51 @@ loop: // break out of for/select infinite loop
|
|||||||
report.Status = "Pushed"
|
report.Status = "Pushed"
|
||||||
}
|
}
|
||||||
report.ID = e.Artifact.Digest.Encoded()[0:12]
|
report.ID = e.Artifact.Digest.Encoded()[0:12]
|
||||||
if err := enc.Encode(report); err != nil {
|
|
||||||
errorWriter.Write([]byte(err.Error()))
|
|
||||||
}
|
|
||||||
flush()
|
|
||||||
case e := <-statusWriter.Chan():
|
|
||||||
report.Status = string(e)
|
|
||||||
if err := enc.Encode(report); err != nil {
|
|
||||||
errorWriter.Write([]byte(err.Error()))
|
|
||||||
}
|
|
||||||
flush()
|
|
||||||
case e := <-errorWriter.Chan():
|
|
||||||
failed = true
|
|
||||||
report.Error = &jsonmessage.JSONError{
|
|
||||||
Message: string(e),
|
|
||||||
}
|
|
||||||
report.ErrorMessage = string(e)
|
|
||||||
if err := enc.Encode(report); err != nil {
|
if err := enc.Encode(report); err != nil {
|
||||||
logrus.Warnf("Failed to json encode error %q", err.Error())
|
logrus.Warnf("Failed to json encode error %q", err.Error())
|
||||||
}
|
}
|
||||||
flush()
|
flush()
|
||||||
case <-runCtx.Done():
|
case err := <-pushErrChan:
|
||||||
if !failed {
|
if err != nil {
|
||||||
digestBytes, err := ioutil.ReadAll(digestFile)
|
var msg string
|
||||||
if err == nil {
|
if errors.Cause(err) != storage.ErrImageUnknown {
|
||||||
tag := query.Tag
|
msg = "An image does not exist locally with the tag: " + imageName
|
||||||
if tag == "" {
|
} else {
|
||||||
tag = "latest"
|
msg = err.Error()
|
||||||
}
|
|
||||||
report.Status = fmt.Sprintf("%s: digest: %s", tag, string(digestBytes))
|
|
||||||
if err := enc.Encode(report); err != nil {
|
|
||||||
logrus.Warnf("Failed to json encode error %q", err.Error())
|
|
||||||
}
|
|
||||||
flush()
|
|
||||||
}
|
}
|
||||||
|
report.Error = &jsonmessage.JSONError{
|
||||||
|
Message: msg,
|
||||||
|
}
|
||||||
|
report.ErrorMessage = msg
|
||||||
|
if err := enc.Encode(report); err != nil {
|
||||||
|
logrus.Warnf("Failed to json encode error %q", err.Error())
|
||||||
|
}
|
||||||
|
flush()
|
||||||
|
break loop
|
||||||
}
|
}
|
||||||
break loop // break out of for/select infinite loop
|
|
||||||
case <-r.Context().Done():
|
digestBytes, err := ioutil.ReadAll(digestFile)
|
||||||
// Client has closed connection
|
if err != nil {
|
||||||
|
report.Error = &jsonmessage.JSONError{
|
||||||
|
Message: err.Error(),
|
||||||
|
}
|
||||||
|
report.ErrorMessage = err.Error()
|
||||||
|
if err := enc.Encode(report); err != nil {
|
||||||
|
logrus.Warnf("Failed to json encode error %q", err.Error())
|
||||||
|
}
|
||||||
|
flush()
|
||||||
|
break loop
|
||||||
|
}
|
||||||
|
tag := query.Tag
|
||||||
|
if tag == "" {
|
||||||
|
tag = "latest"
|
||||||
|
}
|
||||||
|
report.Status = fmt.Sprintf("%s: digest: %s", tag, string(digestBytes))
|
||||||
|
if err := enc.Encode(report); err != nil {
|
||||||
|
logrus.Warnf("Failed to json encode error %q", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
flush()
|
||||||
break loop // break out of for/select infinite loop
|
break loop // break out of for/select infinite loop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user