-
Notifications
You must be signed in to change notification settings - Fork 947
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In fact, we consider carefully about add a kill command for pouch, and finally we think it's a good idea, because much time when we want to stop a container immediately, we can use kill command instead of stop command(which have default timeout 10s). On the other hand, docker already support kill command!
- Loading branch information
Showing
17 changed files
with
625 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// killDescription is used to describe kill command in detail and auto generate command doc. | ||
var killDescription = "Kill one or more running container objects in Pouchd. " + | ||
"You can kill a container using the container’s ID, ID-prefix, or name. " + | ||
"This is useful when you wish to kill a container which is running." | ||
|
||
// KillCommand use to implement 'kill' command, it kills a container. | ||
type KillCommand struct { | ||
baseCommand | ||
signal string | ||
} | ||
|
||
// Init initialize kill command. | ||
func (kc *KillCommand) Init(c *Cli) { | ||
kc.cli = c | ||
kc.cmd = &cobra.Command{ | ||
Use: "kill [OPTIONS] CONTAINER [CONTAINER...]", | ||
Short: "kill one or more running containers", | ||
Long: killDescription, | ||
Args: cobra.MinimumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return kc.runKill(args) | ||
}, | ||
Example: killExample(), | ||
} | ||
kc.addFlags() | ||
} | ||
|
||
// addFlags adds flags for specific command. | ||
func (kc *KillCommand) addFlags() { | ||
flagSet := kc.cmd.Flags() | ||
flagSet.StringVarP(&kc.signal, "signal", "s", "KILL", "Signal to send to the container (default \"KILL\")") | ||
} | ||
|
||
func (kc *KillCommand) runKill(args []string) error { | ||
ctx := context.Background() | ||
apiClient := kc.cli.Client() | ||
|
||
var errs []string | ||
for _, name := range args { | ||
if err := apiClient.ContainerKill(ctx, name, kc.signal); err != nil { | ||
errs = append(errs, err.Error()) | ||
continue | ||
} | ||
fmt.Printf("%s\n", name) | ||
} | ||
|
||
if len(errs) > 0 { | ||
return errors.New(strings.Join(errs, "\n")) | ||
} | ||
return nil | ||
} | ||
|
||
// killExample shows examples in kill command, and is used in auto-generated documentation. | ||
func killExample() string { | ||
return `$ pouch ps -a | ||
Name ID Status Created Image Runtime | ||
foo2 5a0ede Up 2 seconds 3 second ago registry.hub.docker.com/library/busybox:latest runc | ||
foo1 e05637 Up 6 seconds 7 seconds ago registry.hub.docker.com/library/busybox:latest runc | ||
$ pouch kill foo1 | ||
foo1 | ||
$ pouch ps | ||
Name ID Status Created Image Runtime | ||
foo2 5a0ede Up 11 seconds 12 seconds ago registry.hub.docker.com/library/busybox:latest runc` | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
) | ||
|
||
// ContainerKill kill a container. | ||
func (client *APIClient) ContainerKill(ctx context.Context, name string, signal string) error { | ||
q := url.Values{} | ||
q.Add("signal", signal) | ||
|
||
resp, err := client.post(ctx, "/containers/"+name+"/kill", q, nil, nil) | ||
ensureCloseReader(resp) | ||
|
||
return err | ||
} |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestContainerKillError(t *testing.T) { | ||
client := &APIClient{ | ||
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), | ||
} | ||
_, err := client.ContainerGet(context.Background(), "nothing") | ||
if err == nil || !strings.Contains(err.Error(), "Server error") { | ||
t.Fatalf("expected a Server Error, got %v", err) | ||
} | ||
} | ||
|
||
func TestContainerKill(t *testing.T) { | ||
expectedURL := "/containers/container_id/kill" | ||
|
||
httpClient := newMockClient(func(req *http.Request) (*http.Response, error) { | ||
if !strings.HasPrefix(req.URL.Path, expectedURL) { | ||
return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) | ||
} | ||
signal := req.URL.Query().Get("signal") | ||
if signal != "KILL" { | ||
return nil, fmt.Errorf("signal not set in URL query properly. Expected 'KILL', got %s", signal) | ||
} | ||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))), | ||
}, nil | ||
}) | ||
client := &APIClient{ | ||
HTTPCli: httpClient, | ||
} | ||
err := client.ContainerKill(context.Background(), "container_id", "KILL") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
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
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.