Skip to content

Commit

Permalink
feat(ocnet-cni): container vpc network
Browse files Browse the repository at this point in the history
  • Loading branch information
zexi committed May 10, 2024
1 parent f136664 commit 9258dfa
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
11 changes: 11 additions & 0 deletions pkg/cni-plugin/plugin/ovs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package plugin

import (
"context"
"fmt"
"os/exec"
"time"

"yunion.io/x/pkg/errors"
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions pkg/cni-plugin/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 25 additions & 14 deletions pkg/cni-plugin/plugin/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions pkg/cni-plugin/plugin/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 9258dfa

Please sign in to comment.