Skip to content

Commit 1593e43

Browse files
committed
openstackmachine: stop supporting port SGs in Ports
When defining an OpenStackMachine with `Ports`, we no longer allow the use of `SecurityGroupFilters` because we now have a separate loop to reconcile ports based on 2 things: * If `OpenStackCluster.Spec.ManagedSecurityGroups`, machine ports will be reconciled to have the security group of their role (cp/worker). * If `OpenStackMachine.Spec.SecurityGroups` is specified for the machine, we'll reconcile the machine ports to ensure they have the Security Groups asked by the user. However if a user provides `OpenStackMachine.Spec.Ports`, we will return an error if the Port has `SecurityGroupFilters` because we have a separate reconcile which update *all* ports with the same Security Groups.
1 parent 4d4162b commit 1593e43

File tree

3 files changed

+13
-55
lines changed

3 files changed

+13
-55
lines changed

pkg/cloud/services/compute/instance.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,19 +270,14 @@ func (s *Service) createInstanceImpl(eventObject runtime.Object, openStackCluste
270270
return nil, err
271271
}
272272

273-
securityGroups, err := networkingService.GetSecurityGroups(instanceSpec.SecurityGroups)
274-
if err != nil {
275-
return nil, fmt.Errorf("error getting security groups: %v", err)
276-
}
277-
278273
for i := range ports {
279274
portOpts := &ports[i]
280275
iTags := []string{}
281276
if len(instanceSpec.Tags) > 0 {
282277
iTags = instanceSpec.Tags
283278
}
284279
portName := networking.GetPortName(instanceSpec.Name, portOpts, i)
285-
port, err := networkingService.GetOrCreatePort(eventObject, clusterName, portName, portOpts, securityGroups, iTags)
280+
port, err := networkingService.GetOrCreatePort(eventObject, clusterName, portName, portOpts, iTags)
286281
if err != nil {
287282
return nil, err
288283
}

pkg/cloud/services/networking/port.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (s *Service) GetPortFromInstanceIP(instanceID string, ip string) ([]ports.P
5454
return s.client.ListPort(portOpts)
5555
}
5656

57-
func (s *Service) GetOrCreatePort(eventObject runtime.Object, clusterName string, portName string, portOpts *infrav1.PortOpts, instanceSecurityGroups []string, instanceTags []string) (*ports.Port, error) {
57+
func (s *Service) GetOrCreatePort(eventObject runtime.Object, clusterName string, portName string, portOpts *infrav1.PortOpts, instanceTags []string) (*ports.Port, error) {
5858
networkID := portOpts.Network.ID
5959

6060
existingPorts, err := s.client.ListPort(ports.ListOpts{
@@ -88,14 +88,7 @@ func (s *Service) GetOrCreatePort(eventObject runtime.Object, clusterName string
8888
})
8989
}
9090
if portOpts.SecurityGroupFilters != nil {
91-
securityGroups, err = s.GetSecurityGroups(portOpts.SecurityGroupFilters)
92-
if err != nil {
93-
return nil, fmt.Errorf("error getting security groups: %v", err)
94-
}
95-
}
96-
// inherit port security groups from the instance if not explicitly specified
97-
if len(securityGroups) == 0 {
98-
securityGroups = instanceSecurityGroups
91+
return nil, fmt.Errorf("security group filters are not supported for port creation, use OpenStackMachine.Spec.SecurityGroups instead")
9992
}
10093
}
10194

pkg/cloud/services/networking/port_test.go

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,18 @@ func Test_GetOrCreatePort(t *testing.T) {
4747
portSecurityGroupID := "f51d1206-fc5a-4f7a-a5c0-2e03e44e4dc0"
4848

4949
// Other arbitrary variables passed in to the tests
50-
instanceSecurityGroups := []string{"instance-secgroup"}
51-
securityGroupUUIDs := []string{portSecurityGroupID}
5250
portSecurityGroupFilters := []infrav1.SecurityGroupFilter{{ID: portSecurityGroupID, Name: "port-secgroup"}}
5351
valueSpecs := map[string]string{"key": "value"}
5452

5553
pointerToTrue := pointerTo(true)
5654
pointerToFalse := pointerTo(false)
5755

5856
tests := []struct {
59-
name string
60-
portName string
61-
port infrav1.PortOpts
62-
instanceSecurityGroups []string
63-
tags []string
64-
expect func(m *mock.MockNetworkClientMockRecorder)
57+
name string
58+
portName string
59+
port infrav1.PortOpts
60+
tags []string
61+
expect func(m *mock.MockNetworkClientMockRecorder)
6562
// Note the 'wanted' port isn't so important, since it will be whatever we tell ListPort or CreatePort to return.
6663
// Mostly in this test suite, we're checking that ListPort/CreatePort is called with the expected port opts.
6764
want *ports.Port
@@ -75,7 +72,6 @@ func Test_GetOrCreatePort(t *testing.T) {
7572
ID: netID,
7673
},
7774
},
78-
nil,
7975
[]string{},
8076
func(m *mock.MockNetworkClientMockRecorder) {
8177
m.
@@ -99,7 +95,6 @@ func Test_GetOrCreatePort(t *testing.T) {
9995
ID: netID,
10096
},
10197
},
102-
nil,
10398
[]string{},
10499
func(m *mock.MockNetworkClientMockRecorder) {
105100
m.
@@ -123,14 +118,13 @@ func Test_GetOrCreatePort(t *testing.T) {
123118
true,
124119
},
125120
{
126-
"creates port with defaults (description and secgroups) if not specified in portOpts",
121+
"creates port with defaults (description) if not specified in portOpts",
127122
"foo-port-1",
128123
infrav1.PortOpts{
129124
Network: &infrav1.NetworkFilter{
130125
ID: netID,
131126
},
132127
},
133-
instanceSecurityGroups,
134128
[]string{},
135129
func(m *mock.MockNetworkClientMockRecorder) {
136130
// No ports found
@@ -144,7 +138,6 @@ func Test_GetOrCreatePort(t *testing.T) {
144138
CreateOptsBuilder: ports.CreateOpts{
145139
Name: "foo-port-1",
146140
Description: "Created by cluster-api-provider-openstack cluster test-cluster",
147-
SecurityGroups: &instanceSecurityGroups,
148141
NetworkID: netID,
149142
AllowedAddressPairs: []ports.AddressPair{},
150143
},
@@ -170,7 +163,6 @@ func Test_GetOrCreatePort(t *testing.T) {
170163
},
171164
IPAddress: "192.168.0.50",
172165
}, {IPAddress: "192.168.1.50"}},
173-
SecurityGroupFilters: portSecurityGroupFilters,
174166
AllowedAddressPairs: []infrav1.AddressPair{{
175167
IPAddress: "10.10.10.10",
176168
MACAddress: "f1:f1:f1:f1:f1:f1",
@@ -185,7 +177,6 @@ func Test_GetOrCreatePort(t *testing.T) {
185177
Tags: []string{"my-port-tag"},
186178
},
187179
nil,
188-
nil,
189180
func(m *mock.MockNetworkClientMockRecorder) {
190181
portCreateOpts := ports.CreateOpts{
191182
NetworkID: netID,
@@ -201,7 +192,6 @@ func Test_GetOrCreatePort(t *testing.T) {
201192
IPAddress: "192.168.1.50",
202193
},
203194
},
204-
SecurityGroups: &securityGroupUUIDs,
205195
AllowedAddressPairs: []ports.AddressPair{{
206196
IPAddress: "10.10.10.10",
207197
MACAddress: "f1:f1:f1:f1:f1:f1",
@@ -267,7 +257,6 @@ func Test_GetOrCreatePort(t *testing.T) {
267257
}},
268258
},
269259
nil,
270-
nil,
271260
func(m *mock.MockNetworkClientMockRecorder) {
272261
m.
273262
ListPort(ports.ListOpts{
@@ -295,37 +284,24 @@ func Test_GetOrCreatePort(t *testing.T) {
295284
true,
296285
},
297286
{
298-
"overrides default (instance) security groups if port security groups are specified",
287+
"fails to create port with specified portOpts if security group filters are specified",
299288
"foo-port-1",
300289
infrav1.PortOpts{
301290
Network: &infrav1.NetworkFilter{
302291
ID: netID,
303292
},
304293
SecurityGroupFilters: portSecurityGroupFilters,
305294
},
306-
instanceSecurityGroups,
307295
[]string{},
308296
func(m *mock.MockNetworkClientMockRecorder) {
309-
// No ports found
310297
m.
311298
ListPort(ports.ListOpts{
312299
Name: "foo-port-1",
313300
NetworkID: netID,
314301
}).Return([]ports.Port{}, nil)
315-
m.
316-
CreatePort(portsbinding.CreateOptsExt{
317-
CreateOptsBuilder: ports.CreateOpts{
318-
Name: "foo-port-1",
319-
Description: "Created by cluster-api-provider-openstack cluster test-cluster",
320-
SecurityGroups: &securityGroupUUIDs,
321-
NetworkID: netID,
322-
AllowedAddressPairs: []ports.AddressPair{},
323-
},
324-
},
325-
).Return(&ports.Port{ID: portID1}, nil)
326302
},
327-
&ports.Port{ID: portID1},
328-
false,
303+
nil,
304+
true,
329305
},
330306
{
331307
"creates port with instance tags when port tags aren't specified",
@@ -335,7 +311,6 @@ func Test_GetOrCreatePort(t *testing.T) {
335311
ID: netID,
336312
},
337313
},
338-
nil,
339314
[]string{"my-instance-tag"},
340315
func(m *mock.MockNetworkClientMockRecorder) {
341316
// No ports found
@@ -366,7 +341,6 @@ func Test_GetOrCreatePort(t *testing.T) {
366341
},
367342
Tags: []string{"my-port-tag"},
368343
},
369-
nil,
370344
[]string{"my-instance-tag"},
371345
func(m *mock.MockNetworkClientMockRecorder) {
372346
// No ports found
@@ -399,7 +373,6 @@ func Test_GetOrCreatePort(t *testing.T) {
399373
},
400374
Trunk: pointerToTrue,
401375
},
402-
nil,
403376
[]string{"my-tag"},
404377
func(m *mock.MockNetworkClientMockRecorder) {
405378
// No ports found
@@ -451,7 +424,6 @@ func Test_GetOrCreatePort(t *testing.T) {
451424
},
452425
},
453426
nil,
454-
nil,
455427
func(m *mock.MockNetworkClientMockRecorder) {
456428
// No ports found
457429
m.
@@ -482,7 +454,6 @@ func Test_GetOrCreatePort(t *testing.T) {
482454
},
483455
PropagateUplinkStatus: pointerToTrue,
484456
},
485-
instanceSecurityGroups,
486457
[]string{},
487458
func(m *mock.MockNetworkClientMockRecorder) {
488459
// No ports found
@@ -496,7 +467,6 @@ func Test_GetOrCreatePort(t *testing.T) {
496467
CreateOptsBuilder: ports.CreateOpts{
497468
Name: "foo-port-1",
498469
Description: "Created by cluster-api-provider-openstack cluster test-cluster",
499-
SecurityGroups: &instanceSecurityGroups,
500470
NetworkID: netID,
501471
AllowedAddressPairs: []ports.AddressPair{},
502472
PropagateUplinkStatus: pointerToTrue,
@@ -523,7 +493,6 @@ func Test_GetOrCreatePort(t *testing.T) {
523493
"test-cluster",
524494
tt.portName,
525495
&tt.port,
526-
tt.instanceSecurityGroups,
527496
tt.tags,
528497
)
529498
if tt.wantErr {
@@ -618,6 +587,7 @@ func Test_GarbageCollectErrorInstancesPort(t *testing.T) {
618587
func pointerTo(b bool) *bool {
619588
return &b
620589
}
590+
621591
func Test_GetPortIDsFromInstanceID(t *testing.T) {
622592
mockCtrl := gomock.NewController(t)
623593
defer mockCtrl.Finish()

0 commit comments

Comments
 (0)