Skip to content

Commit 091e2a7

Browse files
authored
Merge pull request #1516 from Nordix/lentzi90/remove-security-groups
⚠️ Remove PortOpts.SecurityGroups
2 parents e35f1ca + 6a0e533 commit 091e2a7

14 files changed

+169
-127
lines changed

api/v1alpha5/conversion.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,8 @@ func Convert_v1alpha7_APIServerLoadBalancer_To_v1alpha5_APIServerLoadBalancer(in
224224
// Provider was originally added in v1alpha7, but was backported to v1alpha6, but has no equivalent in v1alpha5
225225
return autoConvert_v1alpha7_APIServerLoadBalancer_To_v1alpha5_APIServerLoadBalancer(in, out, s)
226226
}
227+
228+
func Convert_v1alpha5_PortOpts_To_v1alpha7_PortOpts(in *PortOpts, out *infrav1.PortOpts, s conversion.Scope) error {
229+
// SecurityGroups have been removed in v1alpha7.
230+
return autoConvert_v1alpha5_PortOpts_To_v1alpha7_PortOpts(in, out, s)
231+
}

api/v1alpha5/zz_generated.conversion.go

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

api/v1alpha6/conversion.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ func restorev1alpha6MachineSpec(previous *OpenStackMachineSpec, dst *OpenStackMa
9494
dst.Ports = previous.Ports
9595
}
9696

97+
func restorev1alpha6ClusterStatus(previous *OpenStackClusterStatus, dst *OpenStackClusterStatus) {
98+
// PortOpts.SecurityGroups have been removed in v1alpha7
99+
// We restore the whole PortOpts/Networks since they are anyway immutable.
100+
if previous.ExternalNetwork != nil {
101+
dst.ExternalNetwork.PortOpts = previous.ExternalNetwork.PortOpts
102+
}
103+
if previous.Network != nil {
104+
dst.Network = previous.Network
105+
}
106+
if previous.Bastion != nil && previous.Bastion.Networks != nil {
107+
dst.Bastion.Networks = previous.Bastion.Networks
108+
}
109+
}
110+
97111
func restorev1alpha7MachineSpec(previous *infrav1.OpenStackMachineSpec, dst *infrav1.OpenStackMachineSpec) {
98112
// PropagateUplinkStatus has been added in v1alpha7.
99113
// We restore the whole Ports since they are anyway immutable.
@@ -157,6 +171,7 @@ func (r *OpenStackCluster) ConvertFrom(srcRaw ctrlconversion.Hub) error {
157171
if prevBastion != nil {
158172
restorev1alpha6MachineSpec(&prevBastion.Instance, &r.Spec.Bastion.Instance)
159173
}
174+
restorev1alpha6ClusterStatus(&previous.Status, &r.Status)
160175
}
161176

162177
return nil
@@ -374,6 +389,18 @@ func Convert_v1alpha7_OpenStackClusterSpec_To_v1alpha6_OpenStackClusterSpec(in *
374389
return autoConvert_v1alpha7_OpenStackClusterSpec_To_v1alpha6_OpenStackClusterSpec(in, out, s)
375390
}
376391

392+
func Convert_v1alpha6_PortOpts_To_v1alpha7_PortOpts(in *PortOpts, out *infrav1.PortOpts, s conversion.Scope) error {
393+
err := autoConvert_v1alpha6_PortOpts_To_v1alpha7_PortOpts(in, out, s)
394+
if err != nil {
395+
return err
396+
}
397+
// SecurityGroups are removed in v1alpha7 without replacement. SecurityGroupFilters can be used instead.
398+
for i := range in.SecurityGroups {
399+
out.SecurityGroupFilters = append(out.SecurityGroupFilters, infrav1.SecurityGroupParam{UUID: in.SecurityGroups[i]})
400+
}
401+
return nil
402+
}
403+
377404
func Convert_Slice_v1alpha6_Network_To_Slice_v1alpha7_Network(in *[]Network, out *[]infrav1.Network, s conversion.Scope) error {
378405
*out = make([]infrav1.Network, len(*in))
379406
for i := range *in {

api/v1alpha6/conversion_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/onsi/gomega"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
runtime "k8s.io/apimachinery/pkg/runtime"
2425
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
2526
ctrlconversion "sigs.k8s.io/controller-runtime/pkg/conversion"
2627

@@ -337,6 +338,7 @@ func TestNetworksToPorts(t *testing.T) {
337338
},
338339
},
339340
}
341+
340342
for _, tt := range tests {
341343
t.Run(tt.name, func(t *testing.T) {
342344
before := &OpenStackMachine{
@@ -349,3 +351,91 @@ func TestNetworksToPorts(t *testing.T) {
349351
})
350352
}
351353
}
354+
355+
// TestPortOptsConvertTo checks conversion TO the hub version.
356+
// This is useful to ensure that the SecurityGroups are properly
357+
// converted to SecurityGroupFilters, and merged with any existing
358+
// SecurityGroupFilters.
359+
func TestPortOptsConvertTo(t *testing.T) {
360+
g := gomega.NewWithT(t)
361+
scheme := runtime.NewScheme()
362+
g.Expect(AddToScheme(scheme)).To(gomega.Succeed())
363+
g.Expect(infrav1.AddToScheme(scheme)).To(gomega.Succeed())
364+
365+
// Variables used in the tests
366+
uuids := []string{"abc123", "123abc"}
367+
securityGroupsUuids := []infrav1.SecurityGroupParam{
368+
{UUID: uuids[0]},
369+
{UUID: uuids[1]},
370+
}
371+
securityGroupFilter := []SecurityGroupParam{
372+
{Name: "one"},
373+
{UUID: "654cba"},
374+
}
375+
securityGroupFilterMerged := []infrav1.SecurityGroupParam{
376+
{Name: "one"},
377+
{UUID: "654cba"},
378+
{UUID: uuids[0]},
379+
{UUID: uuids[1]},
380+
}
381+
382+
tests := []struct {
383+
name string
384+
// spokePortOpts are the PortOpts in the spoke version
385+
spokePortOpts []PortOpts
386+
// hubPortOpts are the PortOpts in the hub version that should be expected after conversion
387+
hubPortOpts []infrav1.PortOpts
388+
}{
389+
{
390+
// The list of security group UUIDs should be translated to proper SecurityGroupParams
391+
name: "SecurityGroups to SecurityGroupFilters",
392+
spokePortOpts: []PortOpts{{
393+
SecurityGroups: uuids,
394+
}},
395+
hubPortOpts: []infrav1.PortOpts{{
396+
SecurityGroupFilters: securityGroupsUuids,
397+
}},
398+
},
399+
{
400+
name: "Merge SecurityGroups and SecurityGroupFilters",
401+
spokePortOpts: []PortOpts{{
402+
SecurityGroups: uuids,
403+
SecurityGroupFilters: securityGroupFilter,
404+
}},
405+
hubPortOpts: []infrav1.PortOpts{{
406+
SecurityGroupFilters: securityGroupFilterMerged,
407+
}},
408+
},
409+
}
410+
411+
for _, tt := range tests {
412+
t.Run(tt.name, func(t *testing.T) {
413+
// The spoke machine template with added PortOpts
414+
spokeMachineTemplate := OpenStackMachineTemplate{
415+
Spec: OpenStackMachineTemplateSpec{
416+
Template: OpenStackMachineTemplateResource{
417+
Spec: OpenStackMachineSpec{
418+
Ports: tt.spokePortOpts,
419+
},
420+
},
421+
},
422+
}
423+
// The hub machine template with added PortOpts
424+
hubMachineTemplate := infrav1.OpenStackMachineTemplate{
425+
Spec: infrav1.OpenStackMachineTemplateSpec{
426+
Template: infrav1.OpenStackMachineTemplateResource{
427+
Spec: infrav1.OpenStackMachineSpec{
428+
Ports: tt.hubPortOpts,
429+
},
430+
},
431+
},
432+
}
433+
convertedHub := infrav1.OpenStackMachineTemplate{}
434+
435+
err := spokeMachineTemplate.ConvertTo(&convertedHub)
436+
g.Expect(err).NotTo(gomega.HaveOccurred())
437+
// Comparing spec only here since the conversion will also add annotations that we don't care about for the test
438+
g.Expect(convertedHub.Spec).To(gomega.Equal(hubMachineTemplate.Spec))
439+
})
440+
}
441+
}

api/v1alpha6/zz_generated.conversion.go

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

api/v1alpha7/types.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ type PortOpts struct {
115115
FixedIPs []FixedIP `json:"fixedIPs,omitempty"`
116116
TenantID string `json:"tenantId,omitempty"`
117117
ProjectID string `json:"projectId,omitempty"`
118-
// The uuids of the security groups to assign to the instance
119-
// +listType=set
120-
SecurityGroups []string `json:"securityGroups,omitempty"`
121118
// The names, uuids, filters or any combination these of the security groups to assign to the instance
122119
SecurityGroupFilters []SecurityGroupParam `json:"securityGroupFilters,omitempty"`
123120
AllowedAddressPairs []AddressPair `json:"allowedAddressPairs,omitempty"`

api/v1alpha7/zz_generated.deepcopy.go

Lines changed: 0 additions & 5 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: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3981,13 +3981,6 @@ spec:
39813981
type: string
39823982
type: object
39833983
type: array
3984-
securityGroups:
3985-
description: The uuids of the security groups to assign
3986-
to the instance
3987-
items:
3988-
type: string
3989-
type: array
3990-
x-kubernetes-list-type: set
39913984
tags:
39923985
description: Tags applied to the port (and corresponding
39933986
trunk, if a trunk is configured.) These tags are applied
@@ -4570,13 +4563,6 @@ spec:
45704563
type: string
45714564
type: object
45724565
type: array
4573-
securityGroups:
4574-
description: The uuids of the security groups to assign
4575-
to the instance
4576-
items:
4577-
type: string
4578-
type: array
4579-
x-kubernetes-list-type: set
45804566
tags:
45814567
description: Tags applied to the port (and corresponding
45824568
trunk, if a trunk is configured.) These tags are applied
@@ -4998,13 +4984,6 @@ spec:
49984984
type: string
49994985
type: object
50004986
type: array
5001-
securityGroups:
5002-
description: The uuids of the security groups to assign to
5003-
the instance
5004-
items:
5005-
type: string
5006-
type: array
5007-
x-kubernetes-list-type: set
50084987
tags:
50094988
description: Tags applied to the port (and corresponding trunk,
50104989
if a trunk is configured.) These tags are applied in addition
@@ -5339,13 +5318,6 @@ spec:
53395318
type: string
53405319
type: object
53415320
type: array
5342-
securityGroups:
5343-
description: The uuids of the security groups to assign to
5344-
the instance
5345-
items:
5346-
type: string
5347-
type: array
5348-
x-kubernetes-list-type: set
53495321
tags:
53505322
description: Tags applied to the port (and corresponding trunk,
53515323
if a trunk is configured.) These tags are applied in addition

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,13 +1824,6 @@ spec:
18241824
type: string
18251825
type: object
18261826
type: array
1827-
securityGroups:
1828-
description: The uuids of the security groups
1829-
to assign to the instance
1830-
items:
1831-
type: string
1832-
type: array
1833-
x-kubernetes-list-type: set
18341827
tags:
18351828
description: Tags applied to the port (and corresponding
18361829
trunk, if a trunk is configured.) These tags

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,13 +1357,6 @@ spec:
13571357
type: string
13581358
type: object
13591359
type: array
1360-
securityGroups:
1361-
description: The uuids of the security groups to assign to the
1362-
instance
1363-
items:
1364-
type: string
1365-
type: array
1366-
x-kubernetes-list-type: set
13671360
tags:
13681361
description: Tags applied to the port (and corresponding trunk,
13691362
if a trunk is configured.) These tags are applied in addition

0 commit comments

Comments
 (0)