From 9604565b2296ac704aaba8b2bbf2345b71969ff5 Mon Sep 17 00:00:00 2001 From: Tomofumi Hayashi Date: Tue, 23 Jan 2018 12:59:03 +0900 Subject: [PATCH] Add -hostprefix in DHCP daemon to run the daemon as container This diff adds -hostprefix option in dhcp daemon. This option could be used to run dhcp daemon as container because container cannot touch host's netns directly. The diff changes dhcp daemon to touch procfs mounted to another path, like '/hostfs/proc'. --- plugins/ipam/dhcp/README.md | 1 + plugins/ipam/dhcp/daemon.go | 11 +++++++---- plugins/ipam/dhcp/main.go | 4 +++- 3 files changed, 11 insertions(+), 5 deletions(-) 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) }