@@ -360,6 +360,7 @@ func (c *Controller) initProviderNetwork(pn *kubeovnv1.ProviderNetwork, node *v1
360360 fmt .Sprintf (util .ProviderNetworkInterfaceTemplate , pn .Name ): nil ,
361361 fmt .Sprintf (util .ProviderNetworkMtuTemplate , pn .Name ): nil ,
362362 fmt .Sprintf (util .ProviderNetworkExcludeTemplate , pn .Name ): nil ,
363+ fmt .Sprintf (util .ProviderNetworkVlanIntTemplate , pn .Name ): nil ,
363364 }
364365
365366 vlans := strset .NewWithSize (len (pn .Status .Vlans ) + 1 )
@@ -378,10 +379,52 @@ func (c *Controller) initProviderNetwork(pn *kubeovnv1.ProviderNetwork, node *v1
378379 // always add trunk 0 so that the ovs bridge can communicate with the external network
379380 vlans .Add ("0" )
380381
382+ // VLAN sub-interface handling - use map for efficiency
383+ vlanInterfaceMap := make (map [string ]int ) // interfaceName -> vlanID
384+
385+ // Process explicitly specified VLAN interfaces
386+ if len (pn .Spec .VlanInterfaces ) > 0 {
387+ klog .Infof ("Processing %d explicitly specified VLAN interfaces" , len (pn .Spec .VlanInterfaces ))
388+ for _ , vlanIfName := range pn .Spec .VlanInterfaces {
389+ if util .CheckInterfaceExists (vlanIfName ) {
390+ // Extract VLAN ID from interface name (e.g., "eth0.10" -> 10)
391+ vlanID , err := util .ExtractVlanIDFromInterface (vlanIfName )
392+ if err != nil {
393+ klog .Warningf ("Failed to extract VLAN ID from interface %s: %v" , vlanIfName , err )
394+ continue
395+ }
396+ vlanInterfaceMap [vlanIfName ] = vlanID
397+ vlans .Add (strconv .Itoa (vlanID ))
398+ klog .V (3 ).Infof ("Added explicit VLAN interface %s (VLAN ID %d)" , vlanIfName , vlanID )
399+ } else {
400+ klog .Warningf ("Explicitly specified VLAN interface %s does not exist, skipping" , vlanIfName )
401+ }
402+ }
403+ }
404+
405+ // Auto-detection of additional VLAN interfaces (if enabled)
406+ if pn .Spec .PreserveVlanInterfaces {
407+ klog .Infof ("Auto-detecting VLAN interfaces on %s" , nic )
408+ vlanIDs := util .DetectVlanInterfaces (nic )
409+ for _ , vlanID := range vlanIDs {
410+ vlanIfName := fmt .Sprintf ("%s.%d" , nic , vlanID )
411+ // Only add if not already explicitly specified
412+ if _ , exists := vlanInterfaceMap [vlanIfName ]; ! exists {
413+ vlanInterfaceMap [vlanIfName ] = vlanID
414+ vlans .Add (strconv .Itoa (vlanID ))
415+ klog .V (3 ).Infof ("Auto-detected VLAN interface %s (VLAN ID %d)" , vlanIfName , vlanID )
416+ } else {
417+ klog .V (3 ).Infof ("VLAN interface %s already explicitly specified, skipping auto-detection" , vlanIfName )
418+ }
419+ }
420+ klog .Infof ("Auto-detected %d additional VLAN interfaces for %s" , len (vlanIDs ), nic )
421+ }
422+
381423 var mtu int
382424 var err error
383425 klog .V (3 ).Infof ("ovs init provider network %s" , pn .Name )
384- if mtu , err = c .ovsInitProviderNetwork (pn .Name , nic , vlans .List (), pn .Spec .ExchangeLinkName , c .config .MacLearningFallback ); err != nil {
426+ // Configure main interface with ALL VLANs (including detected ones) in trunk
427+ if mtu , err = c .ovsInitProviderNetwork (pn .Name , nic , vlans .List (), pn .Spec .ExchangeLinkName , c .config .MacLearningFallback , vlanInterfaceMap ); err != nil {
385428 delete (patch , fmt .Sprintf (util .ProviderNetworkExcludeTemplate , pn .Name ))
386429 if err1 := util .PatchLabels (c .config .KubeClient .CoreV1 ().Nodes (), node .Name , patch ); err1 != nil {
387430 klog .Errorf ("failed to patch annotations of node %s: %v" , node .Name , err1 )
@@ -393,6 +436,9 @@ func (c *Controller) initProviderNetwork(pn *kubeovnv1.ProviderNetwork, node *v1
393436 patch [fmt .Sprintf (util .ProviderNetworkReadyTemplate , pn .Name )] = "true"
394437 patch [fmt .Sprintf (util .ProviderNetworkInterfaceTemplate , pn .Name )] = nic
395438 patch [fmt .Sprintf (util .ProviderNetworkMtuTemplate , pn .Name )] = strconv .Itoa (mtu )
439+ if len (vlanInterfaceMap ) > 0 {
440+ patch [fmt .Sprintf (util .ProviderNetworkVlanIntTemplate , pn .Name )] = "true"
441+ }
396442 if err = util .PatchLabels (c .config .KubeClient .CoreV1 ().Nodes (), node .Name , patch ); err != nil {
397443 klog .Errorf ("failed to patch labels of node %s: %v" , node .Name , err )
398444 return err
@@ -481,6 +527,7 @@ func (c *Controller) handleDeleteProviderNetwork(pn *kubeovnv1.ProviderNetwork)
481527 fmt .Sprintf (util .ProviderNetworkInterfaceTemplate , pn .Name ): nil ,
482528 fmt .Sprintf (util .ProviderNetworkMtuTemplate , pn .Name ): nil ,
483529 fmt .Sprintf (util .ProviderNetworkExcludeTemplate , pn .Name ): nil ,
530+ fmt .Sprintf (util .ProviderNetworkVlanIntTemplate , pn .Name ): nil ,
484531 }
485532 if err = util .PatchLabels (c .config .KubeClient .CoreV1 ().Nodes (), node .Name , patch ); err != nil {
486533 klog .Errorf ("failed to patch labels of node %s: %v" , node .Name , err )
0 commit comments