@@ -292,8 +292,14 @@ func popMember(members []v2pools.Member, addr string, port int) []v2pools.Member
292292 return members
293293}
294294
295- func getSecurityGroupName (clusterName string , service * v1.Service ) string {
296- return fmt .Sprintf ("lb-sg-%s-%s-%s" , clusterName , service .Namespace , service .Name )
295+ func getSecurityGroupName (service * v1.Service ) string {
296+ securityGroupName := fmt .Sprintf ("lb-sg-%s-%s-%s" , service .UID , service .Namespace , service .Name )
297+ //OpenStack requires that the name of a security group is shorter than 255 bytes.
298+ if len (securityGroupName ) > 255 {
299+ securityGroupName = securityGroupName [:255 ]
300+ }
301+
302+ return securityGroupName
297303}
298304
299305func getSecurityGroupRules (client * gophercloud.ServiceClient , opts rules.ListOpts ) ([]rules.SecGroupRule , error ) {
@@ -868,6 +874,14 @@ func (lbaas *LbaasV2) EnsureLoadBalancer(clusterName string, apiService *v1.Serv
868874 _ = lbaas .EnsureLoadBalancerDeleted (clusterName , apiService )
869875 return status , err
870876 }
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+ }
871885 }
872886
873887 return status , nil
@@ -899,7 +913,7 @@ func (lbaas *LbaasV2) ensureSecurityGroup(clusterName string, apiService *v1.Ser
899913 }
900914
901915 // ensure security group for LB
902- lbSecGroupName := getSecurityGroupName (clusterName , apiService )
916+ lbSecGroupName := getSecurityGroupName (apiService )
903917 lbSecGroupID , err := groups .IDFromName (lbaas .network , lbSecGroupName )
904918 if err != nil {
905919 // check whether security group does not exist
@@ -914,8 +928,8 @@ func (lbaas *LbaasV2) ensureSecurityGroup(clusterName string, apiService *v1.Ser
914928 if len (lbSecGroupID ) == 0 {
915929 // create security group
916930 lbSecGroupCreateOpts := groups.CreateOpts {
917- Name : getSecurityGroupName (clusterName , apiService ),
918- Description : fmt .Sprintf ("Securty Group for loadbalancer service %s/%s" , apiService .Namespace , apiService .Name ),
931+ Name : getSecurityGroupName (apiService ),
932+ Description : fmt .Sprintf ("Security Group for %s/%s Service LoadBalancer in cluster %s " , apiService .Namespace , apiService .Name , clusterName ),
919933 }
920934
921935 lbSecGroup , err := groups .Create (lbaas .network , lbSecGroupCreateOpts ).Extract ()
@@ -1174,7 +1188,7 @@ func (lbaas *LbaasV2) UpdateLoadBalancer(clusterName string, service *v1.Service
11741188 if lbaas .opts .ManageSecurityGroups {
11751189 err := lbaas .updateSecurityGroup (clusterName , service , nodes , loadbalancer )
11761190 if err != nil {
1177- 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 )
11781192 }
11791193 }
11801194
@@ -1197,7 +1211,7 @@ func (lbaas *LbaasV2) updateSecurityGroup(clusterName string, apiService *v1.Ser
11971211 removals := original .Difference (current )
11981212
11991213 // Generate Name
1200- lbSecGroupName := getSecurityGroupName (clusterName , apiService )
1214+ lbSecGroupName := getSecurityGroupName (apiService )
12011215 lbSecGroupID , err := groups .IDFromName (lbaas .network , lbSecGroupName )
12021216 if err != nil {
12031217 return fmt .Errorf ("error occurred finding security group: %s: %v" , lbSecGroupName , err )
@@ -1368,50 +1382,131 @@ func (lbaas *LbaasV2) EnsureLoadBalancerDeleted(clusterName string, service *v1.
13681382
13691383 // Delete the Security Group
13701384 if lbaas .opts .ManageSecurityGroups {
1371- // Generate Name
1372- lbSecGroupName := getSecurityGroupName (clusterName , service )
1373- lbSecGroupID , err := groups .IDFromName (lbaas .network , lbSecGroupName )
1385+ err := lbaas .EnsureSecurityGroupDeleted (clusterName , service )
13741386 if err != nil {
1375- // check whether security group does not exist
1376- _ , ok := err .(* gophercloud.ErrResourceNotFound )
1377- if ok {
1378- // It is OK when the security group has been deleted by others.
1379- return nil
1380- } else {
1381- return fmt .Errorf ("error occurred finding security group: %s: %v" , lbSecGroupName , err )
1382- }
1387+ return fmt .Errorf ("Failed to delete Security Group for loadbalancer service %s/%s: %v" , service .Namespace , service .Name , err )
13831388 }
13841389
1385- lbSecGroup := groups .Delete (lbaas .network , lbSecGroupID )
1386- if lbSecGroup .Err != nil && ! isNotFound (lbSecGroup .Err ) {
1387- 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 )
13881396 }
1397+ }
1398+
1399+ return nil
1400+ }
13891401
1390- if len (lbaas .opts .NodeSecurityGroupIDs ) == 0 {
1391- // Just happen when nodes have not Security Group, or should not happen
1392- // UpdateLoadBalancer and EnsureLoadBalancer can set lbaas.opts.NodeSecurityGroupIDs when it is empty
1393- // And service controller call UpdateLoadBalancer to set lbaas.opts.NodeSecurityGroupIDs when controller manager service is restarted.
1394- glog .Warningf ("Can not find node-security-group from all the nodes of this cluser when delete loadbalancer service %s/%s" ,
1395- 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
13961413 } else {
1397- // Delete the rules in the Node Security Group
1398- for _ , nodeSecurityGroupID := range lbaas .opts .NodeSecurityGroupIDs {
1399- opts := rules.ListOpts {
1400- SecGroupID : nodeSecurityGroupID ,
1401- RemoteGroupID : lbSecGroupID ,
1402- }
1403- secGroupRules , err := getSecurityGroupRules (lbaas .network , opts )
1414+ return fmt .Errorf ("Error occurred finding security group: %s: %v" , lbSecGroupName , err )
1415+ }
1416+ }
14041417
1405- if err != nil && ! isNotFound (err ) {
1406- msg := fmt .Sprintf ("Error finding rules for remote group id %s in security group id %s: %v" , lbSecGroupID , nodeSecurityGroupID , err )
1407- 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 )
14081447 }
1448+ }
1449+ }
1450+ }
14091451
1410- for _ , rule := range secGroupRules {
1411- res := rules .Delete (lbaas .network , rule .ID )
1412- if res .Err != nil && ! isNotFound (res .Err ) {
1413- return fmt .Errorf ("error occurred deleting security group rule: %s: %v" , rule .ID , res .Err )
1414- }
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 )
14151510 }
14161511 }
14171512 }
0 commit comments