Skip to content

Commit

Permalink
Merge pull request #10517 from cdoern/master
Browse files Browse the repository at this point in the history
API one-shot query implementation/handling
  • Loading branch information
openshift-merge-robot authored Jun 1, 2021
2 parents 24b364a + 2cc4535 commit 1f3b137
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
30 changes: 17 additions & 13 deletions pkg/api/handlers/compat/containers_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
decoder := r.Context().Value("decoder").(*schema.Decoder)

query := struct {
Stream bool `schema:"stream"`
Stream bool `schema:"stream"`
OneShot bool `schema:"one-shot"` //added schema for one shot
}{
Stream: true,
}
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
utils.Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
return
}
if query.Stream && query.OneShot { // mismatch. one-shot can only be passed with stream=false
utils.Error(w, "invalid combination of stream and one-shot", http.StatusBadRequest, define.ErrInvalidArg)
return
}

name := utils.GetName(r)
ctnr, err := runtime.LookupContainer(name)
Expand All @@ -56,6 +61,16 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
return
}

coder := json.NewEncoder(w)
// Write header and content type.
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json")
if flusher, ok := w.(http.Flusher); ok {
flusher.Flush()
}

// Setup JSON encoder for streaming.
coder.SetEscapeHTML(true)
var preRead time.Time
var preCPUStats CPUStats
if query.Stream {
Expand All @@ -75,17 +90,6 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
}
}

// Write header and content type.
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json")
if flusher, ok := w.(http.Flusher); ok {
flusher.Flush()
}

// Setup JSON encoder for streaming.
coder := json.NewEncoder(w)
coder.SetEscapeHTML(true)

streamLabel: // A label to flatten the scope
select {
case <-r.Context().Done():
Expand Down Expand Up @@ -199,7 +203,7 @@ streamLabel: // A label to flatten the scope
flusher.Flush()
}

if !query.Stream {
if !query.Stream || query.OneShot {
return
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/api/server/register_containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ func (s *APIServer) registerContainersHandlers(r *mux.Router) error {
// type: boolean
// default: true
// description: Stream the output
// - in: query
// name: one-shot
// type: boolean
// default: false
// description: Provide a one-shot response in which preCPU stats are blank, resulting in a single cycle return.
// produces:
// - application/json
// responses:
Expand Down
4 changes: 4 additions & 0 deletions test/apiv2/python/rest_api/test_v2_0_0_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def test_stats(self):
self.assertIn(r.status_code, (200, 409), r.text)
if r.status_code == 200:
self.assertId(r.content)
r = requests.get(self.uri(self.resolve_container("/containers/{}/stats?stream=false&one-shot=true")))
self.assertIn(r.status_code, (200, 409), r.text)
if r.status_code == 200:
self.assertId(r.content)

def test_delete(self):
r = requests.delete(self.uri(self.resolve_container("/containers/{}")))
Expand Down

0 comments on commit 1f3b137

Please sign in to comment.