Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,7 @@ func (s *Server) upgradeContainer(ctx context.Context, rw http.ResponseWriter, r
rw.WriteHeader(http.StatusOK)
return nil
}

func (s *Server) topContainer(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
return nil
}
1 change: 1 addition & 0 deletions apis/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func initRoute(s *Server) http.Handler {
r.Path(versionMatcher + "/containers/{name:.*}/unpause").Methods(http.MethodPost).Handler(s.filter(s.unpauseContainer))
r.Path(versionMatcher + "/containers/{name:.*}/update").Methods(http.MethodPost).Handler(s.filter(s.updateContainer))
r.Path(versionMatcher + "/containers/{name:.*}/upgrade").Methods(http.MethodPost).Handler(s.filter(s.upgradeContainer))
r.Path(versionMatcher + "/containers/{name:.*}/top").Methods(http.MethodGet).Handler(s.filter(s.topContainer))

// image
r.Path(versionMatcher + "/images/create").Methods(http.MethodPost).Handler(s.filter(s.pullImage))
Expand Down
37 changes: 37 additions & 0 deletions apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,27 @@ paths:
$ref: "#/responses/500ErrorResponse"
tags: ["Container"]

/containers/{id}/top:
post:
summary: "Display the running processes of a container"
operationId: "ContainerTop"
parameters:
- $ref: "#/parameters/id"
- name: "ps_args"
in: "query"
description: "top arguments"
type: "string"
responses:
200:
description: "no error"
schema:
$ref: "#/definitions/ContainerProcessList"
404:
$ref: "#/responses/404ErrorResponse"
500:
$ref: "#/responses/500ErrorResponse"
tags: ["Container"]

/containers/{id}:
delete:
summary: "Remove one container"
Expand Down Expand Up @@ -2002,6 +2023,22 @@ definitions:
minItems: 1
items:
type: "string"
ContainerProcessList:
description: OK Response to ContainerTop operation
type: "object"
properties:
Titles:
description: "The ps column titles"
type: "array"
items:
type: "string"
Processes:
description: "Each process running in the container, where each is process is an array of values corresponding to the titles"
type: "array"
items:
type: "array"
items:
type: "string"

ExecCreateResp:
type: "object"
Expand Down
85 changes: 85 additions & 0 deletions apis/types/container_process_list.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions cli/top.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"context"
"fmt"

//"github.com/alibaba/pouch/apis/types"
//"github.com/alibaba/pouch/pkg/reference"

"github.com/spf13/cobra"
)

// topDescription
var topDescription = ""

// TopCommand use to implement 'top' command, it displays all processes in a container.
type TopCommand struct {
baseCommand
args []string
}

// Init initialize top command.
func (top *TopCommand) Init(c *Cli) {
top.cli = c
top.cmd = &cobra.Command{
Use: "top CONTAINER",
Short: "Display the running processes of a container",
Long: topDescription,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return top.runTop(args)
},
Example: topExamples(),
}
}

// runTop is the entry of top command.
func (top *TopCommand) runTop(args []string) error {
ctx := context.Background()
apiClient := top.cli.Client()

container := args[0]

arguments := args[1:]
Copy link
Collaborator

@allencloud allencloud Mar 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You add Args: cobra.ExactArgs(1),
So arguments := args[1:] is meaningless.
I think inconsistency happens.
Please correct this in the following PR.


resp, err := apiClient.ContainerTop(ctx, container, arguments)
if err != nil {
return fmt.Errorf("failed to execute top command in container %s: %v", container, err)
}

fmt.Println(resp)
return nil
}

// topExamples shows examples in top command, and is used in auto-generated cli docs.
func topExamples() string {
return ``
}
19 changes: 19 additions & 0 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"net"
"net/url"
"strings"

"github.com/alibaba/pouch/apis/types"
)
Expand Down Expand Up @@ -186,3 +187,21 @@ func (client *APIClient) ContainerUpdate(ctx context.Context, name string, confi
func (client *APIClient) ContainerUpgrade(ctx context.Context, name string, config types.ContainerConfig, hostConfig *types.HostConfig) error {
return nil
}

// ContainerTop shows process information from within a container.
func (client *APIClient) ContainerTop(ctx context.Context, name string, arguments []string) (types.ContainerProcessList, error) {
response := types.ContainerProcessList{}
query := url.Values{}
if len(arguments) > 0 {
query.Set("ps_args", strings.Join(arguments, " "))
}

resp, err := client.get(ctx, "/containers/"+name+"/top", query, nil)
if err != nil {
return response, err
}

err = decodeBody(&response, resp.Body)
ensureCloseReader(resp)
return response, err
}
1 change: 1 addition & 0 deletions client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type ContainerAPIClient interface {
ContainerUnpause(ctx context.Context, name string) error
ContainerUpdate(ctx context.Context, name string, config *types.UpdateConfig) error
ContainerUpgrade(ctx context.Context, name string, config types.ContainerConfig, hostConfig *types.HostConfig) error
ContainerTop(ctx context.Context, name string, arguments []string) (types.ContainerProcessList, error)
}

// ImageAPIClient defines methods of Image client.
Expand Down
22 changes: 22 additions & 0 deletions test/api_container_top_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"github.com/alibaba/pouch/test/environment"
//"github.com/alibaba/pouch/test/request"

"github.com/go-check/check"
)

// APIContainerTopSuite is the test suite for container top API.
type APIContainerTopSuite struct{}

func init() {
check.Suite(&APIContainerTopSuite{})
}

// SetUpTest does common setup in the beginning of each test.
func (suite *APIContainerTopSuite) SetUpTest(c *check.C) {
SkipIfFalse(c, environment.IsLinux)

// TODO Add more
}
29 changes: 29 additions & 0 deletions test/cli_top_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"github.com/alibaba/pouch/test/command"
"github.com/alibaba/pouch/test/environment"

"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
)

// PouchTopSuite is the test suite for top CLI.
type PouchTopSuite struct{}

func init() {
check.Suite(&PouchTopSuite{})
}

// SetupSuite does common setup in the beginning of each test suite.
func (suite *PouchTopSuite) SetupSuite(c *check.C) {
SkipIfFalse(c, environment.IsLinux)

environment.PruneAllContainers(apiClient)

command.PouchRun("pull", busyboxImage).Assert(c, icmd.Success)
}

// TearDownTest does cleanup work in the end of each test.
func (suite *PouchTopSuite) TearDownTest(c *check.C) {
}