Skip to content

Commit

Permalink
Allow Trunk configuration at a Port level.
Browse files Browse the repository at this point in the history
Signed-off-by: Anwar Hassen <[email protected]>
  • Loading branch information
Anwar Hassen committed Jul 12, 2021
1 parent a2acda3 commit 731f1da
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
2 changes: 2 additions & 0 deletions api/v1alpha4/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ type PortOpts struct {
ProjectID string `json:"projectId,omitempty"`
SecurityGroups *[]string `json:"securityGroups,omitempty"`
AllowedAddressPairs []AddressPair `json:"allowedAddressPairs,omitempty"`
// Enables and disables trunk support at port level
Trunk bool `json:"trunk,omitempty"`

// The ID of the host where the port is allocated
HostID string `json:"hostId,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,10 @@ spec:
type: array
tenantId:
type: string
trunk:
description: Enables and disables trunk support at port
level
type: boolean
vnicType:
description: The virtual network interface card (vNIC)
type that is bound to the neutron port.
Expand Down Expand Up @@ -1730,6 +1734,10 @@ spec:
type: array
tenantId:
type: string
trunk:
description: Enables and disables trunk support at port
level
type: boolean
vnicType:
description: The virtual network interface card (vNIC)
type that is bound to the neutron port.
Expand Down Expand Up @@ -1994,6 +2002,9 @@ spec:
type: array
tenantId:
type: string
trunk:
description: Enables and disables trunk support at port level
type: boolean
vnicType:
description: The virtual network interface card (vNIC) type
that is bound to the neutron port.
Expand Down Expand Up @@ -2138,6 +2149,9 @@ spec:
type: array
tenantId:
type: string
trunk:
description: Enables and disables trunk support at port level
type: boolean
vnicType:
description: The virtual network interface card (vNIC) type
that is bound to the neutron port.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,9 @@ spec:
type: array
tenantId:
type: string
trunk:
description: Enables and disables trunk support at port level
type: boolean
vnicType:
description: The virtual network interface card (vNIC) type
that is bound to the neutron port.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,10 @@ spec:
type: array
tenantId:
type: string
trunk:
description: Enables and disables trunk support at port
level
type: boolean
vnicType:
description: The virtual network interface card (vNIC)
type that is bound to the neutron port.
Expand Down
45 changes: 28 additions & 17 deletions pkg/cloud/services/compute/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ func (s *Service) constructNetworks(openStackCluster *infrav1.OpenStackCluster,
Subnet: &infrav1.Subnet{
ID: openStackCluster.Status.Network.Subnet.ID,
},
PortOpts: &infrav1.PortOpts{
Trunk: openStackMachine.Spec.Trunk,
},
}}
}
return &nets, nil
Expand All @@ -188,23 +191,17 @@ func (s *Service) createInstance(eventObject runtime.Object, clusterName string,
return nil, fmt.Errorf("no network was found or provided. Please check your machine configuration and try again")
}

iTrunk := instance.Trunk
iTags := []string{}
if len(instance.Tags) > 0 {
iTags = instance.Tags
}
portName := getPortName(instance.Name, network.PortOpts, i)
port, err := s.getOrCreatePort(eventObject, clusterName, portName, network, instance.SecurityGroups)
port, err := s.getOrCreatePort(eventObject, clusterName, portName, network, instance.SecurityGroups, iTrunk, iTags)
if err != nil {
return nil, err
}

if instance.Trunk {
trunk, err := s.getOrCreateTrunk(eventObject, clusterName, instance.Name, port.ID)
if err != nil {
return nil, err
}

if err = s.replaceAllAttributesTags(eventObject, trunk.ID, instance.Tags); err != nil {
return nil, err
}
}

for _, fip := range port.FixedIPs {
if fip.SubnetID == instance.Subnet {
accessIPv4 = fip.IPAddress
Expand Down Expand Up @@ -421,7 +418,7 @@ func (s *Service) getServerNetworks(networkParams []infrav1.NetworkParam) ([]inf
return nets, nil
}

func (s *Service) getOrCreatePort(eventObject runtime.Object, clusterName string, portName string, net infrav1.Network, instanceSecurityGroups *[]string) (*ports.Port, error) {
func (s *Service) getOrCreatePort(eventObject runtime.Object, clusterName string, portName string, net infrav1.Network, instanceSecurityGroups *[]string, iTrunk bool, tags []string) (*ports.Port, error) {
allPages, err := ports.List(s.networkClient, ports.ListOpts{
Name: portName,
NetworkID: net.ID,
Expand Down Expand Up @@ -505,20 +502,34 @@ func (s *Service) getOrCreatePort(eventObject runtime.Object, clusterName string
}

record.Eventf(eventObject, "SuccessfulCreatePort", "Created port %s with id %s", port.Name, port.ID)

if iTrunk {
if portOpts.Trunk {
trunkDescription := names.GetDescription(clusterName)
trunk, err := s.getOrCreateTrunk(eventObject, trunkDescription, port.Name, port.ID)
if err != nil {
return nil, err
}
if err = s.replaceAllAttributesTags(eventObject, trunk.ID, tags); err != nil {
return nil, err
}
}
}

return port, nil
}

func (s *Service) getOrCreateTrunk(eventObject runtime.Object, clusterName, trunkName, portID string) (*trunks.Trunk, error) {
func (s *Service) getOrCreateTrunk(eventObject runtime.Object, description, trunkName, portID string) (*trunks.Trunk, error) {
allPages, err := trunks.List(s.networkClient, trunks.ListOpts{
Name: trunkName,
PortID: portID,
}).AllPages()
if err != nil {
return nil, fmt.Errorf("searching for existing trunk for server: %v", err)
return nil, fmt.Errorf("searching for existing trunk for port: %v", err)
}
trunkList, err := trunks.ExtractTrunks(allPages)
if err != nil {
return nil, fmt.Errorf("searching for existing trunk for server: %v", err)
return nil, fmt.Errorf("searching for existing trunk for port: %v", err)
}

if len(trunkList) != 0 {
Expand All @@ -528,7 +539,7 @@ func (s *Service) getOrCreateTrunk(eventObject runtime.Object, clusterName, trun
trunkCreateOpts := trunks.CreateOpts{
Name: trunkName,
PortID: portID,
Description: names.GetDescription(clusterName),
Description: description,
}
mc := metrics.NewMetricPrometheusContext("trunk", "create")

Expand Down

0 comments on commit 731f1da

Please sign in to comment.