forked from containers/podman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement top streaming for containers and pods
* Implement API query parameter stream and delay for containers and pods top endpoints * Update swagger with breaking changes * Add python API tests for endpoints Fixes containers#12115 Signed-off-by: Jhon Honce <[email protected]>
- Loading branch information
Showing
6 changed files
with
237 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,106 @@ | ||
package compat | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/containers/podman/v3/libpod" | ||
"github.com/containers/podman/v3/pkg/api/handlers" | ||
"github.com/containers/podman/v3/pkg/api/handlers/utils" | ||
api "github.com/containers/podman/v3/pkg/api/types" | ||
"github.com/gorilla/schema" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func TopContainer(w http.ResponseWriter, r *http.Request) { | ||
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) | ||
decoder := r.Context().Value(api.DecoderKey).(*schema.Decoder) | ||
|
||
defaultValue := "-ef" | ||
psArgs := "-ef" | ||
if utils.IsLibpodRequest(r) { | ||
defaultValue = "" | ||
psArgs = "" | ||
} | ||
query := struct { | ||
Delay int `schema:"delay"` | ||
PsArgs string `schema:"ps_args"` | ||
Stream bool `schema:"stream"` | ||
}{ | ||
PsArgs: defaultValue, | ||
Delay: 5, | ||
PsArgs: psArgs, | ||
} | ||
if err := decoder.Decode(&query, r.URL.Query()); err != nil { | ||
utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, | ||
errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String())) | ||
return | ||
} | ||
|
||
if query.Delay < 1 { | ||
utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, | ||
fmt.Errorf("\"delay\" parameter of value %d < 1", query.Delay)) | ||
return | ||
} | ||
|
||
name := utils.GetName(r) | ||
c, err := runtime.LookupContainer(name) | ||
if err != nil { | ||
utils.ContainerNotFound(w, name, err) | ||
return | ||
} | ||
|
||
output, err := c.Top([]string{query.PsArgs}) | ||
if err != nil { | ||
utils.InternalServerError(w, err) | ||
return | ||
// We are committed now - all errors logged but not reported to client, ship has sailed | ||
w.WriteHeader(http.StatusOK) | ||
w.Header().Set("Content-Type", "application/json") | ||
if f, ok := w.(http.Flusher); ok { | ||
f.Flush() | ||
} | ||
|
||
var body = handlers.ContainerTopOKBody{} | ||
if len(output) > 0 { | ||
body.Titles = strings.Split(output[0], "\t") | ||
for i := range body.Titles { | ||
body.Titles[i] = strings.TrimSpace(body.Titles[i]) | ||
} | ||
encoder := json.NewEncoder(w) | ||
|
||
loop: // break out of for/select infinite` loop | ||
for { | ||
select { | ||
case <-r.Context().Done(): | ||
break loop | ||
default: | ||
output, err := c.Top([]string{query.PsArgs}) | ||
if err != nil { | ||
logrus.Infof("Error from %s %q : %v", r.Method, r.URL, err) | ||
break loop | ||
} | ||
|
||
if len(output) > 0 { | ||
body := handlers.ContainerTopOKBody{} | ||
body.Titles = strings.Split(output[0], "\t") | ||
for i := range body.Titles { | ||
body.Titles[i] = strings.TrimSpace(body.Titles[i]) | ||
} | ||
|
||
for _, line := range output[1:] { | ||
process := strings.Split(line, "\t") | ||
for i := range process { | ||
process[i] = strings.TrimSpace(process[i]) | ||
} | ||
body.Processes = append(body.Processes, process) | ||
} | ||
|
||
if err := encoder.Encode(body); err != nil { | ||
logrus.Infof("Error from %s %q : %v", r.Method, r.URL, err) | ||
break loop | ||
} | ||
if f, ok := w.(http.Flusher); ok { | ||
f.Flush() | ||
} | ||
} | ||
|
||
for _, line := range output[1:] { | ||
process := strings.Split(line, "\t") | ||
for i := range process { | ||
process[i] = strings.TrimSpace(process[i]) | ||
if query.Stream { | ||
time.Sleep(time.Duration(query.Delay) * time.Second) | ||
} else { | ||
break loop | ||
} | ||
body.Processes = append(body.Processes, process) | ||
} | ||
} | ||
utils.WriteJSON(w, http.StatusOK, body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.