@@ -41,13 +41,13 @@ import (
41
41
secgroups "github.com/gophercloud/utils/openstack/networking/v2/extensions/security/groups"
42
42
"gopkg.in/godo.v2/glob"
43
43
corev1 "k8s.io/api/core/v1"
44
- "k8s.io/apimachinery/pkg/types"
45
44
utilerrors "k8s.io/apimachinery/pkg/util/errors"
46
45
"k8s.io/apimachinery/pkg/util/sets"
47
46
"k8s.io/client-go/kubernetes"
48
47
cloudprovider "k8s.io/cloud-provider"
49
48
"k8s.io/klog/v2"
50
49
netutils "k8s.io/utils/net"
50
+ "k8s.io/utils/strings/slices"
51
51
52
52
"k8s.io/cloud-provider-openstack/pkg/metrics"
53
53
cpoutil "k8s.io/cloud-provider-openstack/pkg/util"
@@ -717,48 +717,44 @@ func getSubnetIDForLB(network *gophercloud.ServiceClient, node corev1.Node, pref
717
717
}
718
718
719
719
// applyNodeSecurityGroupIDForLB associates the security group with all the ports on the nodes.
720
- func applyNodeSecurityGroupIDForLB (compute * gophercloud. ServiceClient , network * gophercloud.ServiceClient , nodes []* corev1.Node , sg string ) error {
720
+ func applyNodeSecurityGroupIDForLB (network * gophercloud.ServiceClient , nodes []* corev1.Node , sg string ) error {
721
721
for _ , node := range nodes {
722
- nodeName := types .NodeName (node .Name )
723
- srv , err := getServerByName (compute , nodeName )
722
+ serverID , _ , err := instanceIDFromProviderID (node .Spec .ProviderID )
724
723
if err != nil {
725
- return err
724
+ return fmt . Errorf ( "error getting server ID from the node: %w" , err )
726
725
}
727
-
728
- listOpts := neutronports.ListOpts {DeviceID : srv .ID }
726
+ listOpts := neutronports.ListOpts {DeviceID : serverID }
729
727
allPorts , err := openstackutil .GetPorts (network , listOpts )
730
728
if err != nil {
731
729
return err
732
730
}
733
731
734
732
for _ , port := range allPorts {
735
733
// If the Security Group is already present on the port, skip it.
736
- // As soon as this only supports Go 1.18, this can be replaces by
737
- // slices.Contains.
738
- if func () bool {
739
- for _ , currentSG := range port .SecurityGroups {
740
- if currentSG == sg {
741
- return true
742
- }
743
- }
744
- return false
745
- }() {
734
+ if slices .Contains (port .SecurityGroups , sg ) {
746
735
continue
747
736
}
748
737
738
+ // Add the security group ID as a tag to the port in order to find all these ports when removing the security group.
739
+ // We're doing that before actually applying the SG as if tagging would fail we wouldn't be able to find the port
740
+ // when deleting the SG and operation would be stuck forever. It's better to find more ports than not all of them.
741
+ mc := metrics .NewMetricContext ("port_tag" , "add" )
742
+ err := neutrontags .Add (network , "ports" , port .ID , sg ).ExtractErr ()
743
+ if mc .ObserveRequest (err ) != nil {
744
+ return fmt .Errorf ("failed to add tag %s to port %s: %v" , sg , port .ID , err )
745
+ }
746
+
747
+ // Add the SG to the port
748
+ // TODO(dulek): This isn't an atomic operation. In order to protect from lost update issues we should use
749
+ // `revision_number` handling to make sure our update to `security_groups` field wasn't preceded
750
+ // by a different one. Same applies to a removal of the SG.
749
751
newSGs := append (port .SecurityGroups , sg )
750
752
updateOpts := neutronports.UpdateOpts {SecurityGroups : & newSGs }
751
- mc : = metrics .NewMetricContext ("port" , "update" )
753
+ mc = metrics .NewMetricContext ("port" , "update" )
752
754
res := neutronports .Update (network , port .ID , updateOpts )
753
755
if mc .ObserveRequest (res .Err ) != nil {
754
756
return fmt .Errorf ("failed to update security group for port %s: %v" , port .ID , res .Err )
755
757
}
756
- // Add the security group ID as a tag to the port in order to find all these ports when removing the security group.
757
- mc = metrics .NewMetricContext ("port_tag" , "add" )
758
- err := neutrontags .Add (network , "ports" , port .ID , sg ).ExtractErr ()
759
- if mc .ObserveRequest (err ) != nil {
760
- return fmt .Errorf ("failed to add tag %s to port %s: %v" , sg , port .ID , err )
761
- }
762
758
}
763
759
}
764
760
@@ -2361,7 +2357,7 @@ func (lbaas *LbaasV2) ensureAndUpdateOctaviaSecurityGroup(clusterName string, ap
2361
2357
}
2362
2358
}
2363
2359
2364
- if err := applyNodeSecurityGroupIDForLB (lbaas .compute , lbaas . network , nodes , lbSecGroupID ); err != nil {
2360
+ if err := applyNodeSecurityGroupIDForLB (lbaas .network , nodes , lbSecGroupID ); err != nil {
2365
2361
return err
2366
2362
}
2367
2363
return nil
0 commit comments