Repository: containerd/containerd
File: internal/cri/opts/spec_opts.go (Go)

Code before:
	return m.parts(i) < m.parts(j)
}

// Swap swaps two items in an array of mounts. Used in sorting
func (m orderedMounts) Swap(i, j int) {
	m[i], m[j] = m[j], m[i]
}

// parts returns the number of parts in the destination of a mount. Used in sorting.
func (m orderedMounts) parts(i int) int {
	return strings.Count(filepath.Clean(m[i].ContainerPath), string(os.PathSeparator))
}

// WithAnnotation sets the provided annotation
func WithAnnotation(k, v string) oci.SpecOpts {
	return func(ctx context.Context, client oci.Client, c *containers.Container, s *runtimespec.Spec) error {
		if s.Annotations == nil {
			s.Annotations = make(map[string]string)
		}
		s.Annotations[k] = v
		return nil
	}
}

// WithAffinityCPUs sets the CPU affinity values in runtime spec for windows.
func WithAffinityCPUs(affinityCPUs []*runtime.WindowsCpuGroupAffinity) oci.SpecOpts {
	return func(_ context.Context, _ oci.Client, c *containers.Container, s *runtimespec.Spec) error {
		// We would expect the Windows and Resources struct to be setup by now
		if affinityCPUs == nil || s.Windows == nil || s.Windows.Resources == nil {
			return nil
		}
		if s.Windows.Resources.CPU == nil {
			s.Windows.Resources.CPU = &runtimespec.WindowsCPUResources{}
		}
		if s.Windows.Resources.CPU.Affinity == nil {
			s.Windows.Resources.CPU.Affinity = make([]runtimespec.WindowsCPUGroupAffinity, len(affinityCPUs))
		}
		for i, affinity := range affinityCPUs {
			s.Windows.Resources.CPU.Affinity[i] = runtimespec.WindowsCPUGroupAffinity{
				Mask:  affinity.CpuMask,
				Group: affinity.CpuGroup,
			}
		}
		return nil
	}
}

// WithAdditionalGIDs adds any additional groups listed for a particular user in the
// /etc/groups file of the image's root filesystem to the OCI spec's additionalGids array.
func WithAdditionalGIDs(userstr string) oci.SpecOpts {
	return func(ctx context.Context, client oci.Client, c *containers.Container, s *runtimespec.Spec) (err error) {

Review comment (suggestion):
Curious if we also want to make this feature available for non-CRI purposes; in that case we could (as I mentioned in an earlier comment) implement the core functionality in the `oci` package, and use this as an adaptor; something like (very quick draft);
https://github.com/containerd/containerd/commit/e9c0018681db7313ee2ab5a22f47bc25d15e6cb8

Code after:
	return m.parts(i) < m.parts(j)
}

// Swap swaps two items in an array of mounts. Used in sorting
func (m orderedMounts) Swap(i, j int) {
	m[i], m[j] = m[j], m[i]
}

// parts returns the number of parts in the destination of a mount. Used in sorting.
func (m orderedMounts) parts(i int) int {
	return strings.Count(filepath.Clean(m[i].ContainerPath), string(os.PathSeparator))
}

// WithAnnotation sets the provided annotation
func WithAnnotation(k, v string) oci.SpecOpts {
	return func(ctx context.Context, client oci.Client, c *containers.Container, s *runtimespec.Spec) error {
		if s.Annotations == nil {
			s.Annotations = make(map[string]string)
		}
		s.Annotations[k] = v
		return nil
	}
}

// WithWindowsAffinityCPUs sets the CPU affinity values in runtime spec for windows.
func WithWindowsAffinityCPUs(config *runtime.WindowsContainerConfig) oci.SpecOpts {
	return func(_ context.Context, _ oci.Client, c *containers.Container, s *runtimespec.Spec) error {
		if s.Windows == nil || config.Resources == nil || config.Resources.AffinityCpus == nil {
			return nil
		}

		if s.Windows.Resources == nil {
			s.Windows.Resources = &runtimespec.WindowsResources{}
		}

		if s.Windows.Resources.CPU == nil {
			s.Windows.Resources.CPU = &runtimespec.WindowsCPUResources{}
		}

		affinities := make([]runtimespec.WindowsCPUGroupAffinity, 0, len(config.Resources.AffinityCpus))
		for _, affinity := range config.Resources.AffinityCpus {
			affinities = append(affinities, runtimespec.WindowsCPUGroupAffinity{
				Mask:  affinity.CpuMask,
				Group: affinity.CpuGroup,
			})
		}
		s.Windows.Resources.CPU.Affinity = affinities
		return nil
	}
}