diff --git a/examples/multus-ptp-portmap.conf b/examples/multus-ptp-portmap.conf new file mode 100644 index 000000000..d092c7c53 --- /dev/null +++ b/examples/multus-ptp-portmap.conf @@ -0,0 +1,36 @@ +{ + "name": "multus-cni-network", + "type": "multus" + "capabilities": { + "portMappings": true + }, + "delegates": [ + { + "cniVersion": "0.3.1", + "name": "ptp-tuning-conflist", + "plugins": [ + { + "dns": { + "nameservers": [ + "172.16.1.1" + ] + }, + "ipMasq": true, + "ipam": { + "subnet": "172.16.0.0/24", + "type": "host-local" + }, + "mtu": 512, + "type": "ptp" + }, + { + "capabilities": { + "portMappings": true + }, + "externalSetMarkChain": "KUBE-MARK-MASQ", + "type": "portmap" + } + ] + } + ], +} diff --git a/multus/multus.go b/multus/multus.go index 8abcd6970..916959a6a 100644 --- a/multus/multus.go +++ b/multus/multus.go @@ -122,11 +122,12 @@ func validateIfName(nsname string, ifname string) error { return err } -func conflistAdd(rt *libcni.RuntimeConf, rawnetconflist []byte, binDir string) (cnitypes.Result, error) { +func conflistAdd(rt *libcni.RuntimeConf, rawnetconflist []byte, binDir string, exec invoke.Exec) (cnitypes.Result, error) { logging.Debugf("conflistAdd: %v, %s, %s", rt, string(rawnetconflist), binDir) // In part, adapted from K8s pkg/kubelet/dockershim/network/cni/cni.go - binDirs := []string{binDir} - cniNet := libcni.CNIConfig{Path: binDirs} + binDirs := filepath.SplitList(os.Getenv("CNI_PATH")) + binDirs = append(binDirs, binDir) + cniNet := libcni.NewCNIConfig(binDirs, exec) confList, err := libcni.ConfListFromBytes(rawnetconflist) if err != nil { @@ -208,7 +209,7 @@ func delegateAdd(exec invoke.Exec, ifName string, delegate *types.DelegateNetCon } if delegate.ConfListPlugin != false { - result, err := conflistAdd(rt, delegate.Bytes, binDir) + result, err := conflistAdd(rt, delegate.Bytes, binDir, exec) if err != nil { return nil, logging.Errorf("Multus: error in invoke Conflist add - %q: %v", delegate.ConfList.Name, err) } @@ -435,7 +436,7 @@ func cmdDel(args *skel.CmdArgs, exec invoke.Exec, kubeClient k8s.KubeClient) err } } - rt, _ := types.LoadCNIRuntimeConf(args, k8sArgs, "") + rt, _ := types.LoadCNIRuntimeConf(args, k8sArgs, "", in.RuntimeConfig) return delPlugins(exec, args.IfName, in.Delegates, len(in.Delegates)-1, rt, in.BinDir) } diff --git a/types/conf.go b/types/conf.go index 20b1d6818..790cbe606 100644 --- a/types/conf.go +++ b/types/conf.go @@ -93,9 +93,9 @@ func LoadDelegateNetConf(bytes []byte, net *NetworkSelectionElement, deviceID st return delegateConf, nil } -func LoadCNIRuntimeConf(args *skel.CmdArgs, k8sArgs *K8sArgs, ifName string) (*libcni.RuntimeConf, error) { +func LoadCNIRuntimeConf(args *skel.CmdArgs, k8sArgs *K8sArgs, ifName string, rc *RuntimeConfig) (*libcni.RuntimeConf, error) { - logging.Debugf("LoadCNIRuntimeConf: %v, %v, %s", args, k8sArgs, ifName) + logging.Debugf("LoadCNIRuntimeConf: %v, %v, %s, %v", args, k8sArgs, ifName, rc) // In part, adapted from K8s pkg/kubelet/dockershim/network/cni/cni.go#buildCNIRuntimeConf // Todo // ingress, egress and bandwidth capability features as same as kubelet. @@ -110,6 +110,12 @@ func LoadCNIRuntimeConf(args *skel.CmdArgs, k8sArgs *K8sArgs, ifName string) (*l {"K8S_POD_INFRA_CONTAINER_ID", string(k8sArgs.K8S_POD_INFRA_CONTAINER_ID)}, }, } + + if rc != nil { + rt.CapabilityArgs = map[string]interface{}{ + "portMappings": rc.PortMaps, + } + } return rt, nil } diff --git a/types/conf_test.go b/types/conf_test.go index b6775eef1..47f6c68cb 100644 --- a/types/conf_test.go +++ b/types/conf_test.go @@ -35,13 +35,20 @@ var _ = Describe("config operations", func() { "kubeconfig": "/etc/kubernetes/node-kubeconfig.yaml", "delegates": [{ "type": "weave-net" - }] + }], + "runtimeConfig": { + "portMappings": [ + {"hostPort": 8080, "containerPort": 80, "protocol": "tcp"} + ] + } + }` netConf, err := LoadNetConf([]byte(conf)) Expect(err).NotTo(HaveOccurred()) Expect(len(netConf.Delegates)).To(Equal(1)) Expect(netConf.Delegates[0].Conf.Type).To(Equal("weave-net")) Expect(netConf.Delegates[0].MasterPlugin).To(BeTrue()) + Expect(len(netConf.RuntimeConfig.PortMaps)).To(Equal(1)) }) It("succeeds if only delegates are set", func() { diff --git a/types/types.go b/types/types.go index 10bcb1b9d..0d3d3d6f0 100644 --- a/types/types.go +++ b/types/types.go @@ -51,6 +51,17 @@ type NetConf struct { NamespaceIsolation bool `json:"namespaceIsolation"` } +type RuntimeConfig struct { + PortMaps []PortMapEntry `json:"portMappings,omitempty"` +} + +type PortMapEntry struct { + HostPort int `json:"hostPort"` + ContainerPort int `json:"containerPort"` + Protocol string `json:"protocol"` + HostIP string `json:"hostIP,omitempty"` +} + type NetworkStatus struct { Name string `json:"name"` Interface string `json:"interface,omitempty"`