@@ -50,7 +50,7 @@ func getSecurityGroupName(service *corev1.Service) string {
50
50
}
51
51
52
52
// 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 {
54
54
for _ , node := range nodes {
55
55
serverID , _ , err := instanceIDFromProviderID (node .Spec .ProviderID )
56
56
if err != nil {
@@ -64,7 +64,7 @@ func applyNodeSecurityGroupIDForLB(network *gophercloud.ServiceClient, svcConf *
64
64
}
65
65
66
66
listOpts := neutronports.ListOpts {DeviceID : serverID }
67
- allPorts , err := openstackutil .GetPorts [PortWithPortSecurity ](network , listOpts )
67
+ allPorts , err := openstackutil .GetPorts [PortWithPortSecurity ](ctx , network , listOpts )
68
68
if err != nil {
69
69
return err
70
70
}
@@ -92,7 +92,7 @@ func applyNodeSecurityGroupIDForLB(network *gophercloud.ServiceClient, svcConf *
92
92
newSGs := append (port .SecurityGroups , sg )
93
93
updateOpts := neutronports.UpdateOpts {SecurityGroups : & newSGs }
94
94
mc := metrics .NewMetricContext ("port" , "update" )
95
- res := neutronports .Update (context . TODO () , network , port .ID , updateOpts )
95
+ res := neutronports .Update (ctx , network , port .ID , updateOpts )
96
96
if mc .ObserveRequest (res .Err ) != nil {
97
97
return fmt .Errorf ("failed to update security group for port %s: %v" , port .ID , res .Err )
98
98
}
@@ -103,10 +103,10 @@ func applyNodeSecurityGroupIDForLB(network *gophercloud.ServiceClient, svcConf *
103
103
}
104
104
105
105
// 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 {
107
107
// Find all the ports that have the security group associated.
108
108
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 )
110
110
if err != nil {
111
111
return err
112
112
}
@@ -125,7 +125,7 @@ func disassociateSecurityGroupForLB(network *gophercloud.ServiceClient, sg strin
125
125
// we don't trigger a lost update issue.
126
126
updateOpts := neutronports.UpdateOpts {SecurityGroups : & newSGs }
127
127
mc := metrics .NewMetricContext ("port" , "update" )
128
- res := neutronports .Update (context . TODO () , network , port .ID , updateOpts )
128
+ res := neutronports .Update (ctx , network , port .ID , updateOpts )
129
129
if mc .ObserveRequest (res .Err ) != nil {
130
130
return fmt .Errorf ("failed to update security group for port %s: %v" , port .ID , res .Err )
131
131
}
@@ -134,7 +134,7 @@ func disassociateSecurityGroupForLB(network *gophercloud.ServiceClient, sg strin
134
134
// so this stays for backward compatibility. It's reasonable to delete it in the future. 404s are ignored.
135
135
if slices .Contains (port .Tags , sg ) {
136
136
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 ()
138
138
if mc .ObserveRequest (err ) != nil {
139
139
return fmt .Errorf ("failed to remove tag %s to port %s: %v" , sg , port .ID , res .Err )
140
140
}
@@ -145,9 +145,9 @@ func disassociateSecurityGroupForLB(network *gophercloud.ServiceClient, sg strin
145
145
}
146
146
147
147
// 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 {
149
149
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 ()
151
151
if err != nil && cpoerrors .IsConflictError (err ) {
152
152
// Conflict means the SG rule already exists, so ignoring that error.
153
153
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
204
204
}
205
205
206
206
// 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 {
208
208
// get service ports
209
209
ports := apiService .Spec .Ports
210
210
if len (ports ) == 0 {
@@ -213,7 +213,7 @@ func (lbaas *LbaasV2) ensureAndUpdateOctaviaSecurityGroup(clusterName string, ap
213
213
214
214
// ensure security group for LB
215
215
lbSecGroupName := getSecurityGroupName (apiService )
216
- lbSecGroupID , err := secgroups .IDFromName (context . TODO () , lbaas .network , lbSecGroupName )
216
+ lbSecGroupID , err := secgroups .IDFromName (ctx , lbaas .network , lbSecGroupName )
217
217
if err != nil {
218
218
// If the security group of LB not exist, create it later
219
219
if cpoerrors .IsNotFound (err ) {
@@ -230,15 +230,15 @@ func (lbaas *LbaasV2) ensureAndUpdateOctaviaSecurityGroup(clusterName string, ap
230
230
}
231
231
232
232
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 ()
234
234
if mc .ObserveRequest (err ) != nil {
235
235
return fmt .Errorf ("failed to create Security Group for loadbalancer service %s/%s: %v" , apiService .Namespace , apiService .Name , err )
236
236
}
237
237
lbSecGroupID = lbSecGroup .ID
238
238
}
239
239
240
240
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 ()
242
242
if mc .ObserveRequest (err ) != nil {
243
243
return fmt .Errorf (
244
244
"failed to find subnet %s from openstack: %v" , svcConf .lbMemberSubnetID , err )
@@ -306,7 +306,7 @@ func (lbaas *LbaasV2) ensureAndUpdateOctaviaSecurityGroup(clusterName string, ap
306
306
307
307
// create new rules
308
308
for _ , opts := range toCreate {
309
- err := lbaas .ensureSecurityRule (opts )
309
+ err := lbaas .ensureSecurityRule (ctx , opts )
310
310
if err != nil {
311
311
return fmt .Errorf ("failed to apply security rule (%v), %w" , opts , err )
312
312
}
@@ -316,7 +316,7 @@ func (lbaas *LbaasV2) ensureAndUpdateOctaviaSecurityGroup(clusterName string, ap
316
316
for _ , existingRule := range toDelete {
317
317
klog .Infof ("Deleting rule %s from security group %s (%s)" , existingRule .ID , existingRule .SecGroupID , lbSecGroupName )
318
318
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 ()
320
320
if err != nil && cpoerrors .IsNotFound (err ) {
321
321
// ignore 404
322
322
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
327
327
}
328
328
}
329
329
330
- if err := applyNodeSecurityGroupIDForLB (lbaas .network , svcConf , nodes , lbSecGroupID ); err != nil {
330
+ if err := applyNodeSecurityGroupIDForLB (ctx , lbaas .network , svcConf , nodes , lbSecGroupID ); err != nil {
331
331
return err
332
332
}
333
333
return nil
334
334
}
335
335
336
336
// 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 {
338
338
// Generate Name
339
339
lbSecGroupName := getSecurityGroupName (service )
340
- lbSecGroupID , err := secgroups .IDFromName (context . TODO () , lbaas .network , lbSecGroupName )
340
+ lbSecGroupID , err := secgroups .IDFromName (ctx , lbaas .network , lbSecGroupName )
341
341
if err != nil {
342
342
if cpoerrors .IsNotFound (err ) {
343
343
// It is OK when the security group has been deleted by others.
@@ -347,12 +347,12 @@ func (lbaas *LbaasV2) ensureSecurityGroupDeleted(_ string, service *corev1.Servi
347
347
}
348
348
349
349
// 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 {
351
351
return fmt .Errorf ("failed to disassociate security group %s: %v" , lbSecGroupID , err )
352
352
}
353
353
354
354
mc := metrics .NewMetricContext ("security_group" , "delete" )
355
- lbSecGroup := groups .Delete (context . TODO () , lbaas .network , lbSecGroupID )
355
+ lbSecGroup := groups .Delete (ctx , lbaas .network , lbSecGroupID )
356
356
if lbSecGroup .Err != nil && ! cpoerrors .IsNotFound (lbSecGroup .Err ) {
357
357
return mc .ObserveRequest (lbSecGroup .Err )
358
358
}
0 commit comments