Skip to content

Commit f2a57de

Browse files
authored
[occm] Lookup ports by SG and not tags (#2355) (#2538)
* Update gophercloud to v1.6.0 * Lookup ports by SG and not tags When creating an SG for an LB we tag the ports we attach the SG to in order to be able to find them easily later on when the SG will be getting deleted. We do that to simulate an API allowing us to filter ports by attached SGs, but turns out - such an API already exists in Neutron and Gophercloud v1.6.0 allows to use it. This commit makes sure that when deleting an SG we're discovering ports to remove it from by querying the Neutron filtering by SG. Moreover tagging the ports with the SGs is removed to as not being necessary anymore. I've left the code removing the SG tags from ports for backward compatibility.
1 parent 1f54082 commit f2a57de

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.20
55
require (
66
github.com/container-storage-interface/spec v1.8.0
77
github.com/go-chi/chi/v5 v5.0.8
8-
github.com/gophercloud/gophercloud v1.4.0
8+
github.com/gophercloud/gophercloud v1.6.0
99
github.com/gophercloud/utils v0.0.0-20230330070308-5bd5e1d608f8
1010
github.com/hashicorp/go-version v1.6.0
1111
github.com/kubernetes-csi/csi-lib-utils v0.13.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
226226
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
227227
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
228228
github.com/gophercloud/gophercloud v1.3.0/go.mod h1:aAVqcocTSXh2vYFZ1JTvx4EQmfgzxRcNupUfxZbBNDM=
229-
github.com/gophercloud/gophercloud v1.4.0 h1:RqEu43vaX0lb0LanZr5BylK5ICVxjpFFoc0sxivyuHU=
230-
github.com/gophercloud/gophercloud v1.4.0/go.mod h1:aAVqcocTSXh2vYFZ1JTvx4EQmfgzxRcNupUfxZbBNDM=
229+
github.com/gophercloud/gophercloud v1.6.0 h1:JwJN1bauRnWPba5ueWs9IluONHteXPWjjK+MvfM4krY=
230+
github.com/gophercloud/gophercloud v1.6.0/go.mod h1:aAVqcocTSXh2vYFZ1JTvx4EQmfgzxRcNupUfxZbBNDM=
231231
github.com/gophercloud/utils v0.0.0-20230330070308-5bd5e1d608f8 h1:K9r5WEeAiaEgFZsuOP0OYjE4TtyFcCLG1nI08t9AP6A=
232232
github.com/gophercloud/utils v0.0.0-20230330070308-5bd5e1d608f8/go.mod h1:VSalo4adEk+3sNkmVJLnhHoOyOYYS8sTWLG4mv5BKto=
233233
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=

pkg/openstack/loadbalancer.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -764,22 +764,13 @@ func applyNodeSecurityGroupIDForLB(network *gophercloud.ServiceClient, svcConf *
764764
continue
765765
}
766766

767-
// Add the security group ID as a tag to the port in order to find all these ports when removing the security group.
768-
// We're doing that before actually applying the SG as if tagging would fail we wouldn't be able to find the port
769-
// when deleting the SG and operation would be stuck forever. It's better to find more ports than not all of them.
770-
mc := metrics.NewMetricContext("port_tag", "add")
771-
err := neutrontags.Add(network, "ports", port.ID, sg).ExtractErr()
772-
if mc.ObserveRequest(err) != nil {
773-
return fmt.Errorf("failed to add tag %s to port %s: %v", sg, port.ID, err)
774-
}
775-
776767
// Add the SG to the port
777768
// TODO(dulek): This isn't an atomic operation. In order to protect from lost update issues we should use
778769
// `revision_number` handling to make sure our update to `security_groups` field wasn't preceded
779770
// by a different one. Same applies to a removal of the SG.
780771
newSGs := append(port.SecurityGroups, sg)
781772
updateOpts := neutronports.UpdateOpts{SecurityGroups: &newSGs}
782-
mc = metrics.NewMetricContext("port", "update")
773+
mc := metrics.NewMetricContext("port", "update")
783774
res := neutronports.Update(network, port.ID, updateOpts)
784775
if mc.ObserveRequest(res.Err) != nil {
785776
return fmt.Errorf("failed to update security group for port %s: %v", port.ID, res.Err)
@@ -793,7 +784,7 @@ func applyNodeSecurityGroupIDForLB(network *gophercloud.ServiceClient, svcConf *
793784
// disassociateSecurityGroupForLB removes the given security group from the ports
794785
func disassociateSecurityGroupForLB(network *gophercloud.ServiceClient, sg string) error {
795786
// Find all the ports that have the security group associated.
796-
listOpts := neutronports.ListOpts{TagsAny: sg}
787+
listOpts := neutronports.ListOpts{SecurityGroups: []string{sg}}
797788
allPorts, err := openstackutil.GetPorts[neutronports.Port](network, listOpts)
798789
if err != nil {
799790
return err
@@ -809,17 +800,23 @@ func disassociateSecurityGroupForLB(network *gophercloud.ServiceClient, sg strin
809800

810801
// Update port security groups
811802
newSGs := existingSGs.List()
803+
// TODO(dulek): This should be done using Neutron's revision_number to make sure
804+
// we don't trigger a lost update issue.
812805
updateOpts := neutronports.UpdateOpts{SecurityGroups: &newSGs}
813806
mc := metrics.NewMetricContext("port", "update")
814807
res := neutronports.Update(network, port.ID, updateOpts)
815808
if mc.ObserveRequest(res.Err) != nil {
816809
return fmt.Errorf("failed to update security group for port %s: %v", port.ID, res.Err)
817810
}
818-
// Remove the security group ID tag from the port.
819-
mc = metrics.NewMetricContext("port_tag", "delete")
820-
err := neutrontags.Delete(network, "ports", port.ID, sg).ExtractErr()
821-
if mc.ObserveRequest(err) != nil {
822-
return fmt.Errorf("failed to remove tag %s to port %s: %v", sg, port.ID, res.Err)
811+
812+
// Remove the security group ID tag from the port. Please note we don't tag ports with SG IDs anymore,
813+
// so this stays for backward compatibility. It's reasonable to delete it in the future. 404s are ignored.
814+
if slices.Contains(port.Tags, sg) {
815+
mc = metrics.NewMetricContext("port_tag", "delete")
816+
err := neutrontags.Delete(network, "ports", port.ID, sg).ExtractErr()
817+
if mc.ObserveRequest(err) != nil {
818+
return fmt.Errorf("failed to remove tag %s to port %s: %v", sg, port.ID, res.Err)
819+
}
823820
}
824821
}
825822

@@ -2087,7 +2084,7 @@ func (lbaas *LbaasV2) ensureOctaviaLoadBalancer(ctx context.Context, clusterName
20872084
} else {
20882085
// Attempt to delete the SG if `manage-security-groups` is disabled. When CPO is reconfigured to enable it we
20892086
// will reconcile the LB and create the SG. This is to make sure it works the same in the opposite direction.
2090-
if err := lbaas.EnsureSecurityGroupDeleted(clusterName, service); err != nil {
2087+
if err := lbaas.ensureSecurityGroupDeleted(clusterName, service); err != nil {
20912088
return status, err
20922089
}
20932090
}
@@ -2593,15 +2590,15 @@ func (lbaas *LbaasV2) ensureLoadBalancerDeleted(ctx context.Context, clusterName
25932590

25942591
// Delete the Security Group. We're doing that even if `manage-security-groups` is disabled to make sure we don't
25952592
// orphan created SGs even if CPO got reconfigured.
2596-
if err := lbaas.EnsureSecurityGroupDeleted(clusterName, service); err != nil {
2593+
if err := lbaas.ensureSecurityGroupDeleted(clusterName, service); err != nil {
25972594
return err
25982595
}
25992596

26002597
return nil
26012598
}
26022599

2603-
// EnsureSecurityGroupDeleted deleting security group for specific loadbalancer service.
2604-
func (lbaas *LbaasV2) EnsureSecurityGroupDeleted(_ string, service *corev1.Service) error {
2600+
// ensureSecurityGroupDeleted deleting security group for specific loadbalancer service.
2601+
func (lbaas *LbaasV2) ensureSecurityGroupDeleted(_ string, service *corev1.Service) error {
26052602
// Generate Name
26062603
lbSecGroupName := getSecurityGroupName(service)
26072604
lbSecGroupID, err := secgroups.IDFromName(lbaas.network, lbSecGroupName)

0 commit comments

Comments
 (0)