Make initial lease timeout configurable

This commit is contained in:
Jack O'Sullivan 2021-06-07 21:14:41 +01:00
parent 1bc910c60e
commit d99d903d1e
2 changed files with 39 additions and 12 deletions

View File

@ -4,10 +4,8 @@ import (
"context"
"fmt"
"net"
"time"
dTypes "github.com/docker/docker/api/types"
"github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
@ -27,8 +25,8 @@ const CLIOptionsKey string = "com.docker.network.generic"
func (p *Plugin) CreateNetwork(r CreateNetworkRequest) error {
log.WithField("options", r.Options).Debug("CreateNetwork options")
opts := DHCPNetworkOptions{}
if err := mapstructure.Decode(r.Options[util.OptionsKeyGeneric], &opts); err != nil {
opts, err := decodeOpts(r.Options[util.OptionsKeyGeneric])
if err != nil {
return fmt.Errorf("failed to decode network options: %w", err)
}
@ -111,15 +109,16 @@ func vethPairNames(id string) (string, string) {
}
func (p *Plugin) netOptions(ctx context.Context, id string) (DHCPNetworkOptions, error) {
opts := DHCPNetworkOptions{}
dummy := DHCPNetworkOptions{}
n, err := p.docker.NetworkInspect(ctx, id, dTypes.NetworkInspectOptions{})
if err != nil {
return opts, fmt.Errorf("failed to get info from Docker: %w", err)
return dummy, fmt.Errorf("failed to get info from Docker: %w", err)
}
if err := mapstructure.Decode(n.Options, &opts); err != nil {
return opts, fmt.Errorf("failed to parse options: %w", err)
opts, err := decodeOpts(n.Options)
if err != nil {
return dummy, fmt.Errorf("failed to parse options: %w", err)
}
return opts, nil
@ -188,9 +187,12 @@ func (p *Plugin) CreateEndpoint(ctx context.Context, r CreateEndpointRequest) (C
return fmt.Errorf("failed to attach host side link of veth peer to bridge: %w", err)
}
timeout := defaultLeaseTimeout
if opts.LeaseTimeout != 0 {
timeout = opts.LeaseTimeout
}
initialIP := func(v6 bool) error {
// TODO: Make this a config option
timeoutCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
timeoutCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
info, err := udhcpc.GetIP(timeoutCtx, ctrName, &udhcpc.DHCPClientOptions{V6: v6})

View File

@ -4,9 +4,11 @@ import (
"fmt"
"net"
"net/http"
"time"
docker "github.com/docker/docker/client"
"github.com/gorilla/handlers"
"github.com/mitchellh/mapstructure"
"github.com/devplayer0/docker-net-dhcp/pkg/util"
)
@ -14,10 +16,33 @@ import (
// DriverName is the name of the Docker Network Driver
const DriverName string = "net-dhcp"
const defaultLeaseTimeout = 10 * time.Second
// DHCPNetworkOptions contains options for the DHCP network driver
type DHCPNetworkOptions struct {
Bridge string
IPv6 bool
Bridge string
IPv6 bool
LeaseTimeout time.Duration `mapstructure:"lease_timeout"`
}
func decodeOpts(input interface{}) (DHCPNetworkOptions, error) {
var opts DHCPNetworkOptions
optsDecoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: &opts,
ErrorUnused: true,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
),
})
if err != nil {
return opts, fmt.Errorf("failed to create options decoder: %w", err)
}
if err := optsDecoder.Decode(input); err != nil {
return opts, err
}
return opts, nil
}
type gatewayHint struct {