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

Code before:
// 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) {
		if s.Process == nil {
			s.Process = &runtimespec.Process{}
		}
		gids := s.Process.User.AdditionalGids
		if err := oci.WithAdditionalGIDs(userstr)(ctx, client, c, s); err != nil {
			return err
		}
		// Merge existing gids and new gids.
		s.Process.User.AdditionalGids = mergeGids(s.Process.User.AdditionalGids, gids)
		return nil
	}
}


Review comment (suggestion):
This breaks if `s.Windows.Resources.CPU.Affinity` was not nil.
Perhaps use append and just set the slice cap a L132.

Code after:
// 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
	}
}

// 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) {
		if s.Process == nil {
			s.Process = &runtimespec.Process{}
		}
		gids := s.Process.User.AdditionalGids
		if err := oci.WithAdditionalGIDs(userstr)(ctx, client, c, s); err != nil {
			return err
		}
		// Merge existing gids and new gids.
		s.Process.User.AdditionalGids = mergeGids(s.Process.User.AdditionalGids, gids)