Skip to content

Commit 3a28a4d

Browse files
authored
Merge pull request #4991 from alexander-demicev/fixlbingress
🐛Apply the same set of rules for processing all custom ingress rules
2 parents 1564094 + 666c445 commit 3a28a4d

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

pkg/cloud/services/securitygroup/securitygroups.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -592,24 +592,7 @@ func (s *Service) getSecurityGroupIngressRules(role infrav1.SecurityGroupRole) (
592592
rules = append(rules, s.defaultSSHIngressRule(s.scope.SecurityGroups()[infrav1.SecurityGroupBastion].ID))
593593
}
594594

595-
ingressRules := s.scope.AdditionalControlPlaneIngressRules()
596-
for i := range ingressRules {
597-
if len(ingressRules[i].CidrBlocks) != 0 || len(ingressRules[i].IPv6CidrBlocks) != 0 { // don't set source security group if cidr blocks are set
598-
continue
599-
}
600-
601-
if len(ingressRules[i].SourceSecurityGroupIDs) == 0 && len(ingressRules[i].SourceSecurityGroupRoles) == 0 { // if the rule doesn't have a source security group, use the control plane security group
602-
ingressRules[i].SourceSecurityGroupIDs = []string{s.scope.SecurityGroups()[infrav1.SecurityGroupControlPlane].ID}
603-
continue
604-
}
605-
606-
securityGroupIDs := sets.New[string](ingressRules[i].SourceSecurityGroupIDs...)
607-
for _, sourceSGRole := range ingressRules[i].SourceSecurityGroupRoles {
608-
securityGroupIDs.Insert(s.scope.SecurityGroups()[sourceSGRole].ID)
609-
}
610-
ingressRules[i].SourceSecurityGroupIDs = sets.List[string](securityGroupIDs)
611-
}
612-
rules = append(rules, ingressRules...)
595+
rules = append(rules, s.processIngressRulesSGs(s.scope.AdditionalControlPlaneIngressRules())...)
613596

614597
return append(cniRules, rules...), nil
615598

@@ -656,7 +639,7 @@ func (s *Service) getSecurityGroupIngressRules(role infrav1.SecurityGroupRole) (
656639
return infrav1.IngressRules{}, nil
657640
case infrav1.SecurityGroupAPIServerLB:
658641
kubeletRules := s.getIngressRulesToAllowKubeletToAccessTheControlPlaneLB()
659-
customIngressRules := s.getControlPlaneLBIngressRules()
642+
customIngressRules := s.processIngressRulesSGs(s.getControlPlaneLBIngressRules())
660643
rulesToApply := customIngressRules.Difference(kubeletRules)
661644
return append(kubeletRules, rulesToApply...), nil
662645
case infrav1.SecurityGroupLB:
@@ -980,3 +963,30 @@ func (s *Service) getIngressRuleToAllowVPCCidrInTheAPIServer() infrav1.IngressRu
980963
},
981964
}
982965
}
966+
967+
func (s *Service) processIngressRulesSGs(ingressRules []infrav1.IngressRule) infrav1.IngressRules {
968+
output := []infrav1.IngressRule{}
969+
970+
for _, rule := range ingressRules {
971+
if len(rule.CidrBlocks) != 0 || len(rule.IPv6CidrBlocks) != 0 { // don't set source security group if cidr blocks are set
972+
output = append(output, rule)
973+
continue
974+
}
975+
976+
if len(rule.SourceSecurityGroupIDs) == 0 && len(rule.SourceSecurityGroupRoles) == 0 { // if the rule doesn't have a source security group, use the control plane security group
977+
rule.SourceSecurityGroupIDs = []string{s.scope.SecurityGroups()[infrav1.SecurityGroupControlPlane].ID}
978+
output = append(output, rule)
979+
continue
980+
}
981+
982+
securityGroupIDs := sets.New(rule.SourceSecurityGroupIDs...)
983+
for _, sourceSGRole := range rule.SourceSecurityGroupRoles {
984+
securityGroupIDs.Insert(s.scope.SecurityGroups()[sourceSGRole].ID)
985+
}
986+
rule.SourceSecurityGroupIDs = sets.List(securityGroupIDs)
987+
988+
output = append(output, rule)
989+
}
990+
991+
return output
992+
}

pkg/cloud/services/securitygroup/securitygroups_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ func TestAdditionalControlPlaneSecurityGroup(t *testing.T) {
10671067
t.Fatalf("Expected to port %d, got %d", tc.expectedAdditionalIngresRule.ToPort, r.ToPort)
10681068
}
10691069

1070-
if !sets.New[string](tc.expectedAdditionalIngresRule.SourceSecurityGroupIDs...).Equal(sets.New[string](tc.expectedAdditionalIngresRule.SourceSecurityGroupIDs...)) {
1070+
if !sets.New(tc.expectedAdditionalIngresRule.SourceSecurityGroupIDs...).Equal(sets.New(r.SourceSecurityGroupIDs...)) {
10711071
t.Fatalf("Expected source security group IDs %v, got %v", tc.expectedAdditionalIngresRule.SourceSecurityGroupIDs, r.SourceSecurityGroupIDs)
10721072
}
10731073
}

0 commit comments

Comments
 (0)