Merge branch 'master' of github.com:containernetworking/plugins into issue164

This commit is contained in:
Michael Cambria
2018-10-10 12:28:43 -04:00
9 changed files with 622 additions and 31 deletions

View File

@ -127,7 +127,7 @@ func (d *DHCP) clearLease(contID, netName string) {
delete(d.leases, contID+netName)
}
func getListener() (net.Listener, error) {
func getListener(socketPath string) (net.Listener, error) {
l, err := activation.Listeners()
if err != nil {
return nil, err
@ -151,7 +151,7 @@ func getListener() (net.Listener, error) {
}
}
func runDaemon(pidfilePath string, hostPrefix string) error {
func runDaemon(pidfilePath string, hostPrefix string, socketPath string) error {
// since other goroutines (on separate threads) will change namespaces,
// ensure the RPC server does not get scheduled onto those
runtime.LockOSThread()
@ -166,7 +166,7 @@ func runDaemon(pidfilePath string, hostPrefix string) error {
}
}
l, err := getListener()
l, err := getListener(socketPath)
if err != nil {
return fmt.Errorf("Error getting listener: %v", err)
}

View File

@ -16,9 +16,11 @@ package main
import (
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
@ -38,6 +40,15 @@ import (
. "github.com/onsi/gomega"
)
func getTmpDir() (string, error) {
tmpDir, err := ioutil.TempDir(cniDirPrefix, "dhcp")
if err == nil {
tmpDir = filepath.ToSlash(tmpDir)
}
return tmpDir, err
}
func dhcpServerStart(netns ns.NetNS, leaseIP, serverIP net.IP, stopCh <-chan bool) (*sync.WaitGroup, error) {
// Add the expected IP to the pool
lp := memorypool.MemoryPool{}
@ -96,17 +107,12 @@ func dhcpServerStart(netns ns.NetNS, leaseIP, serverIP net.IP, stopCh <-chan boo
const (
hostVethName string = "dhcp0"
contVethName string = "eth0"
pidfilePath string = "/var/run/cni/dhcp-client.pid"
cniDirPrefix string = "/var/run/cni"
)
var _ = BeforeSuite(func() {
os.Remove(socketPath)
os.Remove(pidfilePath)
})
var _ = AfterSuite(func() {
os.Remove(socketPath)
os.Remove(pidfilePath)
err := os.MkdirAll(cniDirPrefix, 0700)
Expect(err).NotTo(HaveOccurred())
})
var _ = Describe("DHCP Operations", func() {
@ -114,10 +120,17 @@ var _ = Describe("DHCP Operations", func() {
var dhcpServerStopCh chan bool
var dhcpServerDone *sync.WaitGroup
var clientCmd *exec.Cmd
var socketPath string
var tmpDir string
var err error
BeforeEach(func() {
dhcpServerStopCh = make(chan bool)
tmpDir, err = getTmpDir()
Expect(err).NotTo(HaveOccurred())
socketPath = filepath.Join(tmpDir, "dhcp.sock")
// Create a new NetNS so we don't modify the host
var err error
originalNS, err = testutils.NewNS()
@ -184,10 +197,9 @@ var _ = Describe("DHCP Operations", func() {
Expect(err).NotTo(HaveOccurred())
// Start the DHCP client daemon
os.MkdirAll(pidfilePath, 0755)
dhcpPluginPath, err := exec.LookPath("dhcp")
Expect(err).NotTo(HaveOccurred())
clientCmd = exec.Command(dhcpPluginPath, "daemon")
clientCmd = exec.Command(dhcpPluginPath, "daemon", "-socketpath", socketPath)
err = clientCmd.Start()
Expect(err).NotTo(HaveOccurred())
Expect(clientCmd.Process).NotTo(BeNil())
@ -207,19 +219,19 @@ var _ = Describe("DHCP Operations", func() {
Expect(originalNS.Close()).To(Succeed())
Expect(targetNS.Close()).To(Succeed())
os.Remove(socketPath)
os.Remove(pidfilePath)
defer os.RemoveAll(tmpDir)
})
It("configures and deconfigures a link with ADD/DEL", func() {
conf := `{
conf := fmt.Sprintf(`{
"cniVersion": "0.3.1",
"name": "mynet",
"type": "ipvlan",
"ipam": {
"type": "dhcp"
"type": "dhcp",
"daemonSocketPath": "%s"
}
}`
}`, socketPath)
args := &skel.CmdArgs{
ContainerID: "dummy",
@ -254,14 +266,15 @@ var _ = Describe("DHCP Operations", func() {
})
It("correctly handles multiple DELs for the same container", func() {
conf := `{
conf := fmt.Sprintf(`{
"cniVersion": "0.3.1",
"name": "mynet",
"type": "ipvlan",
"ipam": {
"type": "dhcp"
"type": "dhcp",
"daemonSocketPath": "%s"
}
}`
}`, socketPath)
args := &skel.CmdArgs{
ContainerID: "dummy",

View File

@ -15,6 +15,7 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
@ -28,18 +29,24 @@ import (
"github.com/containernetworking/cni/pkg/version"
)
const socketPath = "/run/cni/dhcp.sock"
const defaultSocketPath = "/run/cni/dhcp.sock"
func main() {
if len(os.Args) > 1 && os.Args[1] == "daemon" {
var pidfilePath string
var hostPrefix string
var socketPath 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.StringVar(&socketPath, "socketpath", "", "optional dhcp server socketpath")
daemonFlags.Parse(os.Args[2:])
if err := runDaemon(pidfilePath, hostPrefix); err != nil {
if socketPath == "" {
socketPath = defaultSocketPath
}
if err := runDaemon(pidfilePath, hostPrefix, socketPath); err != nil {
log.Printf(err.Error())
os.Exit(1)
}
@ -78,7 +85,31 @@ func cmdGet(args *skel.CmdArgs) error {
return fmt.Errorf("not implemented")
}
type SocketPathConf struct {
DaemonSocketPath string `json:"daemonSocketPath,omitempty"`
}
type TempNetConf struct {
IPAM SocketPathConf `json:"ipam,omitempty"`
}
func getSocketPath(stdinData []byte) (string, error) {
conf := TempNetConf{}
if err := json.Unmarshal(stdinData, &conf); err != nil {
return "", fmt.Errorf("error parsing socket path conf: %v", err)
}
if conf.IPAM.DaemonSocketPath == "" {
return defaultSocketPath, nil
}
return conf.IPAM.DaemonSocketPath, nil
}
func rpcCall(method string, args *skel.CmdArgs, result interface{}) error {
socketPath, err := getSocketPath(args.StdinData)
if err != nil {
return fmt.Errorf("error obtaining socketPath: %v", err)
}
client, err := rpc.DialHTTP("unix", socketPath)
if err != nil {
return fmt.Errorf("error dialing DHCP daemon: %v", err)