Skip to content

Commit

Permalink
underlay: set trunks of host nic port (#4282)
Browse files Browse the repository at this point in the history
Signed-off-by: zhangzujian <[email protected]>
  • Loading branch information
zhangzujian committed Jul 16, 2024
1 parent 79cf209 commit 69befe3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 10 deletions.
1 change: 1 addition & 0 deletions charts/kube-ovn/templates/ovn-CR.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ rules:
- "kubeovn.io"
resources:
- subnets
- vlans
- provider-networks
verbs:
- get
Expand Down
1 change: 1 addition & 0 deletions dist/images/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3063,6 +3063,7 @@ rules:
- "kubeovn.io"
resources:
- subnets
- vlans
- provider-networks
verbs:
- get
Expand Down
43 changes: 38 additions & 5 deletions pkg/daemon/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"time"

"github.com/scylladb/go-set/strset"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -46,8 +47,9 @@ type Controller struct {
ovnEipsLister kubeovnlister.OvnEipLister
ovnEipsSynced cache.InformerSynced

vlansLister kubeovnlister.VlanLister
vlanSynced cache.InformerSynced
vlansLister kubeovnlister.VlanLister
vlanSynced cache.InformerSynced
updateVlanQueue workqueue.RateLimitingInterface

podsLister listerv1.PodLister
podsSynced cache.InformerSynced
Expand Down Expand Up @@ -96,8 +98,9 @@ func NewController(config *Configuration, stopCh <-chan struct{}, podInformerFac
ovnEipsLister: ovnEipInformer.Lister(),
ovnEipsSynced: ovnEipInformer.Informer().HasSynced,

vlansLister: vlanInformer.Lister(),
vlanSynced: vlanInformer.Informer().HasSynced,
vlansLister: vlanInformer.Lister(),
vlanSynced: vlanInformer.Informer().HasSynced,
updateVlanQueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "UpdateVlan"),

podsLister: podInformer.Lister(),
podsSynced: podInformer.Informer().HasSynced,
Expand Down Expand Up @@ -137,6 +140,11 @@ func NewController(config *Configuration, stopCh <-chan struct{}, podInformerFac
}); err != nil {
return nil, err
}
if _, err = vlanInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: controller.enqueueUpdateVlan,
}); err != nil {
return nil, err
}
if _, err = subnetInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddSubnet,
UpdateFunc: controller.enqueueUpdateSubnet,
Expand Down Expand Up @@ -285,10 +293,26 @@ func (c *Controller) initProviderNetwork(pn *kubeovnv1.ProviderNetwork, node *v1
fmt.Sprintf(util.ProviderNetworkExcludeTemplate, pn.Name): nil,
}

vlans := strset.NewWithSize(len(pn.Status.Vlans) + 1)
for _, vlanName := range pn.Status.Vlans {
vlan, err := c.vlansLister.Get(vlanName)
if err != nil {
if k8serrors.IsNotFound(err) {
klog.Infof("vlan %s not found", vlanName)
continue
}
klog.Errorf("failed to get vlan %q: %v", vlanName, err)
return err
}
vlans.Add(strconv.Itoa(vlan.Spec.ID))
}
// always add trunk 0 so that the ovs bridge can communicate with the external network
vlans.Add("0")

var mtu int
var err error
klog.V(3).Infof("ovs init provider network %s", pn.Name)
if mtu, err = c.ovsInitProviderNetwork(pn.Name, nic, pn.Spec.ExchangeLinkName, c.config.MacLearningFallback); err != nil {
if mtu, err = c.ovsInitProviderNetwork(pn.Name, nic, vlans.List(), pn.Spec.ExchangeLinkName, c.config.MacLearningFallback); err != nil {
delete(labels, fmt.Sprintf(util.ProviderNetworkExcludeTemplate, pn.Name))
if err1 := util.UpdateNodeLabels(c.config.KubeClient.CoreV1().Nodes(), node.Name, labels); err1 != nil {
klog.Errorf("failed to update annotations of node %s: %v", node.Name, err1)
Expand Down Expand Up @@ -404,6 +428,15 @@ func (c *Controller) handleDeleteProviderNetwork(pn *kubeovnv1.ProviderNetwork)
return nil
}

func (c *Controller) enqueueUpdateVlan(oldObj, newObj interface{}) {
oldVlan := oldObj.(*kubeovnv1.Vlan)
newVlan := newObj.(*kubeovnv1.Vlan)
if oldVlan.Spec.ID != newVlan.Spec.ID {
klog.V(3).Infof("enqueue update provider network %q", newVlan.Spec.Provider)
c.addOrUpdateProviderNetworkQueue.Add(newVlan.Spec.Provider)
}
}

type subnetEvent struct {
oldObj, newObj interface{}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/daemon/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func InitMirror(config *Configuration) error {
return configureEmptyMirror(config.MirrorNic, config.MTU)
}

func (c *Controller) ovsInitProviderNetwork(provider, nic string, exchangeLinkName, macLearningFallback bool) (int, error) {
func (c *Controller) ovsInitProviderNetwork(provider, nic string, trunks []string, exchangeLinkName, macLearningFallback bool) (int, error) {
// create and configure external bridge
brName := util.ExternalBridgeName(provider)
if exchangeLinkName {
Expand Down Expand Up @@ -122,7 +122,7 @@ func (c *Controller) ovsInitProviderNetwork(provider, nic string, exchangeLinkNa

// add host nic to the external bridge
klog.Infof("config provider nic %s on bridge %s", nic, brName)
mtu, err := c.configProviderNic(nic, brName)
mtu, err := c.configProviderNic(nic, brName, trunks)
if err != nil {
errMsg := fmt.Errorf("failed to add nic %s to external bridge %s: %v", nic, brName, err)
klog.Error(errMsg)
Expand Down
4 changes: 2 additions & 2 deletions pkg/daemon/ovs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ func (c *Controller) transferAddrsAndRoutes(nicName, brName string, delNonExiste

// Add host nic to external bridge
// Mac address, MTU, IP addresses & routes will be copied/transferred to the external bridge
func (c *Controller) configProviderNic(nicName, brName string) (int, error) {
func (c *Controller) configProviderNic(nicName, brName string, trunks []string) (int, error) {
sysctlDisableIPv6 := fmt.Sprintf("net.ipv6.conf.%s.disable_ipv6", brName)
disableIPv6, err := sysctl.Sysctl(sysctlDisableIPv6)
if err != nil {
Expand All @@ -1155,7 +1155,7 @@ func (c *Controller) configProviderNic(nicName, brName string) (int, error) {
}

if _, err = ovs.Exec(ovs.MayExist, "add-port", brName, nicName,
"--", "set", "port", nicName, "external_ids:vendor="+util.CniTypeName); err != nil {
"--", "set", "port", nicName, "trunks="+strings.Join(trunks, ","), "external_ids:vendor="+util.CniTypeName); err != nil {
return 0, fmt.Errorf("failed to add %s to OVS bridge %s: %v", nicName, brName, err)
}
klog.V(3).Infof("ovs port %s has been added to bridge %s", nicName, brName)
Expand Down
2 changes: 1 addition & 1 deletion pkg/daemon/ovs_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func configureMirrorLink(portName string, mtu int) error {
return nil
}

func (c *Controller) configProviderNic(nicName, brName string) (int, error) {
func (c *Controller) configProviderNic(nicName, brName string, trunks []string) (int, error) {
// nothing to do on Windows
return 0, nil
}
Expand Down

0 comments on commit 69befe3

Please sign in to comment.