(opts *handlerOpts)
| 328 | } |
| 329 | |
| 330 | func getPortMapOpts(opts *handlerOpts) ([]cni.NamespaceOpts, error) { |
| 331 | if len(opts.ports) > 0 { |
| 332 | if !rootlessutil.IsRootlessChild() { |
| 333 | return []cni.NamespaceOpts{cni.WithCapabilityPortMap(opts.ports)}, nil |
| 334 | } |
| 335 | var ( |
| 336 | childIP net.IP |
| 337 | portDriverDisallowsLoopbackChildIP bool |
| 338 | ) |
| 339 | info, err := opts.rootlessKitClient.Info(context.TODO()) |
| 340 | if err != nil { |
| 341 | log.L.WithError(err).Warn("cannot call RootlessKit Info API, make sure you have RootlessKit v0.14.1 or later") |
| 342 | } else { |
| 343 | childIP = info.NetworkDriver.ChildIP |
| 344 | portDriverDisallowsLoopbackChildIP = info.PortDriver.DisallowLoopbackChildIP // true for slirp4netns port driver |
| 345 | } |
| 346 | // For rootless, we need to modify the hostIP that is not bindable in the child namespace. |
| 347 | // https: //github.com/containerd/nerdctl/issues/88 |
| 348 | // |
| 349 | // We must NOT modify opts.ports here, because we use the unmodified opts.ports for |
| 350 | // interaction with RootlessKit API. |
| 351 | ports := make([]cni.PortMapping, len(opts.ports)) |
| 352 | for i, p := range opts.ports { |
| 353 | if hostIP := net.ParseIP(p.HostIP); hostIP != nil && !hostIP.IsUnspecified() { |
| 354 | // loopback address is always bindable in the child namespace, but other addresses are unlikely. |
| 355 | if !hostIP.IsLoopback() { |
| 356 | if !(childIP != nil && childIP.Equal(hostIP)) { |
| 357 | if portDriverDisallowsLoopbackChildIP { |
| 358 | p.HostIP = childIP.String() |
| 359 | } else { |
| 360 | p.HostIP = "127.0.0.1" |
| 361 | } |
| 362 | } |
| 363 | } else if portDriverDisallowsLoopbackChildIP { |
| 364 | p.HostIP = childIP.String() |
| 365 | } |
| 366 | } |
| 367 | ports[i] = p |
| 368 | } |
| 369 | return []cni.NamespaceOpts{cni.WithCapabilityPortMap(ports)}, nil |
| 370 | } |
| 371 | return nil, nil |
| 372 | } |
| 373 | |
| 374 | func getIPAddressOpts(opts *handlerOpts) ([]cni.NamespaceOpts, error) { |
| 375 | if opts.containerIP != "" { |
no test coverage detected
searching dependent graphs…