Skip to content

Commit e7bda91

Browse files
author
Anwar Hassen
committed
Deprecating and replacing securityGroups field from OpenStackMachineTemplate ports
1 parent 83fe948 commit e7bda91

14 files changed

+124
-133
lines changed

api/v1alpha4/conversion.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,28 @@ func Convert_v1alpha5_PortOpts_To_v1alpha4_PortOpts(in *infrav1.PortOpts, out *P
212212
return nil
213213
}
214214

215+
func Convert_Slice_string_To_Slice_v1alpha5_SecurityGroupParam(in *[]string, out *[]infrav1.SecurityGroupParam, s conversion.Scope) error {
216+
if in != nil {
217+
for _, v1alpha4SecurityGroupUID := range *in {
218+
if v1alpha4SecurityGroupUID != "" {
219+
*out = append(*out, infrav1.SecurityGroupParam{UUID: v1alpha4SecurityGroupUID})
220+
}
221+
}
222+
}
223+
return nil
224+
}
225+
226+
func Convert_Slice_v1alpha5_SecurityGroupParam_To_Slice_string(in *[]infrav1.SecurityGroupParam, out *[]string, s conversion.Scope) error {
227+
if in != nil {
228+
for _, v1alpha5SecurityGroups := range *in {
229+
if v1alpha5SecurityGroups.UUID != "" {
230+
*out = append(*out, v1alpha5SecurityGroups.UUID)
231+
}
232+
}
233+
}
234+
return nil
235+
}
236+
215237
func Convert_Slice_v1alpha4_Network_To_Slice_v1alpha5_Network(in *[]Network, out *[]infrav1.Network, s conversion.Scope) error {
216238
*out = make([]infrav1.Network, len(*in))
217239
for i := range *in {

api/v1alpha4/conversion_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ func TestFuzzyConversion(t *testing.T) {
264264
v1alpha5PortOpts.Network = nil
265265
}
266266
}
267-
v1alpha5PortOpts.SecurityGroupFilters = nil
268267
},
269268
func(v1alpha5FixedIP *infrav1.FixedIP, c fuzz.Continue) {
270269
c.FuzzNoCustom(v1alpha5FixedIP)

api/v1alpha4/zz_generated.conversion.go

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

api/v1alpha5/types.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,9 @@ type PortOpts struct {
117117
FixedIPs []FixedIP `json:"fixedIPs,omitempty"`
118118
TenantID string `json:"tenantId,omitempty"`
119119
ProjectID string `json:"projectId,omitempty"`
120-
// The uuids of the security groups to assign to the instance
121-
SecurityGroups *[]string `json:"securityGroups,omitempty"`
122-
// The names, uuids, filters or any combination these of the security groups to assign to the instance
123-
SecurityGroupFilters []SecurityGroupParam `json:"securityGroupFilters,omitempty"`
124-
AllowedAddressPairs []AddressPair `json:"allowedAddressPairs,omitempty"`
120+
// The names of the security groups to assign to the port
121+
SecurityGroups *[]SecurityGroupParam `json:"securityGroups,omitempty"`
122+
AllowedAddressPairs []AddressPair `json:"allowedAddressPairs,omitempty"`
125123
// Enables and disables trunk at port level. If not provided, openStackMachine.Spec.Trunk is inherited.
126124
Trunk *bool `json:"trunk,omitempty"`
127125

api/v1alpha5/zz_generated.deepcopy.go

Lines changed: 2 additions & 7 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: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,9 +2779,9 @@ spec:
27792779
type: object
27802780
projectId:
27812781
type: string
2782-
securityGroupFilters:
2783-
description: The names, uuids, filters or any combination
2784-
these of the security groups to assign to the instance
2782+
securityGroups:
2783+
description: The names of the security groups to assign
2784+
to the port
27852785
items:
27862786
properties:
27872787
filter:
@@ -2823,12 +2823,6 @@ spec:
28232823
type: string
28242824
type: object
28252825
type: array
2826-
securityGroups:
2827-
description: The uuids of the security groups to assign
2828-
to the instance
2829-
items:
2830-
type: string
2831-
type: array
28322826
tags:
28332827
description: Tags applied to the port (and corresponding
28342828
trunk, if a trunk is configured.) These tags are applied
@@ -3298,9 +3292,9 @@ spec:
32983292
type: object
32993293
projectId:
33003294
type: string
3301-
securityGroupFilters:
3302-
description: The names, uuids, filters or any combination
3303-
these of the security groups to assign to the instance
3295+
securityGroups:
3296+
description: The names of the security groups to assign
3297+
to the port
33043298
items:
33053299
properties:
33063300
filter:
@@ -3342,12 +3336,6 @@ spec:
33423336
type: string
33433337
type: object
33443338
type: array
3345-
securityGroups:
3346-
description: The uuids of the security groups to assign
3347-
to the instance
3348-
items:
3349-
type: string
3350-
type: array
33513339
tags:
33523340
description: Tags applied to the port (and corresponding
33533341
trunk, if a trunk is configured.) These tags are applied
@@ -3678,9 +3666,9 @@ spec:
36783666
type: object
36793667
projectId:
36803668
type: string
3681-
securityGroupFilters:
3682-
description: The names, uuids, filters or any combination
3683-
these of the security groups to assign to the instance
3669+
securityGroups:
3670+
description: The names of the security groups to assign to
3671+
the port
36843672
items:
36853673
properties:
36863674
filter:
@@ -3722,12 +3710,6 @@ spec:
37223710
type: string
37233711
type: object
37243712
type: array
3725-
securityGroups:
3726-
description: The uuids of the security groups to assign to
3727-
the instance
3728-
items:
3729-
type: string
3730-
type: array
37313713
tags:
37323714
description: Tags applied to the port (and corresponding trunk,
37333715
if a trunk is configured.) These tags are applied in addition
@@ -3972,9 +3954,9 @@ spec:
39723954
type: object
39733955
projectId:
39743956
type: string
3975-
securityGroupFilters:
3976-
description: The names, uuids, filters or any combination
3977-
these of the security groups to assign to the instance
3957+
securityGroups:
3958+
description: The names of the security groups to assign to
3959+
the port
39783960
items:
39793961
properties:
39803962
filter:
@@ -4016,12 +3998,6 @@ spec:
40163998
type: string
40173999
type: object
40184000
type: array
4019-
securityGroups:
4020-
description: The uuids of the security groups to assign to
4021-
the instance
4022-
items:
4023-
type: string
4024-
type: array
40254001
tags:
40264002
description: Tags applied to the port (and corresponding trunk,
40274003
if a trunk is configured.) These tags are applied in addition

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,10 +1049,9 @@ spec:
10491049
type: object
10501050
projectId:
10511051
type: string
1052-
securityGroupFilters:
1053-
description: The names, uuids, filters or any
1054-
combination these of the security groups to
1055-
assign to the instance
1052+
securityGroups:
1053+
description: The names of the security groups
1054+
to assign to the port
10561055
items:
10571056
properties:
10581057
filter:
@@ -1094,12 +1093,6 @@ spec:
10941093
type: string
10951094
type: object
10961095
type: array
1097-
securityGroups:
1098-
description: The uuids of the security groups
1099-
to assign to the instance
1100-
items:
1101-
type: string
1102-
type: array
11031096
tags:
11041097
description: Tags applied to the port (and corresponding
11051098
trunk, if a trunk is configured.) These tags

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,9 +1044,9 @@ spec:
10441044
type: object
10451045
projectId:
10461046
type: string
1047-
securityGroupFilters:
1048-
description: The names, uuids, filters or any combination these
1049-
of the security groups to assign to the instance
1047+
securityGroups:
1048+
description: The names of the security groups to assign to the
1049+
port
10501050
items:
10511051
properties:
10521052
filter:
@@ -1088,12 +1088,6 @@ spec:
10881088
type: string
10891089
type: object
10901090
type: array
1091-
securityGroups:
1092-
description: The uuids of the security groups to assign to the
1093-
instance
1094-
items:
1095-
type: string
1096-
type: array
10971091
tags:
10981092
description: Tags applied to the port (and corresponding trunk,
10991093
if a trunk is configured.) These tags are applied in addition

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -951,9 +951,9 @@ spec:
951951
type: object
952952
projectId:
953953
type: string
954-
securityGroupFilters:
955-
description: The names, uuids, filters or any combination
956-
these of the security groups to assign to the instance
954+
securityGroups:
955+
description: The names of the security groups to assign
956+
to the port
957957
items:
958958
properties:
959959
filter:
@@ -995,12 +995,6 @@ spec:
995995
type: string
996996
type: object
997997
type: array
998-
securityGroups:
999-
description: The uuids of the security groups to assign
1000-
to the instance
1001-
items:
1002-
type: string
1003-
type: array
1004998
tags:
1005999
description: Tags applied to the port (and corresponding
10061000
trunk, if a trunk is configured.) These tags are applied

pkg/cloud/services/networking/port.go

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,15 @@ func (s *Service) GetOrCreatePort(eventObject runtime.Object, clusterName string
8989
MACAddress: ap.MACAddress,
9090
})
9191
}
92-
securityGroups, err = s.CollectPortSecurityGroups(eventObject, portOpts.SecurityGroups, portOpts.SecurityGroupFilters)
93-
if err != nil {
94-
return nil, err
92+
portSecurityGroups := portOpts.SecurityGroups
93+
if portSecurityGroups != nil {
94+
portSecurityGroups, err := s.GetSecurityGroups(*portSecurityGroups)
95+
if err != nil {
96+
return nil, fmt.Errorf("error getting security groups: %v", err)
97+
}
98+
if len(portSecurityGroups) > 0 {
99+
securityGroups = &portSecurityGroups
100+
}
95101
}
96102
// inherit port security groups from the instance if not explicitly specified
97103
if securityGroups == nil || len(*securityGroups) == 0 {
@@ -258,41 +264,3 @@ func (s *Service) GarbageCollectErrorInstancesPort(eventObject runtime.Object, i
258264

259265
return nil
260266
}
261-
262-
// CollectPortSecurityGroups collects distinct securityGroups from port.SecurityGroups and port.SecurityGroupFilter fields.
263-
func (s *Service) CollectPortSecurityGroups(eventObject runtime.Object, portSecurityGroups *[]string, portSecurityGroupFilters []infrav1.SecurityGroupParam) (*[]string, error) {
264-
var allSecurityGroupIDs []string
265-
// security groups provided with the portSecurityGroupFilters fields
266-
securityGroupFiltersByID, err := s.GetSecurityGroups(portSecurityGroupFilters)
267-
if err != nil {
268-
return portSecurityGroups, fmt.Errorf("error getting security groups: %v", err)
269-
}
270-
allSecurityGroupIDs = append(allSecurityGroupIDs, securityGroupFiltersByID...)
271-
securityGroupCount := 0
272-
// security groups provided with the portSecurityGroups fields
273-
if portSecurityGroups != nil {
274-
allSecurityGroupIDs = append(allSecurityGroupIDs, *portSecurityGroups...)
275-
}
276-
// generate unique values
277-
uids := make(map[string]int)
278-
for _, sg := range allSecurityGroupIDs {
279-
if sg == "" {
280-
continue
281-
}
282-
// count distinct values
283-
_, ok := uids[sg]
284-
if !ok {
285-
securityGroupCount++
286-
}
287-
uids[sg] = 1
288-
}
289-
distinctSecurityGroupIDs := make([]string, 0, securityGroupCount)
290-
// collect distict values
291-
for key := range uids {
292-
if key == "" {
293-
continue
294-
}
295-
distinctSecurityGroupIDs = append(distinctSecurityGroupIDs, key)
296-
}
297-
return &distinctSecurityGroupIDs, nil
298-
}

0 commit comments

Comments
 (0)