mirror of
https://github.com/devplayer0/docker-net-dhcp
synced 2024-10-30 09:20:28 +00:00
Make initial lease timeout configurable
This commit is contained in:
parent
1bc910c60e
commit
d99d903d1e
@ -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})
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user