Skip to content

Commit 6c50ec7

Browse files
committed
- xport only necessary constants
1 parent 722b0b7 commit 6c50ec7

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

pkg/cloud/services/loadbalancer/loadbalancer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (s *Service) ReconcileLoadBalancer(clusterName string, openStackCluster *in
178178
}
179179

180180
func (s *Service) assignNeutronLbaasAPISecGroup(clusterName string, lb *loadbalancers.LoadBalancer) error {
181-
neutronLbaasSecGroupName := networking.GetSecurityGroupName(clusterName, networking.NeutronLbaasSuffix)
181+
neutronLbaasSecGroupName := fmt.Sprintf("%s-cluster-%s-secgroup-%s", networking.SecGroupPrefix, clusterName, networking.NeutronLbaasSuffix)
182182
listOpts := groups.ListOpts{
183183
Name: neutronLbaasSecGroupName,
184184
}

pkg/cloud/services/networking/securitygroups.go

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ import (
2525
"sigs.k8s.io/cluster-api-provider-openstack/pkg/record"
2626
)
2727

28-
// Exported Constants can be used to calculate securitygroupnames in other packages
28+
// export constants for use in loadbalancer-package
2929
const (
30-
secGroupPrefix string = "k8s"
31-
ControlPlaneSuffix string = "controlplane"
32-
WorkerSuffix string = "worker"
33-
BastionSuffix string = "bastion"
34-
NeutronLbaasSuffix string = "lbaas"
35-
remoteGroupIDSelf string = "self"
36-
securityGroupNameFormatString string = "%s-cluster-%s-secgroup-%s"
30+
SecGroupPrefix string = "k8s"
31+
NeutronLbaasSuffix string = "lbaas"
32+
)
33+
34+
const (
35+
controlPlaneSuffix string = "controlplane"
36+
workerSuffix string = "worker"
37+
bastionSuffix string = "bastion"
38+
remoteGroupIDSelf string = "self"
3739
)
3840

3941
var defaultRules = []infrav1.SecurityGroupRule{
@@ -65,20 +67,20 @@ func (s *Service) ReconcileSecurityGroups(clusterName string, openStackCluster *
6567
return nil
6668
}
6769

68-
secControlPlaneGroupName := GetSecurityGroupName(clusterName, ControlPlaneSuffix)
69-
secWorkerGroupName := GetSecurityGroupName(clusterName, WorkerSuffix)
70+
secControlPlaneGroupName := fmt.Sprintf("%s-cluster-%s-secgroup-%s", SecGroupPrefix, clusterName, controlPlaneSuffix)
71+
secWorkerGroupName := fmt.Sprintf("%s-cluster-%s-secgroup-%s", SecGroupPrefix, clusterName, workerSuffix)
7072
secGroupNames := map[string]string{
71-
ControlPlaneSuffix: secControlPlaneGroupName,
72-
WorkerSuffix: secWorkerGroupName,
73+
controlPlaneSuffix: secControlPlaneGroupName,
74+
workerSuffix: secWorkerGroupName,
7375
}
7476

7577
if openStackCluster.Spec.Bastion != nil && openStackCluster.Spec.Bastion.Enabled {
76-
secBastionGroupName := GetSecurityGroupName(clusterName, BastionSuffix)
77-
secGroupNames[BastionSuffix] = secBastionGroupName
78+
secBastionGroupName := fmt.Sprintf("%s-cluster-%s-secgroup-%s", SecGroupPrefix, clusterName, bastionSuffix)
79+
secGroupNames[bastionSuffix] = secBastionGroupName
7880
}
7981

8082
if openStackCluster.Spec.ManagedAPIServerLoadBalancer && !openStackCluster.Spec.UseOctavia {
81-
secLbaasGroupName := GetSecurityGroupName(clusterName, NeutronLbaasSuffix)
83+
secLbaasGroupName := fmt.Sprintf("%s-cluster-%s-secgroup-%s", SecGroupPrefix, clusterName, NeutronLbaasSuffix)
8284
secGroupNames[NeutronLbaasSuffix] = secLbaasGroupName
8385
}
8486

@@ -120,9 +122,9 @@ func (s *Service) ReconcileSecurityGroups(clusterName string, openStackCluster *
120122
}
121123
}
122124

123-
openStackCluster.Status.ControlPlaneSecurityGroup = observedSecGroups[ControlPlaneSuffix]
124-
openStackCluster.Status.WorkerSecurityGroup = observedSecGroups[WorkerSuffix]
125-
openStackCluster.Status.BastionSecurityGroup = observedSecGroups[BastionSuffix]
125+
openStackCluster.Status.ControlPlaneSecurityGroup = observedSecGroups[controlPlaneSuffix]
126+
openStackCluster.Status.WorkerSecurityGroup = observedSecGroups[workerSuffix]
127+
openStackCluster.Status.BastionSecurityGroup = observedSecGroups[bastionSuffix]
126128

127129
return nil
128130
}
@@ -139,11 +141,11 @@ func (s *Service) generateDesiredSecGroups(secGroupNames map[string]string, open
139141
return desiredSecGroups, err
140142
}
141143
switch i {
142-
case ControlPlaneSuffix:
144+
case controlPlaneSuffix:
143145
secControlPlaneGroupID = secGroup.ID
144-
case WorkerSuffix:
146+
case workerSuffix:
145147
secWorkerGroupID = secGroup.ID
146-
case BastionSuffix:
148+
case bastionSuffix:
147149
secBastionGroupID = secGroup.ID
148150
}
149151
}
@@ -317,8 +319,8 @@ func (s *Service) generateDesiredSecGroups(secGroupNames map[string]string, open
317319
},
318320
}...,
319321
)
320-
desiredSecGroups[BastionSuffix] = infrav1.SecurityGroup{
321-
Name: secGroupNames[BastionSuffix],
322+
desiredSecGroups[bastionSuffix] = infrav1.SecurityGroup{
323+
Name: secGroupNames[bastionSuffix],
322324
Rules: append(
323325
[]infrav1.SecurityGroupRule{
324326
{
@@ -374,13 +376,13 @@ func (s *Service) generateDesiredSecGroups(secGroupNames map[string]string, open
374376
}
375377
}
376378

377-
desiredSecGroups[ControlPlaneSuffix] = infrav1.SecurityGroup{
378-
Name: secGroupNames[ControlPlaneSuffix],
379+
desiredSecGroups[controlPlaneSuffix] = infrav1.SecurityGroup{
380+
Name: secGroupNames[controlPlaneSuffix],
379381
Rules: controlPlaneRules,
380382
}
381383

382-
desiredSecGroups[WorkerSuffix] = infrav1.SecurityGroup{
383-
Name: secGroupNames[WorkerSuffix],
384+
desiredSecGroups[workerSuffix] = infrav1.SecurityGroup{
385+
Name: secGroupNames[workerSuffix],
384386
Rules: workerRules,
385387
}
386388

@@ -577,8 +579,3 @@ func convertOSSecGroupRuleToConfigSecGroupRule(osSecGroupRule rules.SecGroupRule
577579
RemoteIPPrefix: osSecGroupRule.RemoteIPPrefix,
578580
}
579581
}
580-
581-
// GetSecurityGroupName Calculate name of securitygroup
582-
func GetSecurityGroupName(clusterName string, suffix string) string {
583-
return fmt.Sprintf(securityGroupNameFormatString, secGroupPrefix, clusterName, suffix)
584-
}

0 commit comments

Comments
 (0)