Skip to content

Commit 83ca993

Browse files
Add HTTP client timeout.
Signed-off-by: Anusha Ragunathan <[email protected]>
1 parent 0596301 commit 83ca993

16 files changed

Lines changed: 96 additions & 23 deletions

File tree

api/server/router/plugin/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// Backend for Plugin
1212
type Backend interface {
1313
Disable(name string) error
14-
Enable(name string) error
14+
Enable(name string, config *enginetypes.PluginEnableConfig) error
1515
List() ([]enginetypes.Plugin, error)
1616
Inspect(name string) (enginetypes.Plugin, error)
1717
Remove(name string, config *enginetypes.PluginRmConfig) error

api/server/router/plugin/plugin_routes.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/base64"
55
"encoding/json"
66
"net/http"
7+
"strconv"
78
"strings"
89

910
"github.com/docker/docker/api/server/httputils"
@@ -56,7 +57,18 @@ func (pr *pluginRouter) createPlugin(ctx context.Context, w http.ResponseWriter,
5657
}
5758

5859
func (pr *pluginRouter) enablePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
59-
return pr.backend.Enable(vars["name"])
60+
if err := httputils.ParseForm(r); err != nil {
61+
return err
62+
}
63+
64+
name := vars["name"]
65+
timeout, err := strconv.Atoi(r.Form.Get("timeout"))
66+
if err != nil {
67+
return err
68+
}
69+
config := &types.PluginEnableConfig{Timeout: timeout}
70+
71+
return pr.backend.Enable(name, config)
6072
}
6173

6274
func (pr *pluginRouter) disablePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

api/swagger.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6307,7 +6307,7 @@ paths:
63076307
summary: "Install a plugin"
63086308
operationId: "PostPluginsPull"
63096309
description: |
6310-
Pulls and installs a plugin. After the plugin is installed, it can be enabled using the [`POST /plugins/{name}/enable` endpoint](#operation/PostPluginEnable).
6310+
Pulls and installs a plugin. After the plugin is installed, it can be enabled using the [`POST /plugins/{name}/enable` endpoint](#operation/PostPluginsEnable).
63116311
produces:
63126312
- "application/json"
63136313
responses:
@@ -6430,6 +6430,11 @@ paths:
64306430
description: "The name of the plugin. The `:latest` tag is optional, and is the default if omitted."
64316431
required: true
64326432
type: "string"
6433+
- name: "timeout"
6434+
in: "query"
6435+
description: "Set the HTTP client timeout (in seconds)"
6436+
type: "integer"
6437+
default: 0
64336438
tags:
64346439
- "Plugins"
64356440
/plugins/{name}/disable:

api/types/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ type PluginRemoveOptions struct {
332332
Force bool
333333
}
334334

335+
// PluginEnableOptions holds parameters to enable plugins.
336+
type PluginEnableOptions struct {
337+
Timeout int
338+
}
339+
335340
// PluginInstallOptions holds parameters to install a plugin.
336341
type PluginInstallOptions struct {
337342
Disabled bool

api/types/configs.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ type ExecConfig struct {
5959
type PluginRmConfig struct {
6060
ForceRemove bool
6161
}
62+
63+
// PluginEnableConfig holds arguments for the plugin enable
64+
type PluginEnableConfig struct {
65+
Timeout int
66+
}

cli/command/plugin/enable.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,40 @@ package plugin
33
import (
44
"fmt"
55

6+
"github.com/docker/docker/api/types"
67
"github.com/docker/docker/cli"
78
"github.com/docker/docker/cli/command"
89
"github.com/docker/docker/reference"
910
"github.com/spf13/cobra"
1011
"golang.org/x/net/context"
1112
)
1213

14+
type enableOpts struct {
15+
timeout int
16+
name string
17+
}
18+
1319
func newEnableCommand(dockerCli *command.DockerCli) *cobra.Command {
20+
var opts enableOpts
21+
1422
cmd := &cobra.Command{
1523
Use: "enable PLUGIN",
1624
Short: "Enable a plugin",
1725
Args: cli.ExactArgs(1),
1826
RunE: func(cmd *cobra.Command, args []string) error {
19-
return runEnable(dockerCli, args[0])
27+
opts.name = args[0]
28+
return runEnable(dockerCli, &opts)
2029
},
2130
}
2231

32+
flags := cmd.Flags()
33+
flags.IntVar(&opts.timeout, "timeout", 0, "HTTP client timeout (in seconds)")
2334
return cmd
2435
}
2536

26-
func runEnable(dockerCli *command.DockerCli, name string) error {
37+
func runEnable(dockerCli *command.DockerCli, opts *enableOpts) error {
38+
name := opts.name
39+
2740
named, err := reference.ParseNamed(name) // FIXME: validate
2841
if err != nil {
2942
return err
@@ -35,7 +48,11 @@ func runEnable(dockerCli *command.DockerCli, name string) error {
3548
if !ok {
3649
return fmt.Errorf("invalid name: %s", named.String())
3750
}
38-
if err := dockerCli.Client().PluginEnable(context.Background(), ref.String()); err != nil {
51+
if opts.timeout < 0 {
52+
return fmt.Errorf("negative timeout %d is invalid", opts.timeout)
53+
}
54+
55+
if err := dockerCli.Client().PluginEnable(context.Background(), ref.String(), types.PluginEnableOptions{Timeout: opts.timeout}); err != nil {
3956
return err
4057
}
4158
fmt.Fprintln(dockerCli.Out(), name)

client/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ type NodeAPIClient interface {
109109
type PluginAPIClient interface {
110110
PluginList(ctx context.Context) (types.PluginsListResponse, error)
111111
PluginRemove(ctx context.Context, name string, options types.PluginRemoveOptions) error
112-
PluginEnable(ctx context.Context, name string) error
112+
PluginEnable(ctx context.Context, name string, options types.PluginEnableOptions) error
113113
PluginDisable(ctx context.Context, name string) error
114114
PluginInstall(ctx context.Context, name string, options types.PluginInstallOptions) error
115115
PluginPush(ctx context.Context, name string, registryAuth string) error

client/plugin_enable.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package client
22

33
import (
4+
"net/url"
5+
"strconv"
6+
7+
"github.com/docker/docker/api/types"
48
"golang.org/x/net/context"
59
)
610

711
// PluginEnable enables a plugin
8-
func (cli *Client) PluginEnable(ctx context.Context, name string) error {
9-
resp, err := cli.post(ctx, "/plugins/"+name+"/enable", nil, nil, nil)
12+
func (cli *Client) PluginEnable(ctx context.Context, name string, options types.PluginEnableOptions) error {
13+
query := url.Values{}
14+
query.Set("timeout", strconv.Itoa(options.Timeout))
15+
16+
resp, err := cli.post(ctx, "/plugins/"+name+"/enable", query, nil, nil)
1017
ensureReaderClosed(resp)
1118
return err
1219
}

client/plugin_enable_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"testing"
1010

11+
"github.com/docker/docker/api/types"
1112
"golang.org/x/net/context"
1213
)
1314

@@ -16,7 +17,7 @@ func TestPluginEnableError(t *testing.T) {
1617
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
1718
}
1819

19-
err := client.PluginEnable(context.Background(), "plugin_name")
20+
err := client.PluginEnable(context.Background(), "plugin_name", types.PluginEnableOptions{})
2021
if err == nil || err.Error() != "Error response from daemon: Server error" {
2122
t.Fatalf("expected a Server Error, got %v", err)
2223
}
@@ -40,7 +41,7 @@ func TestPluginEnable(t *testing.T) {
4041
}),
4142
}
4243

43-
err := client.PluginEnable(context.Background(), "plugin_name")
44+
err := client.PluginEnable(context.Background(), "plugin_name", types.PluginEnableOptions{})
4445
if err != nil {
4546
t.Fatal(err)
4647
}

client/plugin_install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (cli *Client) PluginInstall(ctx context.Context, name string, options types
6262
return nil
6363
}
6464

65-
return cli.PluginEnable(ctx, name)
65+
return cli.PluginEnable(ctx, name, types.PluginEnableOptions{Timeout: 0})
6666
}
6767

6868
func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, registryAuth string) (serverResponse, error) {

0 commit comments

Comments
 (0)