Skip to content

Commit 7ae68cd

Browse files
pranavsriram8YashwantGohokar
authored andcommitted
fix security list rule clean up flow for OCI loadbalancer delete calls
1 parent 24382f6 commit 7ae68cd

File tree

4 files changed

+53
-14
lines changed

4 files changed

+53
-14
lines changed

pkg/cloudprovider/providers/oci/load_balancer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,7 @@ func (cp *CloudProvider) cleanupSecurityRulesForLoadBalancerDelete(lb *client.Ge
14211421
return errors.Wrapf(err, "delete security rules for listener %q on load balancer %q", listenerName, name)
14221422
}
14231423

1424+
logger.Infof("Security rule management mode %s", securityRuleManagerMode)
14241425
if securityRuleManagerMode == ManagementModeAll || securityRuleManagerMode == ManagementModeFrontend {
14251426
if err = securityListManager.Delete(ctx, lbSubnets, nodeSubnets, ports, sourceCIDRs, isPreserveSource); err != nil {
14261427
logger.With(zap.Error(err)).Errorf("Failed to delete security rules for listener %q on load balancer %q", listenerName, name)

pkg/cloudprovider/providers/oci/load_balancer_security_lists.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ func getNodeIngressRules(
382382
"source", *rule.Source,
383383
"destinationPortRangeMin", *rule.TcpOptions.DestinationPortRange.Min,
384384
"destinationPortRangeMax", *rule.TcpOptions.DestinationPortRange.Max,
385-
).Debug("Deleting load balancer ingres security rule")
385+
).Debug("Deleting node ingress security rule")
386386
continue
387387
}
388388
}
@@ -512,7 +512,7 @@ func getLoadBalancerIngressRules(
512512
"source", *rule.Source,
513513
"destinationPortRangeMin", *rule.TcpOptions.DestinationPortRange.Min,
514514
"destinationPortRangeMax", *rule.TcpOptions.DestinationPortRange.Max,
515-
).Debug("Deleting load balancer ingres security rule")
515+
).Debug("Deleting load balancer ingress security rule")
516516
}
517517

518518
if desired.Len() == 0 {
@@ -558,7 +558,7 @@ func getLoadBalancerEgressRules(
558558
"destination", *rule.Destination,
559559
"destinationPortRangeMin", *rule.TcpOptions.DestinationPortRange.Min,
560560
"destinationPortRangeMax", *rule.TcpOptions.DestinationPortRange.Max,
561-
).Debug("Deleting load balancer ingres security rule")
561+
).Debug("Deleting load balancer egress security rule")
562562
continue
563563
}
564564

@@ -655,15 +655,20 @@ func makeIngressSecurityRule(cidrBlock string, port int) core.IngressSecurityRul
655655

656656
func portInUse(serviceLister listersv1.ServiceLister, port int32) (bool, error) {
657657
serviceList, err := serviceLister.List(labels.Everything())
658+
658659
if err != nil {
659660
return false, err
660661
}
661662
for _, service := range serviceList {
662-
if service.Spec.Type == api.ServiceTypeLoadBalancer {
663-
for _, p := range service.Spec.Ports {
664-
if p.Port == port {
665-
return true, nil
666-
}
663+
if service.DeletionTimestamp != nil {
664+
continue
665+
}
666+
if service.Spec.Type != api.ServiceTypeLoadBalancer {
667+
continue
668+
}
669+
for _, p := range service.Spec.Ports {
670+
if p.Port == port {
671+
return true, nil
667672
}
668673
}
669674
}

pkg/cloudprovider/providers/oci/load_balancer_spec.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,17 +421,17 @@ func NewLBSpec(logger *zap.SugaredLogger, svc *v1.Service, nodes []*v1.Node, sub
421421

422422
func getSecurityListManagementMode(svc *v1.Service) (string, error) {
423423
lbType := getLoadBalancerType(svc)
424+
logger := *zap.L().Sugar()
424425
knownSecListModes := map[string]struct{}{
425426
ManagementModeAll: struct{}{},
426427
ManagementModeNone: struct{}{},
427428
ManagementModeFrontend: struct{}{},
428429
}
429-
430+
annotationExists := false
431+
var annotationValue string
430432
switch lbType {
431433
case NLB:
432434
{
433-
annotationExists := false
434-
var annotationValue string
435435
annotationValue, annotationExists = svc.Annotations[ServiceAnnotationNetworkLoadBalancerSecurityListManagementMode]
436436
if !annotationExists {
437437
return ManagementModeNone, nil
@@ -442,6 +442,14 @@ func getSecurityListManagementMode(svc *v1.Service) (string, error) {
442442
return svc.Annotations[ServiceAnnotationNetworkLoadBalancerSecurityListManagementMode], nil
443443
}
444444
default:
445+
annotationValue, annotationExists = svc.Annotations[ServiceAnnotationLoadBalancerSecurityListManagementMode]
446+
if !annotationExists {
447+
return ManagementModeAll, nil
448+
}
449+
if _, ok := knownSecListModes[annotationValue]; !ok {
450+
logger.Infof("invalid value: %s provided for annotation: %s; using default All", annotationValue, ServiceAnnotationLoadBalancerSecurityListManagementMode)
451+
return ManagementModeAll, nil
452+
}
445453
return svc.Annotations[ServiceAnnotationLoadBalancerSecurityListManagementMode], nil
446454
}
447455
}

pkg/cloudprovider/providers/oci/load_balancer_spec_test.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4007,13 +4007,23 @@ func Test_getSecurityListManagementMode(t *testing.T) {
40074007
service *v1.Service
40084008
expected string
40094009
}{
4010-
"defaults": {
4010+
"defaults - lb": {
40114011
service: &v1.Service{
40124012
ObjectMeta: metav1.ObjectMeta{
40134013
Annotations: map[string]string{},
40144014
},
40154015
},
4016-
expected: "",
4016+
expected: "All",
4017+
},
4018+
"defaults - nlb": {
4019+
service: &v1.Service{
4020+
ObjectMeta: metav1.ObjectMeta{
4021+
Annotations: map[string]string{
4022+
ServiceAnnotationLoadBalancerType: "nlb",
4023+
},
4024+
},
4025+
},
4026+
expected: "None",
40174027
},
40184028
"lb mode None": {
40194029
service: &v1.Service{
@@ -4115,7 +4125,22 @@ func Test_getRuleManagementMode(t *testing.T) {
41154125
Annotations: map[string]string{},
41164126
},
41174127
},
4118-
expected: "",
4128+
expected: "All",
4129+
nsg: &ManagedNetworkSecurityGroup{
4130+
nsgRuleManagementMode: ManagementModeNone,
4131+
frontendNsgId: "",
4132+
backendNsgId: []string{},
4133+
},
4134+
},
4135+
"defaults - nlb": {
4136+
service: &v1.Service{
4137+
ObjectMeta: metav1.ObjectMeta{
4138+
Annotations: map[string]string{
4139+
ServiceAnnotationLoadBalancerType: "nlb",
4140+
},
4141+
},
4142+
},
4143+
expected: "None",
41194144
nsg: &ManagedNetworkSecurityGroup{
41204145
nsgRuleManagementMode: ManagementModeNone,
41214146
frontendNsgId: "",

0 commit comments

Comments
 (0)