@@ -63,12 +63,14 @@ func (s *meshDataplane) Stop(skipCleanup bool) {
63
63
64
64
log .Debug ("removing host iptables rules" )
65
65
s .hostIptables .DeleteHostRules ()
66
-
67
- log .Debug ("destroying host ipset" )
68
- s .hostsideProbeIPSet .Flush ()
69
- if err := s .hostsideProbeIPSet .DestroySet (); err != nil {
70
- log .Warnf ("could not destroy host ipset on shutdown" )
71
- }
66
+ _ = util .RunAsHost (func () error {
67
+ log .Debug ("destroying host ipset" )
68
+ s .hostsideProbeIPSet .Flush ()
69
+ if err := s .hostsideProbeIPSet .DestroySet (); err != nil {
70
+ log .Warnf ("could not destroy host ipset on shutdown" )
71
+ }
72
+ return nil
73
+ })
72
74
}
73
75
74
76
s .netServer .Stop (skipCleanup )
@@ -247,16 +249,22 @@ func (s *meshDataplane) addPodToHostNSIpset(pod *corev1.Pod, podIPs []netip.Addr
247
249
var ipsetAddrErrs []error
248
250
var addedIps []netip.Addr
249
251
250
- // For each pod IP
251
- for _ , pip := range podIPs {
252
- // Add to host ipset
253
- log .Debugf ("adding probe ip %s to set" , pip )
254
- if err := s .hostsideProbeIPSet .AddIP (pip , ipProto , podUID , true ); err != nil {
255
- ipsetAddrErrs = append (ipsetAddrErrs , err )
256
- log .Errorf ("failed adding ip %s to set, error was %s" , pip , err )
257
- } else {
258
- addedIps = append (addedIps , pip )
252
+ err := util .RunAsHost (func () error {
253
+ // For each pod IP
254
+ for _ , pip := range podIPs {
255
+ // Add to host ipset
256
+ log .Debugf ("adding probe ip %s to set" , pip )
257
+ if err := s .hostsideProbeIPSet .AddIP (pip , ipProto , podUID , true ); err != nil {
258
+ ipsetAddrErrs = append (ipsetAddrErrs , err )
259
+ log .Errorf ("failed adding ip %s to set, error was %s" , pip , err )
260
+ } else {
261
+ addedIps = append (addedIps , pip )
262
+ }
259
263
}
264
+ return nil
265
+ })
266
+ if err != nil {
267
+ ipsetAddrErrs = append (ipsetAddrErrs , err )
260
268
}
261
269
262
270
return addedIps , errors .Join (ipsetAddrErrs ... )
@@ -267,13 +275,18 @@ func (s *meshDataplane) addPodToHostNSIpset(pod *corev1.Pod, podIPs []netip.Addr
267
275
//
268
276
// We will unconditionally flush our set before use here, so it shouldn't matter.
269
277
func createHostsideProbeIpset (isV6 bool ) (ipset.IPSet , error ) {
270
- linDeps := ipset .RealNlDeps ()
271
- probeSet , err := ipset .NewIPSet (iptables .ProbeIPSet , isV6 , linDeps )
272
- if err != nil {
273
- return probeSet , err
274
- }
275
- probeSet .Flush ()
276
- return probeSet , nil
278
+ var probeSet ipset.IPSet
279
+ runErr := util .RunAsHost (func () error {
280
+ var err error
281
+ linDeps := ipset .RealNlDeps ()
282
+ probeSet , err = ipset .NewIPSet (iptables .ProbeIPSet , isV6 , linDeps )
283
+ if err != nil {
284
+ return err
285
+ }
286
+ probeSet .Flush ()
287
+ return nil
288
+ })
289
+ return probeSet , runErr
277
290
}
278
291
279
292
// removePodFromHostNSIpset will remove (v4, v6) pod IPs from the host IP set(s).
@@ -284,32 +297,35 @@ func removePodFromHostNSIpset(pod *corev1.Pod, hostsideProbeSet *ipset.IPSet) er
284
297
log := log .WithLabels ("ns" , pod .Namespace , "name" , pod .Name , "podUID" , podUID , "ipset" , hostsideProbeSet .Prefix )
285
298
286
299
podIPs := util .GetPodIPsIfPresent (pod )
287
- for _ , pip := range podIPs {
288
- if uidMismatch , err := hostsideProbeSet .ClearEntriesWithIPAndComment (pip , podUID ); err != nil {
289
- return err
290
- } else if uidMismatch != "" {
291
- log .Warnf ("pod ip %s could not be removed from ipset, found entry with pod UID %s instead" , pip , uidMismatch )
300
+ return util .RunAsHost (func () error {
301
+ for _ , pip := range podIPs {
302
+ if uidMismatch , err := hostsideProbeSet .ClearEntriesWithIPAndComment (pip , podUID ); err != nil {
303
+ return err
304
+ } else if uidMismatch != "" {
305
+ log .Warnf ("pod ip %s could not be removed from ipset, found entry with pod UID %s instead" , pip , uidMismatch )
306
+ }
307
+ log .Debugf ("removed pod from host ipset by ip %s" , pip )
292
308
}
293
- log .Debugf ("removed pod from host ipset by ip %s" , pip )
294
- }
295
-
296
- return nil
309
+ return nil
310
+ })
297
311
}
298
312
299
313
func pruneHostIPset (expected sets.Set [netip.Addr ], hostsideProbeSet * ipset.IPSet ) error {
300
- actualIPSetContents , err := hostsideProbeSet .ListEntriesByIP ()
301
- if err != nil {
302
- log .Warnf ("unable to list IPSet: %v" , err )
303
- return err
304
- }
305
- actual := sets .New (actualIPSetContents ... )
306
- stales := actual .DifferenceInPlace (expected )
307
-
308
- for staleIP := range stales {
309
- if err := hostsideProbeSet .ClearEntriesWithIP (staleIP ); err != nil {
314
+ return util .RunAsHost (func () error {
315
+ actualIPSetContents , err := hostsideProbeSet .ListEntriesByIP ()
316
+ if err != nil {
317
+ log .Warnf ("unable to list IPSet: %v" , err )
310
318
return err
311
319
}
312
- log .Debugf ("removed stale ip %s from host ipset %s" , staleIP , hostsideProbeSet .Prefix )
313
- }
314
- return nil
320
+ actual := sets .New (actualIPSetContents ... )
321
+ stales := actual .DifferenceInPlace (expected )
322
+
323
+ for staleIP := range stales {
324
+ if err := hostsideProbeSet .ClearEntriesWithIP (staleIP ); err != nil {
325
+ return err
326
+ }
327
+ log .Debugf ("removed stale ip %s from host ipset %s" , staleIP , hostsideProbeSet .Prefix )
328
+ }
329
+ return nil
330
+ })
315
331
}
0 commit comments