-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add macvtap-cni production code The ConfigureMacvtap interface code is located in the `netlink` util pkg. The following helper functions are also made available on the `netlink` pkg. - configureArp - renameMacvtapIface All the configuration is performed within a single netns.Do instruction. Interface renaming is performed in the target netns, thus reducing the risk of interface name collision. Signed-off-by: Miguel Duarte Barroso <[email protected]> * Use init containers to install the cni plugin The init-container installs the macvtap-cni plugin into the host, and afterwards, the macvtap container listens to device plugin requests, and operates on them. This way, we can achieve macvtap cni & dp co-existence within the same container. Signed-off-by: Miguel Duarte Barroso <[email protected]> * Add unit test to check the correct behavior of the CNI plugin Signed-off-by: Miguel Duarte Barroso <[email protected]> * workaround: remove flag breaking unit tests The -logtostderr flag is somehow preventing the cni unit tests from running. Can't figure this one out; this is just to show I'm not insane. Signed-off-by: Miguel Duarte Barroso <[email protected]>
- Loading branch information
Showing
71 changed files
with
13,988 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/containernetworking/cni/pkg/skel" | ||
"github.com/containernetworking/cni/pkg/version" | ||
bv "github.com/containernetworking/plugins/pkg/utils/buildversion" | ||
macvtap_cni "github.com/kubevirt/macvtap-cni/pkg/cni" | ||
) | ||
|
||
func main() { | ||
skel.PluginMain(macvtap_cni.CmdAdd, macvtap_cni.CmdCheck, macvtap_cni.CmdDel, version.All, bv.BuildString("macvtap")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cni_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestCni(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Cni Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright 2019 CNI authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cni | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net" | ||
"runtime" | ||
|
||
"github.com/kubevirt/macvtap-cni/pkg/util" | ||
|
||
"github.com/containernetworking/cni/pkg/skel" | ||
"github.com/containernetworking/cni/pkg/types" | ||
"github.com/containernetworking/cni/pkg/types/current" | ||
|
||
"github.com/containernetworking/plugins/pkg/ip" | ||
"github.com/containernetworking/plugins/pkg/ns" | ||
) | ||
|
||
// A NetConf structure represents a Multus network attachment definition configuration | ||
type NetConf struct { | ||
types.NetConf | ||
DeviceID string `json:"deviceID"` | ||
MTU int `json:"mtu,omitempty"` | ||
} | ||
|
||
// EnvArgs structure represents inputs sent from each VMI via environment variables | ||
type EnvArgs struct { | ||
types.CommonArgs | ||
MAC types.UnmarshallableString `json:"mac,omitempty"` | ||
} | ||
|
||
func init() { | ||
// this ensures that main runs only on main thread (thread group leader). | ||
// since namespace ops (unshare, setns) are done for a single thread, we | ||
// must ensure that the goroutine does not jump from OS thread to thread | ||
runtime.LockOSThread() | ||
} | ||
|
||
func loadConf(bytes []byte) (NetConf, string, error) { | ||
n := NetConf{} | ||
if err := json.Unmarshal(bytes, &n); err != nil { | ||
return n, "", fmt.Errorf("failed to load netconf: %v", err) | ||
} | ||
|
||
return n, n.CNIVersion, nil | ||
} | ||
|
||
func getEnvArgs(envArgsString string) (EnvArgs, error) { | ||
e := EnvArgs{} | ||
err := types.LoadArgs(envArgsString, &e) | ||
if err != nil { | ||
return e, err | ||
} | ||
return e, nil | ||
} | ||
|
||
// CmdAdd - CNI interface | ||
func CmdAdd(args *skel.CmdArgs) error { | ||
var err error | ||
netConf, cniVersion, err := loadConf(args.StdinData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
envArgs, err := getEnvArgs(args.Args) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var mac *net.HardwareAddr = nil | ||
if envArgs.MAC != "" { | ||
aMac, err := net.ParseMAC(string(envArgs.MAC)) | ||
mac = &aMac | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
netns, err := ns.GetNS(args.Netns) | ||
if err != nil { | ||
return fmt.Errorf("failed to open netns %q: %v", netns, err) | ||
} | ||
|
||
// Delete link if err to avoid link leak in this ns | ||
defer func() { | ||
netns.Close() | ||
if err != nil { | ||
util.LinkDelete(netConf.DeviceID) | ||
} | ||
}() | ||
|
||
macvtapInterface, err := util.ConfigureInterface(netConf.DeviceID, args.IfName, mac, netConf.MTU, netns) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
result := ¤t.Result{ | ||
CNIVersion: cniVersion, | ||
Interfaces: []*current.Interface{macvtapInterface}, | ||
} | ||
|
||
return types.PrintResult(result, cniVersion) | ||
} | ||
|
||
// CmdDel - CNI plugin Interface | ||
func CmdDel(args *skel.CmdArgs) error { | ||
if args.Netns == "" { | ||
return nil | ||
} | ||
|
||
// There is a netns so try to clean up. Delete can be called multiple times | ||
// so don't return an error if the device is already removed. | ||
err := ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error { | ||
|
||
if err := ip.DelLinkByName(args.IfName); err != nil { | ||
if err != ip.ErrLinkNotFound { | ||
return err | ||
} | ||
} | ||
return nil | ||
}) | ||
|
||
return err | ||
} | ||
|
||
// CmdCheck - CNI plugin Interface | ||
func CmdCheck(args *skel.CmdArgs) error { | ||
return nil | ||
} |
Oops, something went wrong.