diff --git a/plugins/ipam/dhcp/README.md b/plugins/ipam/dhcp/README.md index 55e5396c..0ec4c4cb 100644 --- a/plugins/ipam/dhcp/README.md +++ b/plugins/ipam/dhcp/README.md @@ -18,6 +18,7 @@ $ ./dhcp daemon If given `-pidfile ` arguments after 'daemon', the dhcp plugin will write its PID to the given file. +If given `-hostprefix ` arguments after 'daemon', the dhcp plugin will use this prefix for netns as `/`. It could be used in case of running dhcp daemon as container. Alternatively, you can use systemd socket activation protocol. Be sure that the .socket file uses /run/cni/dhcp.sock as the socket path. diff --git a/plugins/ipam/dhcp/daemon.go b/plugins/ipam/dhcp/daemon.go index 7aa84208..a5316d75 100644 --- a/plugins/ipam/dhcp/daemon.go +++ b/plugins/ipam/dhcp/daemon.go @@ -39,8 +39,9 @@ const resendCount = 3 var errNoMoreTries = errors.New("no more tries") type DHCP struct { - mux sync.Mutex - leases map[string]*DHCPLease + mux sync.Mutex + leases map[string]*DHCPLease + hostNetnsPrefix string } func newDHCP() *DHCP { @@ -58,7 +59,8 @@ func (d *DHCP) Allocate(args *skel.CmdArgs, result *current.Result) error { } clientID := args.ContainerID + "/" + conf.Name - l, err := AcquireLease(clientID, args.Netns, args.IfName) + hostNetns := d.hostNetnsPrefix + args.Netns + l, err := AcquireLease(clientID, hostNetns, args.IfName) if err != nil { return err } @@ -140,7 +142,7 @@ func getListener() (net.Listener, error) { } } -func runDaemon(pidfilePath string) error { +func runDaemon(pidfilePath string, hostPrefix string) error { // since other goroutines (on separate threads) will change namespaces, // ensure the RPC server does not get scheduled onto those runtime.LockOSThread() @@ -161,6 +163,7 @@ func runDaemon(pidfilePath string) error { } dhcp := newDHCP() + dhcp.hostNetnsPrefix = hostPrefix rpc.Register(dhcp) rpc.HandleHTTP() http.Serve(l, nil) diff --git a/plugins/ipam/dhcp/main.go b/plugins/ipam/dhcp/main.go index 2e55c27a..73ba318a 100644 --- a/plugins/ipam/dhcp/main.go +++ b/plugins/ipam/dhcp/main.go @@ -33,11 +33,13 @@ const socketPath = "/run/cni/dhcp.sock" func main() { if len(os.Args) > 1 && os.Args[1] == "daemon" { var pidfilePath string + var hostPrefix string daemonFlags := flag.NewFlagSet("daemon", flag.ExitOnError) daemonFlags.StringVar(&pidfilePath, "pidfile", "", "optional path to write daemon PID to") + daemonFlags.StringVar(&hostPrefix, "hostprefix", "", "optional prefix to netns") daemonFlags.Parse(os.Args[2:]) - if err := runDaemon(pidfilePath); err != nil { + if err := runDaemon(pidfilePath, hostPrefix); err != nil { log.Printf(err.Error()) os.Exit(1) }