From 9258dfa5fab2962bf76463be767c0c7e19491ff8 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Fri, 10 May 2024 12:32:18 +0800 Subject: [PATCH] feat(ocnet-cni): container vpc network --- pkg/cni-plugin/plugin/ovs.go | 11 ++++++++++ pkg/cni-plugin/plugin/plugin.go | 1 + pkg/cni-plugin/plugin/types.go | 39 +++++++++++++++++++++------------ pkg/cni-plugin/plugin/utils.go | 5 +++++ 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/pkg/cni-plugin/plugin/ovs.go b/pkg/cni-plugin/plugin/ovs.go index 3d73da1ad..28ffd4fe2 100644 --- a/pkg/cni-plugin/plugin/ovs.go +++ b/pkg/cni-plugin/plugin/ovs.go @@ -2,6 +2,8 @@ package plugin import ( "context" + "fmt" + "os/exec" "time" "yunion.io/x/pkg/errors" @@ -12,6 +14,7 @@ import ( type OVSClient interface { AddPort(bridge, port string) error DeletePort(bridge, port string) error + SetIfaceId(id string, name string) error } func NewOVSClient() (OVSClient, error) { @@ -41,6 +44,14 @@ func (sw *ovsClient) AddPort(bridge, port string) error { })) } +func (sw *ovsClient) SetIfaceId(netId string, ifName string) error { + ovsCmd := exec.Command("ovs-vsctl", "set", "Interface", ifName, fmt.Sprintf("external-ids:iface-id=iface-%s-%s", netId, ifName)) + if out, err := ovsCmd.CombinedOutput(); err != nil { + return errors.Wrapf(err, "set external ids: %s", out) + } + return nil +} + func (sw *ovsClient) DeletePort(bridge, port string) error { return sw.agentCli.W(sw.agentCli.VSwitch.DelBridgePort(newTimeoutCtx(), &pb.DelBridgePortRequest{ Bridge: bridge, diff --git a/pkg/cni-plugin/plugin/plugin.go b/pkg/cni-plugin/plugin/plugin.go index e91c65ed6..7a894e793 100644 --- a/pkg/cni-plugin/plugin/plugin.go +++ b/pkg/cni-plugin/plugin/plugin.go @@ -83,6 +83,7 @@ func cmdAdd(args *skel.CmdArgs) error { if err != nil { return errors.Wrap(err, "GenerateNetworkResultByNics") } + result.CNIVersion = cniVersion for idx, nic := range nics { defaultGw := false diff --git a/pkg/cni-plugin/plugin/types.go b/pkg/cni-plugin/plugin/types.go index e91696340..738b60ecf 100644 --- a/pkg/cni-plugin/plugin/types.go +++ b/pkg/cni-plugin/plugin/types.go @@ -9,21 +9,32 @@ type PodDesc struct { Nics []PodNic `json:"nics"` } +const ( + POD_NIC_PROVIDER_OVN = "ovn" +) + +type PodNicVpc struct { + Id string `json:"id"` + MappedIpAddr string `json:"mapped_ip_addr"` + Provider string `json:"provider"` +} + type PodNic struct { - Index int `json:"index"` - Bridge string `json:"bridge"` - Ifname string `json:"ifname"` - Interface string `json:"interface"` - Ip string `json:"ip"` - Mac string `json:"mac"` - Gateway string `json:"gateway"` - Bandwidth int `json:"bw"` - Dns string `json:"dns"` - Mtu int `json:"mtu"` - Masklen int `json:"masklen,omitempty"` - Domain string `json:"domain,omitempty"` - NetId string `json:"net_id"` - WireId string `json:"wire_id"` + Index int `json:"index"` + Bridge string `json:"bridge"` + Ifname string `json:"ifname"` + Interface string `json:"interface"` + Ip string `json:"ip"` + Mac string `json:"mac"` + Gateway string `json:"gateway"` + Bandwidth int `json:"bw"` + Dns string `json:"dns"` + Mtu int `json:"mtu"` + Masklen int `json:"masklen,omitempty"` + Domain string `json:"domain,omitempty"` + NetId string `json:"net_id"` + WireId string `json:"wire_id"` + Vpc *PodNicVpc `json:"vpc,omitempty"` } func (n PodNic) GetInterface(idx int) string { diff --git a/pkg/cni-plugin/plugin/utils.go b/pkg/cni-plugin/plugin/utils.go index d1521bb33..369e0be82 100644 --- a/pkg/cni-plugin/plugin/utils.go +++ b/pkg/cni-plugin/plugin/utils.go @@ -253,6 +253,11 @@ func setupVeth( if err := cli.AddPort(nic.Bridge, hostIf.Name); err != nil { return nil, nil, errors.Wrapf(err, "Add port to OVS: %s -> %s", hostIf.Name, nic.Bridge) } + if nic.Vpc != nil && nic.Vpc.Provider == POD_NIC_PROVIDER_OVN { + if err := cli.SetIfaceId(nic.NetId, hostIf.Name); err != nil { + return nil, nil, errors.Wrapf(err, "Set interface id: %s -> %s", hostIf.Name, nic.Bridge) + } + } //log.Infof("Port %q added to %q", hostIf.Name, nic.Bridge) return hostIf, ctrIf, nil }