Skip to content
This repository was archived by the owner on Mar 22, 2018. It is now read-only.

Commit 3d06da7

Browse files
committed
Add EnsureOldSecurityGroupDeleted to delete old security group
Consider the migration from the old security group name to the new security group name, we need delete the old security group. At V1.10, we can assume everyone is using the new security group names and remove this code.
1 parent ff8c50e commit 3d06da7

1 file changed

Lines changed: 126 additions & 37 deletions

File tree

pkg/cloudprovider/providers/openstack/openstack_loadbalancer.go

Lines changed: 126 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,14 @@ func (lbaas *LbaasV2) EnsureLoadBalancer(clusterName string, apiService *v1.Serv
874874
_ = lbaas.EnsureLoadBalancerDeleted(clusterName, apiService)
875875
return status, err
876876
}
877+
878+
// delete the old Security Group for the service
879+
// Related to #53764
880+
// TODO(FengyunPan): Remove it at V1.10
881+
err = lbaas.EnsureOldSecurityGroupDeleted(clusterName, apiService)
882+
if err != nil {
883+
return status, fmt.Errorf("Failed to delete the Security Group for loadbalancer service %s/%s: %v", apiService.Namespace, apiService.Name, err)
884+
}
877885
}
878886

879887
return status, nil
@@ -921,7 +929,7 @@ func (lbaas *LbaasV2) ensureSecurityGroup(clusterName string, apiService *v1.Ser
921929
// create security group
922930
lbSecGroupCreateOpts := groups.CreateOpts{
923931
Name: getSecurityGroupName(apiService),
924-
Description: fmt.Sprintf("Securty Group for %s/%s Service LoadBalancer in cluster %s", apiService.Namespace, apiService.Name, clusterName),
932+
Description: fmt.Sprintf("Security Group for %s/%s Service LoadBalancer in cluster %s", apiService.Namespace, apiService.Name, clusterName),
925933
}
926934

927935
lbSecGroup, err := groups.Create(lbaas.network, lbSecGroupCreateOpts).Extract()
@@ -1180,7 +1188,7 @@ func (lbaas *LbaasV2) UpdateLoadBalancer(clusterName string, service *v1.Service
11801188
if lbaas.opts.ManageSecurityGroups {
11811189
err := lbaas.updateSecurityGroup(clusterName, service, nodes, loadbalancer)
11821190
if err != nil {
1183-
return fmt.Errorf("failed to update Securty Group for loadbalancer service %s/%s: %v", service.Namespace, service.Name, err)
1191+
return fmt.Errorf("failed to update Security Group for loadbalancer service %s/%s: %v", service.Namespace, service.Name, err)
11841192
}
11851193
}
11861194

@@ -1374,50 +1382,131 @@ func (lbaas *LbaasV2) EnsureLoadBalancerDeleted(clusterName string, service *v1.
13741382

13751383
// Delete the Security Group
13761384
if lbaas.opts.ManageSecurityGroups {
1377-
// Generate Name
1378-
lbSecGroupName := getSecurityGroupName(service)
1379-
lbSecGroupID, err := groups.IDFromName(lbaas.network, lbSecGroupName)
1385+
err := lbaas.EnsureSecurityGroupDeleted(clusterName, service)
13801386
if err != nil {
1381-
// check whether security group does not exist
1382-
_, ok := err.(*gophercloud.ErrResourceNotFound)
1383-
if ok {
1384-
// It is OK when the security group has been deleted by others.
1385-
return nil
1386-
} else {
1387-
return fmt.Errorf("error occurred finding security group: %s: %v", lbSecGroupName, err)
1388-
}
1387+
return fmt.Errorf("Failed to delete Security Group for loadbalancer service %s/%s: %v", service.Namespace, service.Name, err)
13891388
}
13901389

1391-
lbSecGroup := groups.Delete(lbaas.network, lbSecGroupID)
1392-
if lbSecGroup.Err != nil && !isNotFound(lbSecGroup.Err) {
1393-
return lbSecGroup.Err
1390+
// delete the old Security Group for the service
1391+
// Related to #53764
1392+
// TODO(FengyunPan): Remove it at V1.10
1393+
err = lbaas.EnsureOldSecurityGroupDeleted(clusterName, service)
1394+
if err != nil {
1395+
return fmt.Errorf("Failed to delete the Security Group for loadbalancer service %s/%s: %v", service.Namespace, service.Name, err)
13941396
}
1397+
}
1398+
1399+
return nil
1400+
}
13951401

1396-
if len(lbaas.opts.NodeSecurityGroupIDs) == 0 {
1397-
// Just happen when nodes have not Security Group, or should not happen
1398-
// UpdateLoadBalancer and EnsureLoadBalancer can set lbaas.opts.NodeSecurityGroupIDs when it is empty
1399-
// And service controller call UpdateLoadBalancer to set lbaas.opts.NodeSecurityGroupIDs when controller manager service is restarted.
1400-
glog.Warningf("Can not find node-security-group from all the nodes of this cluser when delete loadbalancer service %s/%s",
1401-
service.Namespace, service.Name)
1402+
// EnsureSecurityGroupDeleted deleting security group for specific loadbalancer service.
1403+
func (lbaas *LbaasV2) EnsureSecurityGroupDeleted(clusterName string, service *v1.Service) error {
1404+
// Generate Name
1405+
lbSecGroupName := getSecurityGroupName(service)
1406+
lbSecGroupID, err := groups.IDFromName(lbaas.network, lbSecGroupName)
1407+
if err != nil {
1408+
// check whether security group does not exist
1409+
_, ok := err.(*gophercloud.ErrResourceNotFound)
1410+
if ok {
1411+
// It is OK when the security group has been deleted by others.
1412+
return nil
14021413
} else {
1403-
// Delete the rules in the Node Security Group
1404-
for _, nodeSecurityGroupID := range lbaas.opts.NodeSecurityGroupIDs {
1405-
opts := rules.ListOpts{
1406-
SecGroupID: nodeSecurityGroupID,
1407-
RemoteGroupID: lbSecGroupID,
1408-
}
1409-
secGroupRules, err := getSecurityGroupRules(lbaas.network, opts)
1414+
return fmt.Errorf("Error occurred finding security group: %s: %v", lbSecGroupName, err)
1415+
}
1416+
}
14101417

1411-
if err != nil && !isNotFound(err) {
1412-
msg := fmt.Sprintf("Error finding rules for remote group id %s in security group id %s: %v", lbSecGroupID, nodeSecurityGroupID, err)
1413-
return fmt.Errorf(msg)
1418+
lbSecGroup := groups.Delete(lbaas.network, lbSecGroupID)
1419+
if lbSecGroup.Err != nil && !isNotFound(lbSecGroup.Err) {
1420+
return lbSecGroup.Err
1421+
}
1422+
1423+
if len(lbaas.opts.NodeSecurityGroupIDs) == 0 {
1424+
// Just happen when nodes have not Security Group, or should not happen
1425+
// UpdateLoadBalancer and EnsureLoadBalancer can set lbaas.opts.NodeSecurityGroupIDs when it is empty
1426+
// And service controller call UpdateLoadBalancer to set lbaas.opts.NodeSecurityGroupIDs when controller manager service is restarted.
1427+
glog.Warningf("Can not find node-security-group from all the nodes of this cluster when delete loadbalancer service %s/%s",
1428+
service.Namespace, service.Name)
1429+
} else {
1430+
// Delete the rules in the Node Security Group
1431+
for _, nodeSecurityGroupID := range lbaas.opts.NodeSecurityGroupIDs {
1432+
opts := rules.ListOpts{
1433+
SecGroupID: nodeSecurityGroupID,
1434+
RemoteGroupID: lbSecGroupID,
1435+
}
1436+
secGroupRules, err := getSecurityGroupRules(lbaas.network, opts)
1437+
1438+
if err != nil && !isNotFound(err) {
1439+
msg := fmt.Sprintf("Error finding rules for remote group id %s in security group id %s: %v", lbSecGroupID, nodeSecurityGroupID, err)
1440+
return fmt.Errorf(msg)
1441+
}
1442+
1443+
for _, rule := range secGroupRules {
1444+
res := rules.Delete(lbaas.network, rule.ID)
1445+
if res.Err != nil && !isNotFound(res.Err) {
1446+
return fmt.Errorf("Error occurred deleting security group rule: %s: %v", rule.ID, res.Err)
14141447
}
1448+
}
1449+
}
1450+
}
14151451

1416-
for _, rule := range secGroupRules {
1417-
res := rules.Delete(lbaas.network, rule.ID)
1418-
if res.Err != nil && !isNotFound(res.Err) {
1419-
return fmt.Errorf("error occurred deleting security group rule: %s: %v", rule.ID, res.Err)
1420-
}
1452+
return nil
1453+
}
1454+
1455+
// getOldSecurityGroupName is used to get the old security group name
1456+
// Related to #53764
1457+
// TODO(FengyunPan): Remove it at V1.10
1458+
func getOldSecurityGroupName(clusterName string, service *v1.Service) string {
1459+
return fmt.Sprintf("lb-sg-%s-%v", clusterName, service.Name)
1460+
}
1461+
1462+
// EnsureOldSecurityGroupDeleted deleting old security group for specific loadbalancer service.
1463+
// Related to #53764
1464+
// TODO(FengyunPan): Remove it at V1.10
1465+
func (lbaas *LbaasV2) EnsureOldSecurityGroupDeleted(clusterName string, service *v1.Service) error {
1466+
glog.V(4).Infof("EnsureOldSecurityGroupDeleted(%v, %v)", clusterName, service)
1467+
// Generate Name
1468+
lbSecGroupName := getOldSecurityGroupName(clusterName, service)
1469+
lbSecGroupID, err := groups.IDFromName(lbaas.network, lbSecGroupName)
1470+
if err != nil {
1471+
// check whether security group does not exist
1472+
_, ok := err.(*gophercloud.ErrResourceNotFound)
1473+
if ok {
1474+
// It is OK when the security group has been deleted by others.
1475+
return nil
1476+
} else {
1477+
return fmt.Errorf("Error occurred finding security group: %s: %v", lbSecGroupName, err)
1478+
}
1479+
}
1480+
1481+
lbSecGroup := groups.Delete(lbaas.network, lbSecGroupID)
1482+
if lbSecGroup.Err != nil && !isNotFound(lbSecGroup.Err) {
1483+
return lbSecGroup.Err
1484+
}
1485+
1486+
if len(lbaas.opts.NodeSecurityGroupIDs) == 0 {
1487+
// Just happen when nodes have not Security Group, or should not happen
1488+
// UpdateLoadBalancer and EnsureLoadBalancer can set lbaas.opts.NodeSecurityGroupIDs when it is empty
1489+
// And service controller call UpdateLoadBalancer to set lbaas.opts.NodeSecurityGroupIDs when controller manager service is restarted.
1490+
glog.Warningf("Can not find node-security-group from all the nodes of this cluster when delete loadbalancer service %s/%s",
1491+
service.Namespace, service.Name)
1492+
} else {
1493+
// Delete the rules in the Node Security Group
1494+
for _, nodeSecurityGroupID := range lbaas.opts.NodeSecurityGroupIDs {
1495+
opts := rules.ListOpts{
1496+
SecGroupID: nodeSecurityGroupID,
1497+
RemoteGroupID: lbSecGroupID,
1498+
}
1499+
secGroupRules, err := getSecurityGroupRules(lbaas.network, opts)
1500+
1501+
if err != nil && !isNotFound(err) {
1502+
msg := fmt.Sprintf("Error finding rules for remote group id %s in security group id %s: %v", lbSecGroupID, nodeSecurityGroupID, err)
1503+
return fmt.Errorf(msg)
1504+
}
1505+
1506+
for _, rule := range secGroupRules {
1507+
res := rules.Delete(lbaas.network, rule.ID)
1508+
if res.Err != nil && !isNotFound(res.Err) {
1509+
return fmt.Errorf("Error occurred deleting security group rule: %s: %v", rule.ID, res.Err)
14211510
}
14221511
}
14231512
}

0 commit comments

Comments
 (0)