Merge pull request #14825 from jmguzik/fix_streaming_pod_stats

Fix streaming for libpod/pods/stats endpoint
This commit is contained in:
openshift-ci[bot]
2022-07-07 12:58:25 +00:00
committed by GitHub
3 changed files with 62 additions and 3 deletions

View File

@ -530,8 +530,12 @@ func PodStats(w http.ResponseWriter, r *http.Request) {
query := struct {
NamesOrIDs []string `schema:"namesOrIDs"`
All bool `schema:"all"`
Stream bool `schema:"stream"`
Delay int `schema:"delay"`
}{
// default would go here
Delay: 5,
Stream: false,
}
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
utils.Error(w, http.StatusBadRequest, fmt.Errorf("failed to parse parameters for %s: %w", r.URL.String(), err))
@ -544,6 +548,10 @@ func PodStats(w http.ResponseWriter, r *http.Request) {
utils.InternalServerError(w, err)
}
var flush = func() {}
if flusher, ok := w.(http.Flusher); ok {
flush = flusher.Flush
}
// Collect the stats and send them over the wire.
containerEngine := abi.ContainerEngine{Libpod: runtime}
reports, err := containerEngine.PodStats(r.Context(), query.NamesOrIDs, options)
@ -554,10 +562,35 @@ func PodStats(w http.ResponseWriter, r *http.Request) {
utils.Error(w, http.StatusNotFound, err)
return
}
utils.InternalServerError(w, err)
return
}
utils.WriteResponse(w, http.StatusOK, reports)
w.Header().Set("Content-Type", "application/json")
coder := json.NewEncoder(w)
coder.SetEscapeHTML(true)
if err := coder.Encode(reports); err != nil {
logrus.Infof("Error from %s %q : %v", r.Method, r.URL, err)
}
flush()
if query.Stream {
for {
select {
case <-r.Context().Done():
return
default:
time.Sleep(time.Duration(query.Delay) * time.Second)
reports, err = containerEngine.PodStats(r.Context(), query.NamesOrIDs, options)
if err != nil {
return
}
if err := coder.Encode(reports); err != nil {
logrus.Infof("Error from %s %q : %v", r.Method, r.URL, err)
return
}
flush()
}
}
}
}

View File

@ -134,4 +134,6 @@ t GET libpod/pods/json?filters='{"label":["testl' 400 \
t DELETE libpod/pods/foo 200
t DELETE "libpod/pods/foo (pod has already been deleted)" 404
t_timeout 5 GET "libpod/pods/stats?stream=true&delay=1" 200
# vim: filetype=sh

View File

@ -56,6 +56,9 @@ fi
# Path to podman binary
PODMAN_BIN=${PODMAN:-${CONTAINERS_HELPER_BINARY_DIR}/podman}
# Timeout for streamed responses
CURL_TIMEOUT=0
# Cleanup handlers
clean_up_server() {
if [ -n "$service_pid" ]; then
@ -216,6 +219,21 @@ function jsonify() {
(IFS=','; echo "{${settings_out[*]}}")
}
#######
# t_timeout # Timeout wrapper for test helper
#######
function t_timeout() {
CURL_TIMEOUT=$1; shift
local min_runtime=$((CURL_TIMEOUT - 1))
start=`date +%s`
t $@
local end=`date +%s`
local runtime=$((end-start))
if ! [[ "$runtime" -ge "$min_runtime" ]]; then
die "Error: Streaming time should be greater or equal to '$min_runtime'"
fi
}
#######
# t # Main test helper
#######
@ -226,6 +244,12 @@ function t() {
local content_type="application/json"
local testname="$method $path"
if [[ $CURL_TIMEOUT != 0 ]]; then
local c_timeout=$CURL_TIMEOUT
curl_args+=("-m $CURL_TIMEOUT")
CURL_TIMEOUT=0 # 'consume' timeout
fi
# POST and PUT requests may be followed by one or more key=value pairs.
# Slurp the command line until we see a 3-digit status code.
if [[ $method = "POST" || $method == "PUT" ]]; then
@ -291,7 +315,7 @@ function t() {
-o $WORKDIR/curl.result.out "$url"); rc=$?; } || :
# Any error from curl is instant bad news, from which we can't recover
if [[ $rc -ne 0 ]]; then
if [[ $rc -ne 0 ]] && [[ $c_timeout -eq 0 ]]; then
die "curl failure ($rc) on $url - cannot continue"
fi