MCPcopy Create free account
hub / github.com/containerd/nerdctl / getPortMapOpts

Function getPortMapOpts

pkg/ocihook/ocihook.go:330–372  ·  view source on GitHub ↗
(opts *handlerOpts)

Source from the content-addressed store, hash-verified

328}
329
330func 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
374func getIPAddressOpts(opts *handlerOpts) ([]cni.NamespaceOpts, error) {
375 if opts.containerIP != "" {

Callers 2

applyNetworkSettingsFunction · 0.85
onPostStopFunction · 0.85

Calls 3

IsRootlessChildFunction · 0.92
InfoMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…