Skip to content

Commit 7e082f1

Browse files
committed
Don't apply worker SG to control plane machines
Currently, if a worker machine security group is specified but a control plane machine security group is not, the worker machine SG will be be applied to both worker *and* control plane machines. Correct this mistake. Signed-off-by: Stephen Finucane <[email protected]>
1 parent f323b4f commit 7e082f1

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

controllers/openstackmachine_controller.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -499,15 +499,21 @@ func machineToInstanceSpec(openStackCluster *infrav1.OpenStackCluster, machine *
499499
instanceSpec.SecurityGroups = openStackMachine.Spec.SecurityGroups
500500
if openStackCluster.Spec.ManagedSecurityGroups {
501501
var managedSecurityGroup string
502-
if util.IsControlPlaneMachine(machine) && openStackCluster.Status.ControlPlaneSecurityGroup != nil {
503-
managedSecurityGroup = openStackCluster.Status.ControlPlaneSecurityGroup.ID
504-
} else if openStackCluster.Status.WorkerSecurityGroup != nil {
505-
managedSecurityGroup = openStackCluster.Status.WorkerSecurityGroup.ID
502+
if util.IsControlPlaneMachine(machine) {
503+
if openStackCluster.Status.ControlPlaneSecurityGroup != nil {
504+
managedSecurityGroup = openStackCluster.Status.ControlPlaneSecurityGroup.ID
505+
}
506+
} else {
507+
if openStackCluster.Status.WorkerSecurityGroup != nil {
508+
managedSecurityGroup = openStackCluster.Status.WorkerSecurityGroup.ID
509+
}
506510
}
507511

508-
instanceSpec.SecurityGroups = append(instanceSpec.SecurityGroups, infrav1.SecurityGroupFilter{
509-
ID: managedSecurityGroup,
510-
})
512+
if managedSecurityGroup != "" {
513+
instanceSpec.SecurityGroups = append(instanceSpec.SecurityGroups, infrav1.SecurityGroupFilter{
514+
ID: managedSecurityGroup,
515+
})
516+
}
511517
}
512518

513519
instanceSpec.Ports = openStackMachine.Spec.Ports

controllers/openstackmachine_controller_test.go

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ func getDefaultOpenStackMachine() *infrav1.OpenStackMachine {
8989
ServerMetadata: map[string]string{
9090
"test-metadata": "test-value",
9191
},
92-
ConfigDrive: pointer.Bool(true),
93-
ServerGroupID: serverGroupUUID,
92+
ConfigDrive: pointer.Bool(true),
93+
SecurityGroups: []infrav1.SecurityGroupFilter{},
94+
ServerGroupID: serverGroupUUID,
9495
},
9596
}
9697
}
@@ -105,10 +106,11 @@ func getDefaultInstanceSpec() *compute.InstanceSpec {
105106
Metadata: map[string]string{
106107
"test-metadata": "test-value",
107108
},
108-
ConfigDrive: *pointer.Bool(true),
109-
FailureDomain: *pointer.String(failureDomain),
110-
ServerGroupID: serverGroupUUID,
111-
Tags: []string{"test-tag"},
109+
ConfigDrive: *pointer.Bool(true),
110+
FailureDomain: *pointer.String(failureDomain),
111+
ServerGroupID: serverGroupUUID,
112+
SecurityGroups: []infrav1.SecurityGroupFilter{},
113+
Tags: []string{"test-tag"},
112114
}
113115
}
114116

@@ -165,6 +167,44 @@ func Test_machineToInstanceSpec(t *testing.T) {
165167
return i
166168
},
167169
},
170+
{
171+
name: "Control plane security group not applied to worker",
172+
openStackCluster: func() *infrav1.OpenStackCluster {
173+
c := getDefaultOpenStackCluster()
174+
c.Spec.ManagedSecurityGroups = true
175+
c.Status.WorkerSecurityGroup = nil
176+
return c
177+
},
178+
machine: getDefaultMachine,
179+
openStackMachine: getDefaultOpenStackMachine,
180+
wantInstanceSpec: func() *compute.InstanceSpec {
181+
i := getDefaultInstanceSpec()
182+
i.SecurityGroups = []infrav1.SecurityGroupFilter{}
183+
return i
184+
},
185+
},
186+
{
187+
name: "Worker security group not applied to control plane",
188+
openStackCluster: func() *infrav1.OpenStackCluster {
189+
c := getDefaultOpenStackCluster()
190+
c.Spec.ManagedSecurityGroups = true
191+
c.Status.ControlPlaneSecurityGroup = nil
192+
return c
193+
},
194+
machine: func() *clusterv1.Machine {
195+
m := getDefaultMachine()
196+
m.Labels = map[string]string{
197+
clusterv1.MachineControlPlaneLabel: "true",
198+
}
199+
return m
200+
},
201+
openStackMachine: getDefaultOpenStackMachine,
202+
wantInstanceSpec: func() *compute.InstanceSpec {
203+
i := getDefaultInstanceSpec()
204+
i.SecurityGroups = []infrav1.SecurityGroupFilter{}
205+
return i
206+
},
207+
},
168208
{
169209
name: "Extra security group",
170210
openStackCluster: func() *infrav1.OpenStackCluster {

0 commit comments

Comments
 (0)