Repository: containerd/containerd
File: cmd/ctr/commands/run/run_unix.go (Go)

Code before:

func getNetNSPath(_ context.Context, task containerd.Task) (string, error) {
	return fmt.Sprintf("/proc/%d/ns/net", task.Pid()), nil
}

// vendorLister is an interface for listing GPU vendors from CDI.
type vendorLister interface {
	ListVendors() []string
}

// detectGPUVendor detects available GPU vendors from CDI spec files.
// It returns the vendor string for the first known GPU vendor found,
// or an empty string if no GPU vendor is detected.
func detectGPUVendor(ctx context.Context, lister vendorLister) (string, error) {
	if lister == nil {
		return "", errors.New("vendor lister is nil")
	}

	availableVendors := lister.ListVendors()
	knownVendors := []string{"nvidia.com", "amd.com"}

	// Check if a known GPU vendor is available
	// NVIDIA is checked first followed by AMD
	for _, known := range knownVendors {
		for _, available := range availableVendors {
			if available == known {
				log.G(ctx).Debugf("Detected GPU vendor from CDI specs: %s", known)
				return known, nil
			}
		}
	}

	return "", fmt.Errorf("no known GPU vendor detected in CDI specs, only AMD and NVIDIA are supported")
}

// gpuDeviceNames converts GPU indices to qualified CDI device names.
// It auto-detects the GPU vendor (e.g., nvidia.com or amd.com) from available CDI specs.
// Returns the device names and an error if no GPU vendor is found.
func gpuDeviceNames(ctx context.Context, lister vendorLister, gpuIDs ...int) ([]string, error) {
	if len(gpuIDs) == 0 {
		return nil, nil
	}

	// Detect GPU vendor from CDI specs
	vendor, err := detectGPUVendor(ctx, lister)
	if err != nil {
		return nil, fmt.Errorf("GPU device CDI name resolution failed: %w", err)
	}

	// Build CDI device names from GPU indices
	devices := make([]string, 0, len(gpuIDs))

Review comment (bug):
nit: can use `slices` package to slightly reduce number of branches.

```go
  for _, known := range knownVendors {
      if slices.Contains(availableVendors, known) {
          log.G(ctx).Debugf("Detected GPU vendor from CDI specs: %s", known)
          return known, nil
      }
  }
```

Code after:
}

func getNetNSPath(_ context.Context, task containerd.Task) (string, error) {
	return fmt.Sprintf("/proc/%d/ns/net", task.Pid()), nil
}

// detectGPUVendor detects a known GPU vendor from a list of available vendors.
// It returns the vendor string for the first known GPU vendor found in availableVendors,
// or an error if no known GPU vendor is detected.
func detectGPUVendor(ctx context.Context, availableVendors []string) (string, error) {
	knownVendors := []string{"nvidia.com", "amd.com"}
	for _, known := range knownVendors {
		if slices.Contains(availableVendors, known) {
			log.G(ctx).Debugf("Detected GPU vendor from CDI specs: %s", known)
			return known, nil
		}
	}
	return "", fmt.Errorf("no known GPU vendor detected in CDI specs, only AMD and NVIDIA are supported")
}

// gpuDeviceNames converts GPU indices to qualified CDI device names for the given vendor.
func gpuDeviceNames(ctx context.Context, vendor string, gpuIDs ...int) ([]string, error) {
	if len(gpuIDs) == 0 {
		return nil, nil
	}
	if vendor == "" {
		return nil, fmt.Errorf("gpu vendor can't be empty")
	}
	devices := make([]string, 0, len(gpuIDs))
	for _, id := range gpuIDs {
		devices = append(devices, fmt.Sprintf("%s/gpu=%d", vendor, id))
	}
	log.G(ctx).Debugf("Resolved GPU device names: %v", devices)
	return devices, nil
}

// withStaticCDIRegistry inits the CDI registry and disables auto-refresh.
// This is used from the `run` command to avoid creating a registry with auto-refresh enabled.
// It also provides a way to override the CDI spec file paths if required.
func withStaticCDIRegistry() oci.SpecOpts {
	return func(ctx context.Context, _ oci.Client, _ *containers.Container, s *oci.Spec) error {
		_ = cdi.Configure(cdi.WithAutoRefresh(false))
		if err := cdi.Refresh(); err != nil {
			// We don't consider registry refresh failure a fatal error.
			// For instance, a dynamically generated invalid CDI Spec file for
			// any particular vendor shouldn't prevent injection of devices of
			// different vendors. CDI itself knows better and it will fail the
			// injection if necessary.
			log.G(ctx).Warnf("CDI registry refresh failed: %v", err)
		}
		return nil