ContainerFromNative instantiates a Docker-compatible Container from containerd-native Container.
(n *native.Container)
| 342 | |
| 343 | // ContainerFromNative instantiates a Docker-compatible Container from containerd-native Container. |
| 344 | func ContainerFromNative(n *native.Container) (*Container, error) { |
| 345 | var hostname string |
| 346 | c := &Container{ |
| 347 | ID: n.ID, |
| 348 | Created: n.CreatedAt.Format(time.RFC3339Nano), |
| 349 | Image: n.Image, |
| 350 | Name: n.Labels[labels.Name], |
| 351 | Driver: n.Snapshotter, |
| 352 | // XXX is this always right? what if the container OS is NOT the same as the host OS? |
| 353 | Platform: runtime.GOOS, // for Docker compatibility, this Platform string does NOT contain arch like "/amd64" |
| 354 | } |
| 355 | c.HostConfig = new(HostConfig) |
| 356 | if n.Labels[restart.StatusLabel] == string(containerd.Running) { |
| 357 | c.RestartCount, _ = strconv.Atoi(n.Labels[restart.CountLabel]) |
| 358 | } |
| 359 | containerAnnotations := make(map[string]string) |
| 360 | if sp, ok := n.Spec.(*specs.Spec); ok { |
| 361 | containerAnnotations = sp.Annotations |
| 362 | if p := sp.Process; p != nil { |
| 363 | if len(p.Args) > 0 { |
| 364 | c.Path = p.Args[0] |
| 365 | if len(p.Args) > 1 { |
| 366 | c.Args = p.Args[1:] |
| 367 | } |
| 368 | } |
| 369 | c.AppArmorProfile = p.ApparmorProfile |
| 370 | } |
| 371 | c.Mounts = mountsFromNative(sp.Mounts) |
| 372 | for _, mount := range c.Mounts { |
| 373 | if mount.Destination == "/etc/resolv.conf" { |
| 374 | c.ResolvConfPath = mount.Source |
| 375 | } else if mount.Destination == "/etc/hostname" { |
| 376 | c.HostnamePath = mount.Source |
| 377 | } else if mount.Destination == "/etc/hosts" { |
| 378 | c.HostsPath = mount.Source |
| 379 | } |
| 380 | } |
| 381 | hostname = sp.Hostname |
| 382 | } |
| 383 | if nerdctlStateDir := n.Labels[labels.StateDir]; nerdctlStateDir != "" { |
| 384 | resolvConfPath := filepath.Join(nerdctlStateDir, "resolv.conf") |
| 385 | if _, err := os.Stat(resolvConfPath); err == nil { |
| 386 | c.ResolvConfPath = resolvConfPath |
| 387 | } |
| 388 | hostnamePath := filepath.Join(nerdctlStateDir, "hostname") |
| 389 | if _, err := os.Stat(hostnamePath); err == nil { |
| 390 | c.HostnamePath = hostnamePath |
| 391 | } |
| 392 | c.LogPath = filepath.Join(nerdctlStateDir, n.ID+"-json.log") |
| 393 | if _, err := os.Stat(c.LogPath); err != nil { |
| 394 | c.LogPath = "" |
| 395 | } |
| 396 | hostsPath := filepath.Join(nerdctlStateDir, "hosts") |
| 397 | if _, err := os.Stat(hostsPath); err == nil { |
| 398 | c.HostsPath = hostsPath |
| 399 | } |
| 400 | } |
| 401 |
searching dependent graphs…