@@ -330,11 +330,9 @@ func getSubnetsByFilter(is *InstanceService, opts *subnets.ListOpts) ([]subnets.
330330}
331331
332332func getOrCreatePort (is * InstanceService , name string , portOpts openstackconfigv1.PortOpts ) (* ports.Port , error ) {
333- var portName string
333+ portName := name
334334 if portOpts .NameSuffix != "" {
335335 portName = name + "-" + portOpts .NameSuffix
336- } else {
337- portName = name
338336 }
339337 if len (portName ) > PortNameMaxSize {
340338 portName = portName [len (portName )- PortNameMaxSize :]
@@ -459,6 +457,54 @@ func GetSecurityGroups(is *InstanceService, sg_param []openstackconfigv1.Securit
459457 return sgIDs , nil
460458}
461459
460+ func trunkSupportNeeded (config * openstackconfigv1.OpenstackProviderSpec ) bool {
461+ if config .Trunk == true {
462+ return true
463+ }
464+ for _ , portCreateOpts := range config .Ports {
465+ if portCreateOpts .Trunk != nil && * portCreateOpts .Trunk == true {
466+ return true
467+ }
468+ }
469+ return false
470+ }
471+
472+ func getOrCreateTrunk (is * InstanceService , port * ports.Port , machineTags []string ) (* trunks.Trunk , error ) {
473+ allPages , err := trunks .List (is .networkClient , trunks.ListOpts {
474+ Name : port .Name ,
475+ PortID : port .ID ,
476+ }).AllPages ()
477+ if err != nil {
478+ return nil , fmt .Errorf ("Searching for existing trunk for port %q err: %v" , port .ID , err )
479+ }
480+ trunkList , err := trunks .ExtractTrunks (allPages )
481+ if err != nil {
482+ return nil , fmt .Errorf ("Searching for existing trunk for port %q err: %v" , port .ID , err )
483+ }
484+ var trunk trunks.Trunk
485+ if len (trunkList ) == 0 {
486+ // create trunk with the previous port as parent
487+ trunkCreateOpts := trunks.CreateOpts {
488+ Name : port .Name ,
489+ PortID : port .ID ,
490+ }
491+ newTrunk , err := trunks .Create (is .networkClient , trunkCreateOpts ).Extract ()
492+ if err != nil {
493+ return nil , fmt .Errorf ("Create trunk for port %q err: %v" , port .ID , err )
494+ }
495+ trunk = * newTrunk
496+ } else {
497+ trunk = trunkList [0 ]
498+ }
499+
500+ _ , err = attributestags .ReplaceAll (is .networkClient , "trunks" , trunk .ID , attributestags.ReplaceAllOpts {
501+ Tags : machineTags }).Extract ()
502+ if err != nil {
503+ return nil , fmt .Errorf ("Tagging trunk for port %q err: %v" , port .ID , err )
504+ }
505+ return & trunk , nil
506+ }
507+
462508// InstanceCreate creates a compute instance.
463509// If ServerGroupName is nonempty and no server group exists with that name,
464510// then InstanceCreate creates a server group with that name.
@@ -477,7 +523,7 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, clust
477523 if config == nil {
478524 return nil , fmt .Errorf ("create Options need be specified to create instace" )
479525 }
480- if config . Trunk == true {
526+ if trunkSupportNeeded ( config ) == true {
481527 trunkSupport , err := GetTrunkSupport (is )
482528 if err != nil {
483529 return nil , fmt .Errorf ("There was an issue verifying whether trunk support is available, please disable it: %v" , err )
@@ -607,42 +653,17 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, clust
607653 })
608654
609655 if config .Trunk == true {
610- allPages , err := trunks .List (is .networkClient , trunks.ListOpts {
611- Name : name ,
612- PortID : port .ID ,
613- }).AllPages ()
656+ _ , err := getOrCreateTrunk (is , port , machineTags )
614657 if err != nil {
615- return nil , fmt .Errorf ("Searching for existing trunk for server err: %v" , err )
616- }
617- trunkList , err := trunks .ExtractTrunks (allPages )
618- if err != nil {
619- return nil , fmt .Errorf ("Searching for existing trunk for server err: %v" , err )
620- }
621- var trunk trunks.Trunk
622- if len (trunkList ) == 0 {
623- // create trunk with the previous port as parent
624- trunkCreateOpts := trunks.CreateOpts {
625- Name : name ,
626- PortID : port .ID ,
627- }
628- newTrunk , err := trunks .Create (is .networkClient , trunkCreateOpts ).Extract ()
629- if err != nil {
630- return nil , fmt .Errorf ("Create trunk for server err: %v" , err )
631- }
632- trunk = * newTrunk
633- } else {
634- trunk = trunkList [0 ]
635- }
636-
637- _ , err = attributestags .ReplaceAll (is .networkClient , "trunks" , trunk .ID , attributestags.ReplaceAllOpts {
638- Tags : machineTags }).Extract ()
639- if err != nil {
640- return nil , fmt .Errorf ("Tagging trunk for server err: %v" , err )
658+ return nil , err
641659 }
642660 }
643661 }
644662
645663 for _ , portCreateOpts := range config .Ports {
664+ if portCreateOpts .Trunk == nil {
665+ portCreateOpts .Trunk = & config .Trunk
666+ }
646667 port , err := getOrCreatePort (is , name , portCreateOpts )
647668 if err != nil {
648669 return nil , err
@@ -658,6 +679,13 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, clust
658679 portsList = append (portsList , servers.Network {
659680 Port : port .ID ,
660681 })
682+
683+ if * portCreateOpts .Trunk == true {
684+ _ , err := getOrCreateTrunk (is , port , machineTags )
685+ if err != nil {
686+ return nil , err
687+ }
688+ }
661689 }
662690
663691 if len (portsList ) == 0 {
0 commit comments