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'.
This commit is contained in:
parent
d228f980e1
commit
9604565b22
@ -18,6 +18,7 @@ $ ./dhcp daemon
|
|||||||
|
|
||||||
If given `-pidfile <path>` arguments after 'daemon', the dhcp plugin will write
|
If given `-pidfile <path>` arguments after 'daemon', the dhcp plugin will write
|
||||||
its PID to the given file.
|
its PID to the given file.
|
||||||
|
If given `-hostprefix <prefix>` arguments after 'daemon', the dhcp plugin will use this prefix for netns as `<prefix>/<original netns>`. It could be used in case of running dhcp daemon as container.
|
||||||
|
|
||||||
Alternatively, you can use systemd socket activation protocol.
|
Alternatively, you can use systemd socket activation protocol.
|
||||||
Be sure that the .socket file uses /run/cni/dhcp.sock as the socket path.
|
Be sure that the .socket file uses /run/cni/dhcp.sock as the socket path.
|
||||||
|
@ -39,8 +39,9 @@ const resendCount = 3
|
|||||||
var errNoMoreTries = errors.New("no more tries")
|
var errNoMoreTries = errors.New("no more tries")
|
||||||
|
|
||||||
type DHCP struct {
|
type DHCP struct {
|
||||||
mux sync.Mutex
|
mux sync.Mutex
|
||||||
leases map[string]*DHCPLease
|
leases map[string]*DHCPLease
|
||||||
|
hostNetnsPrefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDHCP() *DHCP {
|
func newDHCP() *DHCP {
|
||||||
@ -58,7 +59,8 @@ func (d *DHCP) Allocate(args *skel.CmdArgs, result *current.Result) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
clientID := args.ContainerID + "/" + conf.Name
|
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 {
|
if err != nil {
|
||||||
return err
|
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,
|
// since other goroutines (on separate threads) will change namespaces,
|
||||||
// ensure the RPC server does not get scheduled onto those
|
// ensure the RPC server does not get scheduled onto those
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
@ -161,6 +163,7 @@ func runDaemon(pidfilePath string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dhcp := newDHCP()
|
dhcp := newDHCP()
|
||||||
|
dhcp.hostNetnsPrefix = hostPrefix
|
||||||
rpc.Register(dhcp)
|
rpc.Register(dhcp)
|
||||||
rpc.HandleHTTP()
|
rpc.HandleHTTP()
|
||||||
http.Serve(l, nil)
|
http.Serve(l, nil)
|
||||||
|
@ -33,11 +33,13 @@ const socketPath = "/run/cni/dhcp.sock"
|
|||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) > 1 && os.Args[1] == "daemon" {
|
if len(os.Args) > 1 && os.Args[1] == "daemon" {
|
||||||
var pidfilePath string
|
var pidfilePath string
|
||||||
|
var hostPrefix string
|
||||||
daemonFlags := flag.NewFlagSet("daemon", flag.ExitOnError)
|
daemonFlags := flag.NewFlagSet("daemon", flag.ExitOnError)
|
||||||
daemonFlags.StringVar(&pidfilePath, "pidfile", "", "optional path to write daemon PID to")
|
daemonFlags.StringVar(&pidfilePath, "pidfile", "", "optional path to write daemon PID to")
|
||||||
|
daemonFlags.StringVar(&hostPrefix, "hostprefix", "", "optional prefix to netns")
|
||||||
daemonFlags.Parse(os.Args[2:])
|
daemonFlags.Parse(os.Args[2:])
|
||||||
|
|
||||||
if err := runDaemon(pidfilePath); err != nil {
|
if err := runDaemon(pidfilePath, hostPrefix); err != nil {
|
||||||
log.Printf(err.Error())
|
log.Printf(err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user