Skip to content

Commit 942db28

Browse files
authored
[occm] Delete sgs on reconfiguration (#2305)
* Use errors.IsNotFound() SG management code used its own function to check if `err` is a 404. We have a dedicated function for that, let's use it there too. * Make sure SGs are deleted when CPO is reconfigured If we reconfigure CPO to set `manage-security-groups=true`, it will correctly reconcile LBs to make sure SGs are created and associated with nodes. This commit makes sure that SGs are properly deleted when CPO is reconfigured in opposite direction, with `manage-security-groups=false`.
1 parent b229f84 commit 942db28

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

pkg/openstack/loadbalancer.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -801,21 +801,6 @@ func disassociateSecurityGroupForLB(network *gophercloud.ServiceClient, sg strin
801801
return nil
802802
}
803803

804-
// isSecurityGroupNotFound return true while 'err' is object of gophercloud.ErrResourceNotFound
805-
func isSecurityGroupNotFound(err error) bool {
806-
errType := reflect.TypeOf(err).String()
807-
errTypeSlice := strings.Split(errType, ".")
808-
errTypeValue := ""
809-
if len(errTypeSlice) != 0 {
810-
errTypeValue = errTypeSlice[len(errTypeSlice)-1]
811-
}
812-
if errTypeValue == "ErrResourceNotFound" {
813-
return true
814-
}
815-
816-
return false
817-
}
818-
819804
// deleteListeners deletes listeners and its default pool.
820805
func (lbaas *LbaasV2) deleteListeners(lbID string, listenerList []listeners.Listener) error {
821806
for _, listener := range listenerList {
@@ -2028,6 +2013,12 @@ func (lbaas *LbaasV2) ensureOctaviaLoadBalancer(ctx context.Context, clusterName
20282013
if err != nil {
20292014
return status, fmt.Errorf("failed when reconciling security groups for LB service %v/%v: %v", service.Namespace, service.Name, err)
20302015
}
2016+
} else {
2017+
// Attempt to delete the SG if `manage-security-groups` is disabled. When CPO is reconfigured to enable it we
2018+
// will reconcile the LB and create the SG. This is to make sure it works the same in the opposite direction.
2019+
if err := lbaas.EnsureSecurityGroupDeleted(clusterName, service); err != nil {
2020+
return status, err
2021+
}
20312022
}
20322023

20332024
return status, nil
@@ -2154,6 +2145,9 @@ func (lbaas *LbaasV2) updateOctaviaLoadBalancer(ctx context.Context, clusterName
21542145
return fmt.Errorf("failed to update Security Group for loadbalancer service %s: %v", serviceName, err)
21552146
}
21562147
}
2148+
// We don't try to lookup and delete the SG here when `manage-security-group=false` as `UpdateLoadBalancer()` is
2149+
// only called on changes to the list of the Nodes. Deletion of the SG on reconfiguration will be handled by
2150+
// EnsureLoadBalancer() that is the true LB reconcile function.
21572151

21582152
return nil
21592153
}
@@ -2222,7 +2216,7 @@ func (lbaas *LbaasV2) ensureAndUpdateOctaviaSecurityGroup(clusterName string, ap
22222216
lbSecGroupID, err := secgroups.IDFromName(lbaas.network, lbSecGroupName)
22232217
if err != nil {
22242218
// If the security group of LB not exist, create it later
2225-
if isSecurityGroupNotFound(err) {
2219+
if cpoerrors.IsNotFound(err) {
22262220
lbSecGroupID = ""
22272221
} else {
22282222
return fmt.Errorf("error occurred finding security group: %s: %v", lbSecGroupName, err)
@@ -2522,11 +2516,10 @@ func (lbaas *LbaasV2) ensureLoadBalancerDeleted(ctx context.Context, clusterName
25222516
klog.InfoS("Updated load balancer tags", "lbID", loadbalancer.ID)
25232517
}
25242518

2525-
// Delete the Security Group
2526-
if lbaas.opts.ManageSecurityGroups {
2527-
if err := lbaas.EnsureSecurityGroupDeleted(clusterName, service); err != nil {
2528-
return err
2529-
}
2519+
// Delete the Security Group. We're doing that even if `manage-security-groups` is disabled to make sure we don't
2520+
// orphan created SGs even if CPO got reconfigured.
2521+
if err := lbaas.EnsureSecurityGroupDeleted(clusterName, service); err != nil {
2522+
return err
25302523
}
25312524

25322525
return nil
@@ -2538,7 +2531,7 @@ func (lbaas *LbaasV2) EnsureSecurityGroupDeleted(_ string, service *corev1.Servi
25382531
lbSecGroupName := getSecurityGroupName(service)
25392532
lbSecGroupID, err := secgroups.IDFromName(lbaas.network, lbSecGroupName)
25402533
if err != nil {
2541-
if isSecurityGroupNotFound(err) {
2534+
if cpoerrors.IsNotFound(err) {
25422535
// It is OK when the security group has been deleted by others.
25432536
return nil
25442537
}

0 commit comments

Comments
 (0)