diff --git a/controllers/openstackmachine_controller.go b/controllers/openstackmachine_controller.go index a07b5144b0..6c8f3c0a2a 100644 --- a/controllers/openstackmachine_controller.go +++ b/controllers/openstackmachine_controller.go @@ -527,6 +527,9 @@ func openStackMachineSpecToOpenStackServerSpec(openStackMachineSpec *infrav1.Ope }, } } + if len(openStackMachineSpec.SecurityGroups) > 0 { + serverPorts[i].SecurityGroups = append(serverPorts[i].SecurityGroups, openStackMachineSpec.SecurityGroups...) + } } openStackServerSpec.Ports = serverPorts diff --git a/controllers/openstackmachine_controller_test.go b/controllers/openstackmachine_controller_test.go index 7d9af38abb..2cace25414 100644 --- a/controllers/openstackmachine_controller_test.go +++ b/controllers/openstackmachine_controller_test.go @@ -76,6 +76,21 @@ func TestOpenStackMachineSpecToOpenStackServerSpec(t *testing.T) { }, }, } + portOptsWithAdditionalSecurityGroup := []infrav1.PortOpts{ + { + Network: &infrav1.NetworkParam{ + ID: ptr.To(openStackCluster.Status.Network.ID), + }, + SecurityGroups: []infrav1.SecurityGroupParam{ + { + ID: ptr.To(openStackCluster.Status.WorkerSecurityGroup.ID), + }, + { + ID: ptr.To(extraSecurityGroupUUID), + }, + }, + }, + } image := infrav1.ImageParam{Filter: &infrav1.ImageFilter{Name: ptr.To("my-image")}} tags := []string{"tag1", "tag2"} userData := &corev1.LocalObjectReference{Name: "server-data-secret"} @@ -101,6 +116,28 @@ func TestOpenStackMachineSpecToOpenStackServerSpec(t *testing.T) { UserDataRef: userData, }, }, + { + name: "Test a OpenStackMachineSpec to OpenStackServerSpec conversion with an additional security group", + spec: &infrav1.OpenStackMachineSpec{ + Flavor: ptr.To(flavorName), + Image: image, + SSHKeyName: sshKeyName, + SecurityGroups: []infrav1.SecurityGroupParam{ + { + ID: ptr.To(extraSecurityGroupUUID), + }, + }, + }, + want: &infrav1alpha1.OpenStackServerSpec{ + Flavor: ptr.To(flavorName), + IdentityRef: identityRef, + Image: image, + SSHKeyName: sshKeyName, + Ports: portOptsWithAdditionalSecurityGroup, + Tags: tags, + UserDataRef: userData, + }, + }, } for i := range tests { tt := tests[i]