Skip to content

Commit fe34fda

Browse files
TruongHoangPhuLock8s-infra-cherrypick-robot
authored andcommitted
corrected lint requirements
1 parent ded3cf3 commit fe34fda

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

pkg/cloud/services/networking/securitygroups_rules.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -258,20 +258,19 @@ func getSGWorkerAllowAll(remoteGroupIDSelf, secControlPlaneGroupID string) []res
258258
// Permit ports that defined in openStackCluster.Spec.APIServerLoadBalancer.AdditionalPorts.
259259
func getSGControlPlaneAdditionalPorts(ports []int) []resolvedSecurityGroupRuleSpec {
260260
controlPlaneRules := []resolvedSecurityGroupRuleSpec{}
261-
262-
r := []resolvedSecurityGroupRuleSpec{
263-
{
264-
Description: "Additional ports",
265-
Direction: "ingress",
266-
EtherType: "IPv4",
267-
Protocol: "tcp",
268-
},
269-
}
261+
// Preallocate r with len(ports)
262+
r := make([]resolvedSecurityGroupRuleSpec, len(ports))
270263
for i, p := range ports {
271-
r[i].PortRangeMin = p
272-
r[i].PortRangeMax = p
273-
controlPlaneRules = append(controlPlaneRules, r...)
264+
r[i] = resolvedSecurityGroupRuleSpec{
265+
Description: "Additional port",
266+
Direction: "ingress",
267+
EtherType: "IPv4",
268+
Protocol: "tcp",
269+
PortRangeMin: p,
270+
PortRangeMax: p,
271+
}
274272
}
273+
controlPlaneRules = append(controlPlaneRules, r...)
275274
return controlPlaneRules
276275
}
277276

pkg/cloud/services/networking/securitygroups_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,3 +681,62 @@ func TestService_ReconcileSecurityGroups(t *testing.T) {
681681
})
682682
}
683683
}
684+
685+
func TestGetSGControlPlaneAdditionalPorts(t *testing.T) {
686+
tests := []struct {
687+
name string
688+
ports []int
689+
want []resolvedSecurityGroupRuleSpec
690+
}{
691+
{
692+
name: "no ports",
693+
ports: []int{},
694+
want: []resolvedSecurityGroupRuleSpec{},
695+
},
696+
{
697+
name: "single port",
698+
ports: []int{6443},
699+
want: []resolvedSecurityGroupRuleSpec{
700+
{
701+
Description: "Additional port",
702+
Direction: "ingress",
703+
EtherType: "IPv4",
704+
Protocol: "tcp",
705+
PortRangeMin: 6443,
706+
PortRangeMax: 6443,
707+
},
708+
},
709+
},
710+
{
711+
name: "multiple ports",
712+
ports: []int{80, 443},
713+
want: []resolvedSecurityGroupRuleSpec{
714+
{
715+
Description: "Additional port",
716+
Direction: "ingress",
717+
EtherType: "IPv4",
718+
Protocol: "tcp",
719+
PortRangeMin: 80,
720+
PortRangeMax: 80,
721+
},
722+
{
723+
Description: "Additional port",
724+
Direction: "ingress",
725+
EtherType: "IPv4",
726+
Protocol: "tcp",
727+
PortRangeMin: 443,
728+
PortRangeMax: 443,
729+
},
730+
},
731+
},
732+
}
733+
734+
for _, tt := range tests {
735+
t.Run(tt.name, func(t *testing.T) {
736+
got := getSGControlPlaneAdditionalPorts(tt.ports)
737+
if !reflect.DeepEqual(got, tt.want) {
738+
t.Errorf("getSGControlPlaneAdditionalPorts() = %v, want %v", got, tt.want)
739+
}
740+
})
741+
}
742+
}

0 commit comments

Comments
 (0)