mirror of
https://github.com/k8snetworkplumbingwg/multus-cni.git
synced 2025-08-01 08:00:49 +00:00
Support IPRequest to specify IP address for interface
This commit is contained in:
parent
d1c8a3d93f
commit
f4068d18bd
@ -25,6 +25,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/containernetworking/cni/libcni"
|
"github.com/containernetworking/cni/libcni"
|
||||||
@ -161,7 +162,7 @@ func conflistDel(rt *libcni.RuntimeConf, rawnetconflist []byte, binDir string, e
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func delegateAdd(exec invoke.Exec, ifName string, delegate *types.DelegateNetConf, rt *libcni.RuntimeConf, binDir string) (cnitypes.Result, error) {
|
func delegateAdd(exec invoke.Exec, ifName string, delegate *types.DelegateNetConf, rt *libcni.RuntimeConf, binDir string, cniArgs string) (cnitypes.Result, error) {
|
||||||
logging.Debugf("delegateAdd: %v, %s, %v, %v, %s", exec, ifName, delegate, rt, binDir)
|
logging.Debugf("delegateAdd: %v, %s, %v, %v, %s", exec, ifName, delegate, rt, binDir)
|
||||||
if os.Setenv("CNI_IFNAME", ifName) != nil {
|
if os.Setenv("CNI_IFNAME", ifName) != nil {
|
||||||
return nil, logging.Errorf("Multus: error in setting CNI_IFNAME")
|
return nil, logging.Errorf("Multus: error in setting CNI_IFNAME")
|
||||||
@ -171,17 +172,40 @@ func delegateAdd(exec invoke.Exec, ifName string, delegate *types.DelegateNetCon
|
|||||||
return nil, logging.Errorf("cannot set %q ifname to %q: %v", delegate.Conf.Type, ifName, err)
|
return nil, logging.Errorf("cannot set %q ifname to %q: %v", delegate.Conf.Type, ifName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if delegate.MacRequest != "" {
|
if delegate.MacRequest != "" || delegate.IPRequest != "" {
|
||||||
// validate Mac address
|
if cniArgs != "" {
|
||||||
_, err := net.ParseMAC(delegate.MacRequest)
|
cniArgs = fmt.Sprintf("%s;IgnoreUnknown=true", cniArgs)
|
||||||
if err != nil {
|
} else {
|
||||||
return nil, logging.Errorf("failed to parse mac address %q", delegate.MacRequest)
|
cniArgs = "IgnoreUnknown=true"
|
||||||
|
}
|
||||||
|
if delegate.MacRequest != "" {
|
||||||
|
// validate Mac address
|
||||||
|
_, err := net.ParseMAC(delegate.MacRequest)
|
||||||
|
if err != nil {
|
||||||
|
return nil, logging.Errorf("failed to parse mac address %q", delegate.MacRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
cniArgs = fmt.Sprintf("%s;MAC=%s", cniArgs, delegate.MacRequest)
|
||||||
|
logging.Debugf("Set MAC address %q to %q", delegate.MacRequest, ifName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if os.Setenv("CNI_ARGS",fmt.Sprintf("%s;IgnoreUnknown=true;MAC=%s", os.Getenv("CNI_ARGS"), delegate.MacRequest)) != nil {
|
if delegate.IPRequest != "" {
|
||||||
return nil, logging.Errorf("cannot set %q mac to %q: %v", delegate.Conf.Type, delegate.MacRequest, err)
|
// validate IP address
|
||||||
|
if strings.Contains(delegate.IPRequest, "/") {
|
||||||
|
_, _, err := net.ParseCIDR(delegate.IPRequest)
|
||||||
|
if err != nil {
|
||||||
|
return nil, logging.Errorf("failed to parse CIDR %q", delegate.MacRequest)
|
||||||
|
}
|
||||||
|
} else if net.ParseIP(delegate.IPRequest) == nil {
|
||||||
|
return nil, logging.Errorf("failed to parse IP address %q", delegate.IPRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
cniArgs = fmt.Sprintf("%s;IP=%s", cniArgs, delegate.IPRequest)
|
||||||
|
logging.Debugf("Set IP address %q to %q", delegate.IPRequest, ifName)
|
||||||
|
}
|
||||||
|
if os.Setenv("CNI_ARGS", cniArgs) != nil {
|
||||||
|
return nil, logging.Errorf("cannot set %q mac to %q and ip to %q", delegate.Conf.Type, delegate.MacRequest, delegate.IPRequest)
|
||||||
}
|
}
|
||||||
logging.Debugf("Set MAC address %q to %q", delegate.MacRequest, ifName)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if delegate.ConfListPlugin != false {
|
if delegate.ConfListPlugin != false {
|
||||||
@ -287,11 +311,12 @@ func cmdAdd(args *skel.CmdArgs, exec invoke.Exec, kubeClient k8s.KubeClient) (cn
|
|||||||
var netStatus []*types.NetworkStatus
|
var netStatus []*types.NetworkStatus
|
||||||
var rt *libcni.RuntimeConf
|
var rt *libcni.RuntimeConf
|
||||||
lastIdx := 0
|
lastIdx := 0
|
||||||
|
cniArgs := os.Getenv("CNI_ARGS")
|
||||||
for idx, delegate := range n.Delegates {
|
for idx, delegate := range n.Delegates {
|
||||||
lastIdx = idx
|
lastIdx = idx
|
||||||
ifName := getIfname(delegate, args.IfName, idx)
|
ifName := getIfname(delegate, args.IfName, idx)
|
||||||
rt, _ = types.LoadCNIRuntimeConf(args, k8sArgs, ifName, n.RuntimeConfig)
|
rt, _ = types.LoadCNIRuntimeConf(args, k8sArgs, ifName, n.RuntimeConfig)
|
||||||
tmpResult, err = delegateAdd(exec, ifName, delegate, rt, n.BinDir)
|
tmpResult, err = delegateAdd(exec, ifName, delegate, rt, n.BinDir, cniArgs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -426,4 +451,4 @@ func main() {
|
|||||||
},
|
},
|
||||||
func(args *skel.CmdArgs) error { return cmdDel(args, nil, nil) },
|
func(args *skel.CmdArgs) error { return cmdDel(args, nil, nil) },
|
||||||
version.All, "meta-plugin that delegates to other CNI plugins")
|
version.All, "meta-plugin that delegates to other CNI plugins")
|
||||||
}
|
}
|
@ -44,12 +44,11 @@ func TestMultus(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type fakePlugin struct {
|
type fakePlugin struct {
|
||||||
expectedEnv []string
|
expectedEnv []string
|
||||||
expectedConf string
|
expectedConf string
|
||||||
expectedIfname string
|
expectedIfname string
|
||||||
expectedMacAddr string
|
result cnitypes.Result
|
||||||
result cnitypes.Result
|
err error
|
||||||
err error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type fakeExec struct {
|
type fakeExec struct {
|
||||||
@ -60,7 +59,7 @@ type fakeExec struct {
|
|||||||
plugins []*fakePlugin
|
plugins []*fakePlugin
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeExec) addPlugin(expectedEnv []string, expectedIfname, expectedMacAddr, expectedConf string, result *types020.Result, err error) {
|
func (f *fakeExec) addPlugin(expectedEnv []string, expectedIfname, expectedConf string, result *types020.Result, err error) {
|
||||||
f.plugins = append(f.plugins, &fakePlugin{
|
f.plugins = append(f.plugins, &fakePlugin{
|
||||||
expectedEnv: expectedEnv,
|
expectedEnv: expectedEnv,
|
||||||
expectedConf: expectedConf,
|
expectedConf: expectedConf,
|
||||||
@ -124,11 +123,12 @@ func (f *fakeExec) ExecPlugin(pluginPath string, stdinData []byte, environ []str
|
|||||||
if plugin.expectedIfname != "" {
|
if plugin.expectedIfname != "" {
|
||||||
Expect(os.Getenv("CNI_IFNAME")).To(Equal(plugin.expectedIfname))
|
Expect(os.Getenv("CNI_IFNAME")).To(Equal(plugin.expectedIfname))
|
||||||
}
|
}
|
||||||
if plugin.expectedMacAddr != "" {
|
|
||||||
Expect(os.Getenv("MAC")).To(Equal(plugin.expectedMacAddr))
|
|
||||||
}
|
|
||||||
if len(plugin.expectedEnv) > 0 {
|
if len(plugin.expectedEnv) > 0 {
|
||||||
matchArray(gatherCNIEnv(), plugin.expectedEnv)
|
cniEnv := gatherCNIEnv()
|
||||||
|
for _, expectedCniEnvVar := range plugin.expectedEnv {
|
||||||
|
Expect(cniEnv).Should(ContainElement(expectedCniEnvVar))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if plugin.err != nil {
|
if plugin.err != nil {
|
||||||
@ -207,7 +207,7 @@ var _ = Describe("multus operations", func() {
|
|||||||
"cniVersion": "0.2.0",
|
"cniVersion": "0.2.0",
|
||||||
"type": "weave-net"
|
"type": "weave-net"
|
||||||
}`
|
}`
|
||||||
fExec.addPlugin(nil, "eth0", "", expectedConf1, expectedResult1, nil)
|
fExec.addPlugin(nil, "eth0", expectedConf1, expectedResult1, nil)
|
||||||
|
|
||||||
expectedResult2 := &types020.Result{
|
expectedResult2 := &types020.Result{
|
||||||
CNIVersion: "0.2.0",
|
CNIVersion: "0.2.0",
|
||||||
@ -220,7 +220,7 @@ var _ = Describe("multus operations", func() {
|
|||||||
"cniVersion": "0.2.0",
|
"cniVersion": "0.2.0",
|
||||||
"type": "other-plugin"
|
"type": "other-plugin"
|
||||||
}`
|
}`
|
||||||
fExec.addPlugin(nil, "net1", "", expectedConf2, expectedResult2, nil)
|
fExec.addPlugin(nil, "net1", expectedConf2, expectedResult2, nil)
|
||||||
|
|
||||||
os.Setenv("CNI_COMMAND", "ADD")
|
os.Setenv("CNI_COMMAND", "ADD")
|
||||||
os.Setenv("CNI_IFNAME", "eth0")
|
os.Setenv("CNI_IFNAME", "eth0")
|
||||||
@ -245,11 +245,13 @@ var _ = Describe("multus operations", func() {
|
|||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
It("executes delegates with interface name and MAC addr", func() {
|
It("executes delegates with interface name and MAC and IP addr", func() {
|
||||||
podNet := `[{"name":"net1",
|
podNet := `[{"name":"net1",
|
||||||
"interface": "test1"},
|
"interface": "test1",
|
||||||
|
"ips":"1.2.3.4/24"},
|
||||||
{"name":"net2",
|
{"name":"net2",
|
||||||
"mac": "c2:11:22:33:44:66"}
|
"mac": "c2:11:22:33:44:66",
|
||||||
|
"ips": "10.0.0.1"}
|
||||||
]`
|
]`
|
||||||
fakePod := testhelpers.NewFakePod("testpod", podNet)
|
fakePod := testhelpers.NewFakePod("testpod", podNet)
|
||||||
net1 := `{
|
net1 := `{
|
||||||
@ -291,14 +293,14 @@ var _ = Describe("multus operations", func() {
|
|||||||
"cniVersion": "0.2.0",
|
"cniVersion": "0.2.0",
|
||||||
"type": "weave-net"
|
"type": "weave-net"
|
||||||
}`
|
}`
|
||||||
fExec.addPlugin(nil, "eth0", "", expectedConf1, expectedResult1, nil)
|
fExec.addPlugin(nil, "eth0", expectedConf1, expectedResult1, nil)
|
||||||
fExec.addPlugin(nil, "test1", "", net1, &types020.Result{
|
fExec.addPlugin([]string{"CNI_ARGS=IgnoreUnknown=true;IP=1.2.3.4/24"}, "test1", net1, &types020.Result{
|
||||||
CNIVersion: "0.2.0",
|
CNIVersion: "0.2.0",
|
||||||
IP4: &types020.IPConfig{
|
IP4: &types020.IPConfig{
|
||||||
IP: *testhelpers.EnsureCIDR("1.1.1.3/24"),
|
IP: *testhelpers.EnsureCIDR("1.1.1.3/24"),
|
||||||
},
|
},
|
||||||
}, nil)
|
}, nil)
|
||||||
fExec.addPlugin(nil, "net2", "c2:11:22:33:44:66", net2, &types020.Result{
|
fExec.addPlugin([]string{"CNI_ARGS=IgnoreUnknown=true;MAC=c2:11:22:33:44:66;IP=10.0.0.1"}, "net2", net2, &types020.Result{
|
||||||
CNIVersion: "0.2.0",
|
CNIVersion: "0.2.0",
|
||||||
IP4: &types020.IPConfig{
|
IP4: &types020.IPConfig{
|
||||||
IP: *testhelpers.EnsureCIDR("1.1.1.4/24"),
|
IP: *testhelpers.EnsureCIDR("1.1.1.4/24"),
|
||||||
@ -368,14 +370,14 @@ var _ = Describe("multus operations", func() {
|
|||||||
"cniVersion": "0.2.0",
|
"cniVersion": "0.2.0",
|
||||||
"type": "weave-net"
|
"type": "weave-net"
|
||||||
}`
|
}`
|
||||||
fExec.addPlugin(nil, "eth0", "", expectedConf1, expectedResult1, nil)
|
fExec.addPlugin(nil, "eth0", expectedConf1, expectedResult1, nil)
|
||||||
fExec.addPlugin(nil, "net1", "", net1, &types020.Result{
|
fExec.addPlugin(nil, "net1", net1, &types020.Result{
|
||||||
CNIVersion: "0.2.0",
|
CNIVersion: "0.2.0",
|
||||||
IP4: &types020.IPConfig{
|
IP4: &types020.IPConfig{
|
||||||
IP: *testhelpers.EnsureCIDR("1.1.1.3/24"),
|
IP: *testhelpers.EnsureCIDR("1.1.1.3/24"),
|
||||||
},
|
},
|
||||||
}, nil)
|
}, nil)
|
||||||
fExec.addPlugin(nil, "net2", "", net2, &types020.Result{
|
fExec.addPlugin(nil, "net2", net2, &types020.Result{
|
||||||
CNIVersion: "0.2.0",
|
CNIVersion: "0.2.0",
|
||||||
IP4: &types020.IPConfig{
|
IP4: &types020.IPConfig{
|
||||||
IP: *testhelpers.EnsureCIDR("1.1.1.4/24"),
|
IP: *testhelpers.EnsureCIDR("1.1.1.4/24"),
|
||||||
@ -439,7 +441,7 @@ var _ = Describe("multus operations", func() {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
}`
|
}`
|
||||||
fExec.addPlugin(nil, "eth0", "", expectedConf1, nil, nil)
|
fExec.addPlugin(nil, "eth0", expectedConf1, nil, nil)
|
||||||
os.Setenv("CNI_COMMAND", "ADD")
|
os.Setenv("CNI_COMMAND", "ADD")
|
||||||
os.Setenv("CNI_IFNAME", "eth0")
|
os.Setenv("CNI_IFNAME", "eth0")
|
||||||
_, err := cmdAdd(args, fExec, nil)
|
_, err := cmdAdd(args, fExec, nil)
|
||||||
|
@ -83,6 +83,9 @@ func LoadDelegateNetConf(bytes []byte, net *NetworkSelectionElement, deviceID st
|
|||||||
if net.MacRequest != "" {
|
if net.MacRequest != "" {
|
||||||
delegateConf.MacRequest = net.MacRequest
|
delegateConf.MacRequest = net.MacRequest
|
||||||
}
|
}
|
||||||
|
if net.IPRequest != "" {
|
||||||
|
delegateConf.IPRequest = net.IPRequest
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
delegateConf.Bytes = bytes
|
delegateConf.Bytes = bytes
|
||||||
|
@ -74,6 +74,7 @@ type DelegateNetConf struct {
|
|||||||
ConfList types.NetConfList
|
ConfList types.NetConfList
|
||||||
IfnameRequest string `json:"ifnameRequest,omitempty"`
|
IfnameRequest string `json:"ifnameRequest,omitempty"`
|
||||||
MacRequest string `json:"macRequest,omitempty"`
|
MacRequest string `json:"macRequest,omitempty"`
|
||||||
|
IPRequest string `json:"ipRequest,omitempty"`
|
||||||
// MasterPlugin is only used internal housekeeping
|
// MasterPlugin is only used internal housekeeping
|
||||||
MasterPlugin bool `json:"-"`
|
MasterPlugin bool `json:"-"`
|
||||||
// Conflist plugin is only used internal housekeeping
|
// Conflist plugin is only used internal housekeeping
|
||||||
|
Loading…
Reference in New Issue
Block a user