Skip to content

Commit 4b76bbe

Browse files
dulekmandre
authored andcommitted
[occm] Delete sgs on reconfiguration (kubernetes#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 e53d8ef commit 4b76bbe

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
@@ -797,21 +797,6 @@ func disassociateSecurityGroupForLB(network *gophercloud.ServiceClient, sg strin
797797
return nil
798798
}
799799

800-
// isSecurityGroupNotFound return true while 'err' is object of gophercloud.ErrResourceNotFound
801-
func isSecurityGroupNotFound(err error) bool {
802-
errType := reflect.TypeOf(err).String()
803-
errTypeSlice := strings.Split(errType, ".")
804-
errTypeValue := ""
805-
if len(errTypeSlice) != 0 {
806-
errTypeValue = errTypeSlice[len(errTypeSlice)-1]
807-
}
808-
if errTypeValue == "ErrResourceNotFound" {
809-
return true
810-
}
811-
812-
return false
813-
}
814-
815800
// deleteListeners deletes listeners and its default pool.
816801
func (lbaas *LbaasV2) deleteListeners(lbID string, listenerList []listeners.Listener) error {
817802
for _, listener := range listenerList {
@@ -2059,6 +2044,12 @@ func (lbaas *LbaasV2) ensureOctaviaLoadBalancer(ctx context.Context, clusterName
20592044
if err != nil {
20602045
return status, fmt.Errorf("failed when reconciling security groups for LB service %v/%v: %v", service.Namespace, service.Name, err)
20612046
}
2047+
} else {
2048+
// Attempt to delete the SG if `manage-security-groups` is disabled. When CPO is reconfigured to enable it we
2049+
// will reconcile the LB and create the SG. This is to make sure it works the same in the opposite direction.
2050+
if err := lbaas.EnsureSecurityGroupDeleted(clusterName, service); err != nil {
2051+
return status, err
2052+
}
20622053
}
20632054

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

21942188
return nil
21952189
}
@@ -2258,7 +2252,7 @@ func (lbaas *LbaasV2) ensureAndUpdateOctaviaSecurityGroup(clusterName string, ap
22582252
lbSecGroupID, err := secgroups.IDFromName(lbaas.network, lbSecGroupName)
22592253
if err != nil {
22602254
// If the security group of LB not exist, create it later
2261-
if isSecurityGroupNotFound(err) {
2255+
if cpoerrors.IsNotFound(err) {
22622256
lbSecGroupID = ""
22632257
} else {
22642258
return fmt.Errorf("error occurred finding security group: %s: %v", lbSecGroupName, err)
@@ -2558,11 +2552,10 @@ func (lbaas *LbaasV2) ensureLoadBalancerDeleted(ctx context.Context, clusterName
25582552
klog.InfoS("Updated load balancer tags", "lbID", loadbalancer.ID)
25592553
}
25602554

2561-
// Delete the Security Group
2562-
if lbaas.opts.ManageSecurityGroups {
2563-
if err := lbaas.EnsureSecurityGroupDeleted(clusterName, service); err != nil {
2564-
return err
2565-
}
2555+
// Delete the Security Group. We're doing that even if `manage-security-groups` is disabled to make sure we don't
2556+
// orphan created SGs even if CPO got reconfigured.
2557+
if err := lbaas.EnsureSecurityGroupDeleted(clusterName, service); err != nil {
2558+
return err
25662559
}
25672560

25682561
return nil
@@ -2574,7 +2567,7 @@ func (lbaas *LbaasV2) EnsureSecurityGroupDeleted(_ string, service *corev1.Servi
25742567
lbSecGroupName := getSecurityGroupName(service)
25752568
lbSecGroupID, err := secgroups.IDFromName(lbaas.network, lbSecGroupName)
25762569
if err != nil {
2577-
if isSecurityGroupNotFound(err) {
2570+
if cpoerrors.IsNotFound(err) {
25782571
// It is OK when the security group has been deleted by others.
25792572
return nil
25802573
}

0 commit comments

Comments
 (0)