Skip to content

Commit 1fd76a5

Browse files
authored
Merge pull request #934 from Nordix/toggle_trunk
✨ Allow Trunk configuration at a Port level
2 parents 4e178f0 + 8f3bf57 commit 1fd76a5

7 files changed

+67
-19
lines changed

api/v1alpha4/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ type PortOpts struct {
130130
ProjectID string `json:"projectId,omitempty"`
131131
SecurityGroups *[]string `json:"securityGroups,omitempty"`
132132
AllowedAddressPairs []AddressPair `json:"allowedAddressPairs,omitempty"`
133+
// Enables and disables trunk at port level. If not provided, openStackMachine.Spec.Trunk is inherited.
134+
Trunk *bool `json:"trunk,omitempty"`
133135

134136
// The ID of the host where the port is allocated
135137
HostID string `json:"hostId,omitempty"`

api/v1alpha4/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclusters.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,10 @@ spec:
13221322
type: array
13231323
tenantId:
13241324
type: string
1325+
trunk:
1326+
description: Enables and disables trunk at port level.
1327+
If not provided, openStackMachine.Spec.Trunk is inherited.
1328+
type: boolean
13251329
vnicType:
13261330
description: The virtual network interface card (vNIC)
13271331
type that is bound to the neutron port.
@@ -1771,6 +1775,10 @@ spec:
17711775
type: array
17721776
tenantId:
17731777
type: string
1778+
trunk:
1779+
description: Enables and disables trunk at port level.
1780+
If not provided, openStackMachine.Spec.Trunk is inherited.
1781+
type: boolean
17741782
vnicType:
17751783
description: The virtual network interface card (vNIC)
17761784
type that is bound to the neutron port.
@@ -2047,6 +2055,10 @@ spec:
20472055
type: array
20482056
tenantId:
20492057
type: string
2058+
trunk:
2059+
description: Enables and disables trunk at port level. If
2060+
not provided, openStackMachine.Spec.Trunk is inherited.
2061+
type: boolean
20502062
vnicType:
20512063
description: The virtual network interface card (vNIC) type
20522064
that is bound to the neutron port.
@@ -2235,6 +2247,10 @@ spec:
22352247
type: array
22362248
tenantId:
22372249
type: string
2250+
trunk:
2251+
description: Enables and disables trunk at port level. If
2252+
not provided, openStackMachine.Spec.Trunk is inherited.
2253+
type: boolean
22382254
vnicType:
22392255
description: The virtual network interface card (vNIC) type
22402256
that is bound to the neutron port.

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclustertemplates.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ spec:
304304
type: array
305305
tenantId:
306306
type: string
307+
trunk:
308+
description: Enables and disables trunk at port
309+
level. If not provided, openStackMachine.Spec.Trunk
310+
is inherited.
311+
type: boolean
307312
vnicType:
308313
description: The virtual network interface card
309314
(vNIC) type that is bound to the neutron port.

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachines.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,10 @@ spec:
611611
type: array
612612
tenantId:
613613
type: string
614+
trunk:
615+
description: Enables and disables trunk at port level. If not
616+
provided, openStackMachine.Spec.Trunk is inherited.
617+
type: boolean
614618
vnicType:
615619
description: The virtual network interface card (vNIC) type
616620
that is bound to the neutron port.

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachinetemplates.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,10 @@ spec:
561561
type: array
562562
tenantId:
563563
type: string
564+
trunk:
565+
description: Enables and disables trunk at port level.
566+
If not provided, openStackMachine.Spec.Trunk is inherited.
567+
type: boolean
564568
vnicType:
565569
description: The virtual network interface card (vNIC)
566570
type that is bound to the neutron port.

pkg/cloud/services/compute/instance.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,24 @@ func (s *Service) constructNetworks(openStackCluster *infrav1.OpenStackCluster,
145145
}
146146
}
147147
for i, port := range openStackMachine.Spec.Ports {
148+
pOpts := &openStackMachine.Spec.Ports[i]
149+
// No Trunk field specified for the port, inherit openStackMachine.Spec.Trunk.
150+
if pOpts.Trunk == nil {
151+
pOpts.Trunk = &openStackMachine.Spec.Trunk
152+
}
148153
if port.NetworkID != "" {
149154
nets = append(nets, infrav1.Network{
150155
ID: port.NetworkID,
151156
Subnet: &infrav1.Subnet{},
152-
PortOpts: &openStackMachine.Spec.Ports[i],
157+
PortOpts: pOpts,
153158
})
154159
} else {
155160
nets = append(nets, infrav1.Network{
156161
ID: openStackCluster.Status.Network.ID,
157162
Subnet: &infrav1.Subnet{
158163
ID: openStackCluster.Status.Network.Subnet.ID,
159164
},
160-
PortOpts: &openStackMachine.Spec.Ports[i],
165+
PortOpts: pOpts,
161166
})
162167
}
163168
}
@@ -168,6 +173,9 @@ func (s *Service) constructNetworks(openStackCluster *infrav1.OpenStackCluster,
168173
Subnet: &infrav1.Subnet{
169174
ID: openStackCluster.Status.Network.Subnet.ID,
170175
},
176+
PortOpts: &infrav1.PortOpts{
177+
Trunk: &openStackMachine.Spec.Trunk,
178+
},
171179
}}
172180
}
173181
return nets, nil
@@ -181,24 +189,16 @@ func (s *Service) createInstance(eventObject runtime.Object, clusterName string,
181189
if network.ID == "" {
182190
return nil, fmt.Errorf("no network was found or provided. Please check your machine configuration and try again")
183191
}
184-
192+
iTags := []string{}
193+
if len(instanceSpec.Tags) > 0 {
194+
iTags = instanceSpec.Tags
195+
}
185196
portName := getPortName(instanceSpec.Name, network.PortOpts, i)
186-
port, err := s.getOrCreatePort(eventObject, clusterName, portName, network, instanceSpec.SecurityGroups)
197+
port, err := s.getOrCreatePort(eventObject, clusterName, portName, network, &instanceSpec.SecurityGroups, iTags)
187198
if err != nil {
188199
return nil, err
189200
}
190201

191-
if instanceSpec.Trunk {
192-
trunk, err := s.getOrCreateTrunk(eventObject, clusterName, instanceSpec.Name, port.ID)
193-
if err != nil {
194-
return nil, err
195-
}
196-
197-
if err = s.replaceAllAttributesTags(eventObject, trunk.ID, instanceSpec.Tags); err != nil {
198-
return nil, err
199-
}
200-
}
201-
202202
for _, fip := range port.FixedIPs {
203203
if fip.SubnetID == instanceSpec.Subnet {
204204
accessIPv4 = fip.IPAddress
@@ -420,7 +420,7 @@ func (s *Service) getServerNetworks(networkParams []infrav1.NetworkParam) ([]inf
420420
return nets, nil
421421
}
422422

423-
func (s *Service) getOrCreatePort(eventObject runtime.Object, clusterName string, portName string, net infrav1.Network, instanceSecurityGroups []string) (*ports.Port, error) {
423+
func (s *Service) getOrCreatePort(eventObject runtime.Object, clusterName string, portName string, net infrav1.Network, instanceSecurityGroups *[]string, tags []string) (*ports.Port, error) {
424424
mc := metrics.NewMetricPrometheusContext("port", "list")
425425
allPages, err := ports.List(s.networkClient, ports.ListOpts{
426426
Name: portName,
@@ -467,7 +467,7 @@ func (s *Service) getOrCreatePort(eventObject runtime.Object, clusterName string
467467

468468
// inherit port security groups from the instance if not explicitly specified
469469
if securityGroups == nil {
470-
securityGroups = &instanceSecurityGroups
470+
securityGroups = instanceSecurityGroups
471471
}
472472
}
473473

@@ -523,6 +523,18 @@ func (s *Service) getOrCreatePort(eventObject runtime.Object, clusterName string
523523
}
524524

525525
record.Eventf(eventObject, "SuccessfulCreatePort", "Created port %s with id %s", port.Name, port.ID)
526+
if portOpts.Trunk != nil && *portOpts.Trunk {
527+
trunk, err := s.getOrCreateTrunk(eventObject, clusterName, port.Name, port.ID)
528+
if err != nil {
529+
record.Warnf(eventObject, "FailedCreateTrunk", "Failed to create trunk for port %s: %v", portName, err)
530+
return nil, err
531+
}
532+
if err = s.replaceAllAttributesTags(eventObject, trunk.ID, tags); err != nil {
533+
record.Warnf(eventObject, "FailedReplaceTags", "Failed to replace trunk tags %s: %v", portName, err)
534+
return nil, err
535+
}
536+
}
537+
526538
return port, nil
527539
}
528540

@@ -548,11 +560,11 @@ func (s *Service) getOrCreateTrunk(eventObject runtime.Object, clusterName, trun
548560
PortID: portID,
549561
}).AllPages()
550562
if mc.ObserveRequest(err) != nil {
551-
return nil, fmt.Errorf("searching for existing trunk for server: %v", err)
563+
return nil, fmt.Errorf("searching for existing trunk for port: %v", err)
552564
}
553565
trunkList, err := trunks.ExtractTrunks(allPages)
554566
if err != nil {
555-
return nil, fmt.Errorf("searching for existing trunk for server: %v", err)
567+
return nil, fmt.Errorf("searching for existing trunk for port: %v", err)
556568
}
557569

558570
if len(trunkList) != 0 {

0 commit comments

Comments
 (0)