@@ -50,7 +50,7 @@ func getSecurityGroupName(service *corev1.Service) string {
5050}
5151
5252// applyNodeSecurityGroupIDForLB associates the security group with the ports being members of the LB on the nodes.
53- func applyNodeSecurityGroupIDForLB (network * gophercloud.ServiceClient , svcConf * serviceConfig , nodes []* corev1.Node , sg string ) error {
53+ func applyNodeSecurityGroupIDForLB (ctx context. Context , network * gophercloud.ServiceClient , svcConf * serviceConfig , nodes []* corev1.Node , sg string ) error {
5454 for _ , node := range nodes {
5555 serverID , _ , err := instanceIDFromProviderID (node .Spec .ProviderID )
5656 if err != nil {
@@ -64,7 +64,7 @@ func applyNodeSecurityGroupIDForLB(network *gophercloud.ServiceClient, svcConf *
6464 }
6565
6666 listOpts := neutronports.ListOpts {DeviceID : serverID }
67- allPorts , err := openstackutil .GetPorts [PortWithPortSecurity ](network , listOpts )
67+ allPorts , err := openstackutil .GetPorts [PortWithPortSecurity ](ctx , network , listOpts )
6868 if err != nil {
6969 return err
7070 }
@@ -92,7 +92,7 @@ func applyNodeSecurityGroupIDForLB(network *gophercloud.ServiceClient, svcConf *
9292 newSGs := append (port .SecurityGroups , sg )
9393 updateOpts := neutronports.UpdateOpts {SecurityGroups : & newSGs }
9494 mc := metrics .NewMetricContext ("port" , "update" )
95- res := neutronports .Update (context . TODO () , network , port .ID , updateOpts )
95+ res := neutronports .Update (ctx , network , port .ID , updateOpts )
9696 if mc .ObserveRequest (res .Err ) != nil {
9797 return fmt .Errorf ("failed to update security group for port %s: %v" , port .ID , res .Err )
9898 }
@@ -103,10 +103,10 @@ func applyNodeSecurityGroupIDForLB(network *gophercloud.ServiceClient, svcConf *
103103}
104104
105105// disassociateSecurityGroupForLB removes the given security group from the ports
106- func disassociateSecurityGroupForLB (network * gophercloud.ServiceClient , sg string ) error {
106+ func disassociateSecurityGroupForLB (ctx context. Context , network * gophercloud.ServiceClient , sg string ) error {
107107 // Find all the ports that have the security group associated.
108108 listOpts := neutronports.ListOpts {SecurityGroups : []string {sg }}
109- allPorts , err := openstackutil .GetPorts [neutronports.Port ](network , listOpts )
109+ allPorts , err := openstackutil .GetPorts [neutronports.Port ](ctx , network , listOpts )
110110 if err != nil {
111111 return err
112112 }
@@ -125,7 +125,7 @@ func disassociateSecurityGroupForLB(network *gophercloud.ServiceClient, sg strin
125125 // we don't trigger a lost update issue.
126126 updateOpts := neutronports.UpdateOpts {SecurityGroups : & newSGs }
127127 mc := metrics .NewMetricContext ("port" , "update" )
128- res := neutronports .Update (context . TODO () , network , port .ID , updateOpts )
128+ res := neutronports .Update (ctx , network , port .ID , updateOpts )
129129 if mc .ObserveRequest (res .Err ) != nil {
130130 return fmt .Errorf ("failed to update security group for port %s: %v" , port .ID , res .Err )
131131 }
@@ -134,7 +134,7 @@ func disassociateSecurityGroupForLB(network *gophercloud.ServiceClient, sg strin
134134 // so this stays for backward compatibility. It's reasonable to delete it in the future. 404s are ignored.
135135 if slices .Contains (port .Tags , sg ) {
136136 mc = metrics .NewMetricContext ("port_tag" , "delete" )
137- err := neutrontags .Delete (context . TODO () , network , "ports" , port .ID , sg ).ExtractErr ()
137+ err := neutrontags .Delete (ctx , network , "ports" , port .ID , sg ).ExtractErr ()
138138 if mc .ObserveRequest (err ) != nil {
139139 return fmt .Errorf ("failed to remove tag %s to port %s: %v" , sg , port .ID , res .Err )
140140 }
@@ -145,9 +145,9 @@ func disassociateSecurityGroupForLB(network *gophercloud.ServiceClient, sg strin
145145}
146146
147147// group, if it not present.
148- func (lbaas * LbaasV2 ) ensureSecurityRule (sgRuleCreateOpts rules.CreateOpts ) error {
148+ func (lbaas * LbaasV2 ) ensureSecurityRule (ctx context. Context , sgRuleCreateOpts rules.CreateOpts ) error {
149149 mc := metrics .NewMetricContext ("security_group_rule" , "create" )
150- _ , err := rules .Create (context . TODO () , lbaas .network , sgRuleCreateOpts ).Extract ()
150+ _ , err := rules .Create (ctx , lbaas .network , sgRuleCreateOpts ).Extract ()
151151 if err != nil && cpoerrors .IsConflictError (err ) {
152152 // Conflict means the SG rule already exists, so ignoring that error.
153153 klog .Warningf ("Security group rule already found when trying to create it. This indicates concurrent " +
@@ -204,7 +204,7 @@ func getRulesToCreateAndDelete(wantedRules []rules.CreateOpts, existingRules []r
204204}
205205
206206// ensureAndUpdateOctaviaSecurityGroup handles the creation and update of the security group and the securiry rules for the octavia load balancer
207- func (lbaas * LbaasV2 ) ensureAndUpdateOctaviaSecurityGroup (clusterName string , apiService * corev1.Service , nodes []* corev1.Node , svcConf * serviceConfig ) error {
207+ func (lbaas * LbaasV2 ) ensureAndUpdateOctaviaSecurityGroup (ctx context. Context , clusterName string , apiService * corev1.Service , nodes []* corev1.Node , svcConf * serviceConfig ) error {
208208 // get service ports
209209 ports := apiService .Spec .Ports
210210 if len (ports ) == 0 {
@@ -213,7 +213,7 @@ func (lbaas *LbaasV2) ensureAndUpdateOctaviaSecurityGroup(clusterName string, ap
213213
214214 // ensure security group for LB
215215 lbSecGroupName := getSecurityGroupName (apiService )
216- lbSecGroupID , err := secgroups .IDFromName (context . TODO () , lbaas .network , lbSecGroupName )
216+ lbSecGroupID , err := secgroups .IDFromName (ctx , lbaas .network , lbSecGroupName )
217217 if err != nil {
218218 // If the security group of LB not exist, create it later
219219 if cpoerrors .IsNotFound (err ) {
@@ -230,15 +230,15 @@ func (lbaas *LbaasV2) ensureAndUpdateOctaviaSecurityGroup(clusterName string, ap
230230 }
231231
232232 mc := metrics .NewMetricContext ("security_group" , "create" )
233- lbSecGroup , err := groups .Create (context . TODO () , lbaas .network , lbSecGroupCreateOpts ).Extract ()
233+ lbSecGroup , err := groups .Create (ctx , lbaas .network , lbSecGroupCreateOpts ).Extract ()
234234 if mc .ObserveRequest (err ) != nil {
235235 return fmt .Errorf ("failed to create Security Group for loadbalancer service %s/%s: %v" , apiService .Namespace , apiService .Name , err )
236236 }
237237 lbSecGroupID = lbSecGroup .ID
238238 }
239239
240240 mc := metrics .NewMetricContext ("subnet" , "get" )
241- subnet , err := subnets .Get (context . TODO () , lbaas .network , svcConf .lbMemberSubnetID ).Extract ()
241+ subnet , err := subnets .Get (ctx , lbaas .network , svcConf .lbMemberSubnetID ).Extract ()
242242 if mc .ObserveRequest (err ) != nil {
243243 return fmt .Errorf (
244244 "failed to find subnet %s from openstack: %v" , svcConf .lbMemberSubnetID , err )
@@ -306,7 +306,7 @@ func (lbaas *LbaasV2) ensureAndUpdateOctaviaSecurityGroup(clusterName string, ap
306306
307307 // create new rules
308308 for _ , opts := range toCreate {
309- err := lbaas .ensureSecurityRule (opts )
309+ err := lbaas .ensureSecurityRule (ctx , opts )
310310 if err != nil {
311311 return fmt .Errorf ("failed to apply security rule (%v), %w" , opts , err )
312312 }
@@ -316,7 +316,7 @@ func (lbaas *LbaasV2) ensureAndUpdateOctaviaSecurityGroup(clusterName string, ap
316316 for _ , existingRule := range toDelete {
317317 klog .Infof ("Deleting rule %s from security group %s (%s)" , existingRule .ID , existingRule .SecGroupID , lbSecGroupName )
318318 mc := metrics .NewMetricContext ("security_group_rule" , "delete" )
319- err := rules .Delete (context . TODO () , lbaas .network , existingRule .ID ).ExtractErr ()
319+ err := rules .Delete (ctx , lbaas .network , existingRule .ID ).ExtractErr ()
320320 if err != nil && cpoerrors .IsNotFound (err ) {
321321 // ignore 404
322322 klog .Warningf ("Security group rule %s found missing when trying to delete it. This indicates concurrent " +
@@ -327,17 +327,17 @@ func (lbaas *LbaasV2) ensureAndUpdateOctaviaSecurityGroup(clusterName string, ap
327327 }
328328 }
329329
330- if err := applyNodeSecurityGroupIDForLB (lbaas .network , svcConf , nodes , lbSecGroupID ); err != nil {
330+ if err := applyNodeSecurityGroupIDForLB (ctx , lbaas .network , svcConf , nodes , lbSecGroupID ); err != nil {
331331 return err
332332 }
333333 return nil
334334}
335335
336336// ensureSecurityGroupDeleted deleting security group for specific loadbalancer service.
337- func (lbaas * LbaasV2 ) ensureSecurityGroupDeleted (_ string , service * corev1.Service ) error {
337+ func (lbaas * LbaasV2 ) ensureSecurityGroupDeleted (ctx context. Context , service * corev1.Service ) error {
338338 // Generate Name
339339 lbSecGroupName := getSecurityGroupName (service )
340- lbSecGroupID , err := secgroups .IDFromName (context . TODO () , lbaas .network , lbSecGroupName )
340+ lbSecGroupID , err := secgroups .IDFromName (ctx , lbaas .network , lbSecGroupName )
341341 if err != nil {
342342 if cpoerrors .IsNotFound (err ) {
343343 // It is OK when the security group has been deleted by others.
@@ -347,12 +347,12 @@ func (lbaas *LbaasV2) ensureSecurityGroupDeleted(_ string, service *corev1.Servi
347347 }
348348
349349 // Disassociate the security group from the neutron ports on the nodes.
350- if err := disassociateSecurityGroupForLB (lbaas .network , lbSecGroupID ); err != nil {
350+ if err := disassociateSecurityGroupForLB (ctx , lbaas .network , lbSecGroupID ); err != nil {
351351 return fmt .Errorf ("failed to disassociate security group %s: %v" , lbSecGroupID , err )
352352 }
353353
354354 mc := metrics .NewMetricContext ("security_group" , "delete" )
355- lbSecGroup := groups .Delete (context . TODO () , lbaas .network , lbSecGroupID )
355+ lbSecGroup := groups .Delete (ctx , lbaas .network , lbSecGroupID )
356356 if lbSecGroup .Err != nil && ! cpoerrors .IsNotFound (lbSecGroup .Err ) {
357357 return mc .ObserveRequest (lbSecGroup .Err )
358358 }
0 commit comments