diff --git a/cmd/net-dhcp/main.go b/cmd/net-dhcp/main.go index ac3d445..bfd4eaf 100644 --- a/cmd/net-dhcp/main.go +++ b/cmd/net-dhcp/main.go @@ -4,6 +4,7 @@ import ( "flag" "os" "os/signal" + "time" log "github.com/sirupsen/logrus" "golang.org/x/sys/unix" @@ -42,7 +43,15 @@ func main() { log.StandardLogger().Out = f } - p, err := plugin.NewPlugin() + awaitTimeout := 5 * time.Second + if t, ok := os.LookupEnv("AWAIT_TIMEOUT"); ok { + awaitTimeout, err = time.ParseDuration(t) + if err != nil { + log.WithError(err).Fatal("Failed to parse await timeout") + } + } + + p, err := plugin.NewPlugin(awaitTimeout) if err != nil { log.WithError(err).Fatal("Failed to create plugin") } diff --git a/config.json b/config.json index 4453e85..e65a740 100644 --- a/config.json +++ b/config.json @@ -15,6 +15,14 @@ "settable": [ "value" ] + }, + { + "description": "Log level", + "name": "AWAIT_TIMEOUT", + "value": "10s", + "settable": [ + "value" + ] } ], "workdir": "/", diff --git a/pkg/plugin/network.go b/pkg/plugin/network.go index c8841f9..0d7dac2 100644 --- a/pkg/plugin/network.go +++ b/pkg/plugin/network.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net" - "time" dTypes "github.com/docker/docker/api/types" "github.com/mitchellh/mapstructure" @@ -441,8 +440,7 @@ func (p *Plugin) Join(ctx context.Context, r JoinRequest) (JoinResponse, error) } go func() { - // TODO: Make timeout configurable? - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), p.awaitTimeout) defer cancel() m := newDHCPManager(p.docker, r, opts) diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go index 0bf26e2..3e17c8c 100644 --- a/pkg/plugin/plugin.go +++ b/pkg/plugin/plugin.go @@ -55,6 +55,8 @@ type joinHint struct { // Plugin is the DHCP network plugin type Plugin struct { + awaitTimeout time.Duration + docker *docker.Client server http.Server @@ -63,13 +65,15 @@ type Plugin struct { } // NewPlugin creates a new Plugin -func NewPlugin() (*Plugin, error) { +func NewPlugin(awaitTimeout time.Duration) (*Plugin, error) { client, err := docker.NewClient("unix:///run/docker.sock", "v1.13.1", nil, nil) if err != nil { return nil, fmt.Errorf("failed to create docker client: %w", err) } p := Plugin{ + awaitTimeout: awaitTimeout, + docker: client, joinHints: make(map[string]joinHint),